Frederick Kaefer

Introduction to Python Programming for Business and Social Science Applications


Скачать книгу

in Figure 3.13 in Figure 3.14. The only difference between this output and the output from executing the Python code in Figure 3.1 is that the data type for the taxi_ride_info variable is now a tuple. It is important to note that we cannot use the same .append(), .insert(), .pop(), or .remove() methods that we used with a list on a tuple because a tuple is immutable. However, as we have just seen, we can examine and assign indexed elements of a tuple just as we were able to do with a string, which is another immutable sequence object.

      A screenshot displays the output of the Python code for assigning tuples.Description

      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.

      A screenshot displays a Python code illustrating the GSS Tuples example.Description

      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.

      A screenshot displays the output from the execution of the Python code for the GSS Tuples example.Description

      Figure 3.16 Output from GSS Tuples Example

       Stop, Code, and Understand!

      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.

A screenshot displays two lines of code in Python IDLE editor as follows. Line 1: tuple_variable = (3, 5, 7, 3, 2, 5, 2, 7, 3, 3). Line 2: # Add a line of code to print out how many times the value 3 is in the tuple.

      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.

      A screenshot displays a Python code in a Python file, illustrating the GSS dictionary example.Description

      Figure 3.17 GSS Dictionary Example

      A screenshot displays the output from the execution of the Python code of the GSS dictionary example.Description

      Figure 3.18 Output from GSS Dictionary Example

       Stop, Code, and Understand!

      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