-
Book Overview & Buying
-
Table Of Contents
IPython Notebook Essentials
By :
Python sequence types are used to represent ordered collections of objects. They are classified into mutable and immutable sequence types. Here, we will only discuss lists (mutable) and tuples and strings (both immutable). Other sequence types are mentioned at the end of this section.
The following example shows how to construct a list in Python and assign it to a variable:
numbers = [0, 1.2, 234259399992, 4+3j]
Individual entries in the list are accessed with index notation as follows:
numbers[2]
Notice that indexing always starts with 0. Negative indices are allowed and they represent positions starting at the end of the list. For example, numbers[-1] is the last entry, numbers[-2] is the next-to-last entry, and so forth.
Since lists are a mutable sequence type, we are allowed to modify the entries in-place:
numbers[0] = -3 numbers[2] += numbers[0] print numbers
Another important way to refer to elements in a Python sequence type is slices, which allow the extraction of...
Change the font size
Change margin width
Change background colour