Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 87

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.

Description

Figure 3.13 Assigning Tuples

We show the output from executing the Python code 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.

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.

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.

Description

Figure 3.16 Output from GSS Tuples Example

Introduction to Python Programming for Business and Social Science Applications

Подняться наверх