Lists
Python’s built-in list data type is a sequence, like strings. However, they are mutable, which means you can change them. Lists are like arrays in that they hold a selection of elements in a given order. You can cycle through them, index into them, and slice them:
>>> mylist = ["python", "perl", "php"]>>> mylist['python', 'perl', 'php']>>> mylist + ["java"]["python", 'perl', 'php', 'java']>>> mylist * 2['python', 'perl', 'php', 'python', 'perl', 'php']>>> mylist[1]'perl'>>> mylist[1] = "c++">>> mylist[1]'c++'>>> mylist[1:3]['c++', 'php']
The brackets notation is important: You cannot use parentheses, ((
and )
) or braces ({
and }
) for lists. Using +
for lists is different from using +
for numbers. Python detects that you are working with ...
Get Ubuntu Unleashed 2013 Edition: Covering 12.10 and 13.04, Eighth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.