Swipe to navigate through the chapters of this book
This chapter introduces a new concept: data structures. A data structure is a collection of data elements (such as numbers or characters, or even other data structures) that is structured in some way, such as by numbering the elements. The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number—its position, or index. The first index is zero, the second index is one, and so forth. Some programming languages number their sequence elements starting with one, but the zero-indexing convention has a natural interpretation of an offset from the beginning of the sequence, with negative indexes wrapping around to the end. If you find the numbering a bit odd, I can assure you that you’ll most likely get used to it pretty fast.
Please log in to get access to this content
To get access to this content you need the following product:
It’s actually a class, not a function, but the difference isn’t important right now.
Actually, from version 2.2 of Python, list is a type, not a function. (This is the case with tuple and str as well.) For the full story on this, see the section “Subclassing list, dict, and str” in Chapter 9.
In case you’re interested, from Python 2.3 on, the sort method uses a stable sorting algorithm.
The sorted function can, in fact, be used on any iterable object. You learn more about iterable objects in Chapter 9.
Like list, tuple isn’t really a function—it’s a type. And, as with list, you can safely ignore this for now.
- Lists and Tuples
Magnus Lie Hetland
- Sequence number
- Chapter number
- Chapter 2