Figure 3.14 Output from Assigning Tuples
Figure 3.15 illustrates the creation and use of a tuple using data related to the GSS data set that we will be working with throughout the textbook. We introduce and describe this data set in Chapter 1 (see Tables 1.4 and 1.5). Line 2 of the code in Figure 3.15 creates a tuple containing ages of a selection of survey respondents. Lines 4 through 6 use some common sequence operations on the tuple to report how many respondents were in the tuple, how old the oldest respondent was among the respondents, and how old the youngest respondent was among the respondents.
Figure 3.15 GSS Tuples Example
The output from executing the code in Figure 3.15 is in Figure 3.16. This output illustrates that there were seven respondents, the oldest was 59 years old, and the youngest was 19 years old. We could have seen this ourselves from the values assigned to the respondent_ages tuple object, but from a much larger number of respondents, this would been much more difficult to discern visually and just as easy for the Python code to determine and report to us.
Figure 3.16 Output from GSS Tuples Example
SCU 3.5 Tuple Operations
Download the file “SCU 3_5.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 number of times the value 3 is in the tuple. 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 about tuples, which are immutable sequences. Tuples have important usages when we have sequences of elements that we do not want to change.
Dictionaries
Another very useful data type to learn is the dictionary. A dictionary (commonly referred to as a dict) is a mapping that stores data in key-value pairs, rather than simply a list or sequence of values. A mapping is a collection that allows you to look up information associated with key values (Zelle, 2010). Dictionary keys locate values within a dictionary and can be any immutable type, most often being strings or numbers. However, you should not use floating point numbers as dictionary keys because computers store them as approximations (Python Software Foundation, 2019, “Mapping Types—dict”). Known in other languages as associative arrays or hash tables, this enables the programmer to store multiple related data values in one structure and reference them by their specified key values. Consider a physical dictionary. The “key” is the word you would like to reference, and the “value” would be the definition for which you are searching. Understanding how dictionaries work will help you when we work with data from the Web using JavaScript Object Notation (JSON; in Chapter 7) and when we transform data into required formats for data visualization (in Chapter 9).
One way to create a dictionary object is to use an assignment statement in which the right-hand side has key-value pairs specified in curly braces “( ).” We specify the key-value pairs within the curly braces using the format key:value and separate the pairs by commas. To illustrate this, the Python code in Figure 3.17 creates a simple dictionary based on some overall counts of survey information. Lines 2 to 4 of the code in Figure 3.17 identify the keys in the dictionary to be “survey_count,” “2014_takers,” and “2016_takers” and the corresponding values for the keys are 1000, 43, and 52, respectively. It can be very useful to store data in a structure where all the data elements are related and to look up a value using a string key that makes sense in that context. The output from running this code is shown in Figure 3.18.
Figure 3.17 GSS Dictionary Example
Figure 3.18 Output from GSS Dictionary Example
SCU 3.6 Reference a Dictionary
Download the file “SCU 3_6.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 modify the indicated line of code to print the just the taxi driver’s name from the dictionary. Execute the modified program after the change to verify that the revised code runs and produces the correct result.
Dictionary Operations
We list several commonly used dictionary operations in Table 3.4. We use the first operation listed in the table, “dict,” to construct a dictionary from information specified in several different ways. This is different from just specifying the key-value pairs as we did earlier, because we can use mapping objects as well as data that reside in other sequences of values. The second operation in the table is simply using the dictionary name along with a key value in square brackets to look up the value corresponding to the key value in the dictionary. This may seem like how we have referenced other sequences by using indexes, but there is a clear distinction. The key value does not have to be a number, and even if it is a number, it does not have to follow any specific pattern. The next operation assigns a specified value to the dictionary entry with a specified key value. If that key value already is present in the dictionary, the interpreter updates its corresponding value. If it is not present in the dictionary, the interpreter creates a new entry. The next operation list in the table uses the “del” keyword to remove the specified entry (referenced