Operations
In the previous section, we saw the usefulness of the string slicing and concatenation operations. These operations are also useful with lists, because lists are sequences of elements. We list other commonly used sequence operations in Table 3.3. Note that the first three (in, not in, and +) use operators, the next two use indexing and slicing as described earlier, the next three (len, min, and max) use functions, and the last operation employs a method to determine how many times an object occurs within the specified sequence. Additional sequence operations as well as additional details pertaining to the operations in the table are in the official Python documentation (Python Software Foundation, 2019, “Common Sequence Operations”).
Table 3.3
The Python code in Figure 3.11 illustrates the use of several of these sequence operations on both a string and a list. Lines 2 and 3 of the code in Figure 3.11 construct the list and string, respectively. Lines 6 and 7 use the len function to determine how many elements are in the list and string sequences. Lines 10 and 11 use the .count() method of each sequence type to determine how many times the character “a” appears in each sequence. Lines 14 and 15 determine if the character “e” is in each sequence. Lines 18 and 19 use the slicing operation to report the third to seventh elements in each sequence.
Figure 3.11 Commonly Used Sequence Operations
Figure 3.12 illustrates the output of executing the Python code. Interestingly, the length of the list is only 10, whereas the length of the string is 35, even though the specification of the list appears longer in the code. The explanation for this is that the len function is counting the number of elements, and in the list, some of the elements are several characters or numeric digits. The string length is determined by the number of characters in the string, which includes the spaces between the letters in addition to the string’s alphanumeric characters. Perhaps more surprising is the next output that reports the character “a” appears only once in the list but three times in the string. Looking at the list, we see the letter “a” twice, right at the beginning. However, the second instance is the string “ab” and not solely the character “a,” so it is not a “match” for what we are counting. Because the string is a sequence of characters, the interpreter examines each character individually, and thus the result is 3, as the character “a” appears three times in the string. The next output reports that “e” does not appear in the list but does appear in the string. When looking at line 2 and the construction of the list, we can see the letter “e” in the True and False elements. However, these are Boolean values and so are not a match when checking to see if the character “e” is in the list. The string operates differently, due to the fact that the interpreter examines each character individually. Finally, the output for the slicing operation (recall that the index 2 actually references the third element in the sequence, and even though the index 7 references the eighth element in each sequence, the last value specified in the slice is not included) shows that for a list, five list elements are returned, some being strings, a number, and a Boolean value, whereas for a string, five characters are returned (including a space in the middle in this example). These and other sequence operations are extremely useful in Python, and we will see more applications later in this chapter as well as in later chapters of the textbook.
Figure 3.12 Output from Executing Sequence Operations
SCU 3.4 Sequence Operations
Download the file “SCU 3_4.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and add a line of Python code where indicated to print out the largest value in the list stored in the variable list_variable. Execute the modified program after the change to verify that the revised code runs and produces the correct result.
Lessons learned: In this section, we learned how Python sequence operations can be used on strings and lists to do things like searching for values or counting how many times a value appears in a list of values.
Tuples
We commonly use tuples parameters to functions and for values that functions return. A tuple in Python is like a list (an object that is a collection of objects referenceable using an index), but unlike a list (which is mutable), a tuple is immutable. Because tuples are immutable, they are not useful in all situations. However, there are many reasons to use tuples. In business, there may be values that don’t change (or change rarely, for example, annually). Let’s consider the sales tax rate for several states in a region. As of 2019, the sales tax rates for Chicago, New York, and Los Angeles are 10.25, 8.875, and 9.5, respectively. We could store this in a tuple like so:
sales_tax = (10.25, 8.875, 9.5)
Just like lists, we can access individual elements by index (sales_tax[0], etc.). However unlike lists, calling .append, .sort, or .remove will result in errors. This prevents modification to the tuple later in the program. So in this business example, we can avoid the risk of accidentally changing the sales tax rates in this variable, which makes sense as these rates change infrequently. Likewise, for survey respondents, we could store a tuple of respondent_ID values that could be linked to an individual survey. This can ensure that we have an unchanging set of respondents to study, and using a Python tuple can allow the programmer to maintain a set that will not be modified in any way throughout code that performs analyses.
Tuples are also sequences, and so we can use all the sequence operations just discussed with tuple objects. We can create a tuple object in several ways. We create an empty tuple object by using an assignment statement in which the right-hand side has an empty parenthesis (as opposed to using square brackets for creating a list object). We can create a tuple with only one element by using an assignment in which the right-hand side has a value and a trailing comma. We can include the element and trailing comma in parentheses, but the parentheses are optional, as it is the presence of the comma that indicates the creation of a tuple (Python Software Foundation, 2019, “Tuples”). We can create a tuple with multiple elements by having multiple elements separated by commas on the right-hand side of the assignment statement (optionally enclosed in parentheses).
The Python code in Figure 3.13 is almost identical to the code in Figure 3.1, with the difference being that we are creating a tuple in Figure 3.13 and we created a list in Figure 3.1. The tuple object created on line 3 of Figure 3.13 comprises four objects, the first being a string, the second an integer, the third a float, and the fourth a Boolean-type object. As in a list, each of the objects that are in a tuple can have different data types, and when working with tuples, each element is referenceable using an index value. We reference the first item in the list using the index value 0, the second item using index value 1, and so on. We illustrate this mechanism for referencing items in a tuple in lines 8, 10, 12, and 14 in Figure 3.13.
Figure 3.13 Assigning Tuples
We