Working with sequences

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 ...

Get Python Essentials 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.