Sequences in the Python Programming Language
Errors using inadequate data are much less than those using no data at all.
In This Chapter
Shared sequence operations
Lists and tuples
Strings and string methods
In Chapter 2, “Fundamentals of Python,” you learned about collections of types. This chapter introduces the group of built-in types called sequences. A sequence is an ordered, finite collection. You might think of a sequence as a shelf in a library, where each book on the shelf has a location and can be accessed easily if you know its place. The books are ordered, with each book (except those at the ends) having books before and after it. You can add books to the shelf, and you can remove them, and it is possible for the shelf to be empty. The built-in types that comprise a sequence are lists, tuples, strings, binary strings, and ranges. This chapter covers the shared characteristics and specifics of these types.
The sequences family shares quite a bit of functionality. Specifically, there are ways of using sequences that are applicable to most of the group members. There are operations that relate to sequences having a finite length, for accessing the items in a sequence, and for creating a new sequence based a sequence’s content.
You can test whether an item is a member of a sequence by using the in operation. This operation returns True if the sequence contains an item that evaluates as equal to the item in question, and it returns False otherwise. The following are examples of using in with different sequence types:
'first' in ['first', 'second', 'third'] True 23 in (23,) True 'b' in 'cat' False b'a' in b'ieojjza' True
You can use the keyword not in conjunction with in to check whether something is absent from a sequence:
'b' not in 'cat' True
The two places you are most likely to use in and not in are in an interactive session to explore data and as part of an if statement (see Chapter 5, “Execution Control”).
Because a sequence is an ordered series of items, you can access an item in a sequence by using its position, or index. Indexes start at zero and go up to one less than the number of items. In an eight-item sequence, for example, the first item has an index of zero, and the last item an index of seven.
To access an item by using its index, you use square brackets around the index number. The following example defines a string and accesses its first and last substrings using their index numbers:
name = "Ignatius" name 'I' name 't'
You can also index counting back from the end of a sequence by using negative index numbers:
name[-1] 's' name[-2] 'u'
You can use indexes to create new sequences that represent subsequences of the original. In square brackets, supply the beginning and ending index numbers of the subsequence separated by a colon, and a new sequence is returned:
name = "Ignatius" name[2:5] 'nat'
The subsequence that is returned contains items starting from the first index and up to, but not including, the ending index. If you leave out the beginning index, the subsequence starts at the beginning of the parent sequence; if you leave out the end index, the subsequence goes to the end of the sequence:
name[:5] 'Ignat' name[4:] 'tius'
You can use negative index numbers to create slices counting from the end of a sequence. This example shows how to grab the last three letters of a string:
If you want a slice to skip items, you can provide a third argument that indicates what to count by. So, if you have a list sequence of integers, as shown earlier, you can create a slice just by using the starting and ending index numbers:
scores = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] scores[3:15] [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
But you can also indicate the step to take, such as counting by threes:
scores[3:15:3] [3, 6, 9, 12]
To count backward, you use a negative step:
scores[18:0:-4] [18, 14, 10, 6, 2]
You can perform shared operations on sequences to glean information about them. Because a sequence is finite, it has a length, which you can find by using the len function:
name = "Ignatius" len(name) 8
You can use the min and max functions to find the minimum and maximum items, respectively:
scores = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] min(scores) 0 max(name) 'u'
These methods assume that the contents of a sequence can be compared in a way that implies an ordering. For sequence types that allow for mixed item types, an error occurs if the contents cannot be compared:
max(['Free', 2, 'b']) ----------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-15-d8babe38f9d9> in <module>() ----> 1 max(['Free', 2, 'b']) TypeError: '>' not supported between instances of 'int' and 'str'
You can find out how many times an item appears in a sequence by using the count method:
You can get the index of an item in a sequence by using the index method:
You can use the result of the index method to create a slice up to an item, such as a letter in a string:
You can perform addition and multiplication with sequences of the same type. When you do, you conduct these operations on the sequence, not on its contents. So, for example, adding the list  to the list  will produce the list [1,2], not . Here is an example of using the plus (+) operator to create a new string from three separate strings:
"prefix" + "-" + "postfix" 'prefix-postfix'
The multiplication (*) operator works by performing multiple additions on the whole sequence, not on its contents:
[0,2] * 4 [0, 2, 0, 2, 0, 2, 0, 2]
This is a useful way of setting up a sequence with default values. For example, say that you want to track scores for a set number of participants in a list. You can initialize that list so that it has an initial score for each participant by using multiplication:
num_participants = 10 scores =  * num_participants scores [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]