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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access