In this chapter, we'll introduce Python sequence collections. We'll look at strings and tuples as the first two examples of this class. Python offers a number of other sequence collections; we'll look at them in Chapter 6, More Complex Data Types. All of these sequences have common features.
Python sequences identify the individual elements by position. Position numbers start with zero. Here's a tuple
collection with five elements:
>>> t=("hello", 3.14, 23, None, True) >>> t[0] 'hello' >>> t[4] True
In addition to the expected ascending numbers, Python also offers reverse numbering. Position -1
is the end of the sequence:
>>> t[-1] True >>> t[-2] >>> t[-5] 'hello'
Note that position 3 (or -2) has a value of None
. The REPL doesn't display the None
object, so the value of t[-2]
appears to be missing. For more visible evidence that this value is None
, use this:
>>> t[3] is None True
The sequences use an extra comparison...