import sandbox_widget
import puzzle_widget23 Tuples
Grouping values
A tuple is a sequence of values, just like a list. However, unlike a list, the elements of a tuple can not be changed. You cannot append to a tuple, either. Once a tuple is made, it is immutable (or unchangeable). To make a tuple, you just use round brackets instead of square brackets:
%%sandbox
fruits = ("apple", "banana", "cherry")It may seem strange that Python has both tuples and lists. One reason is that tuples are more efficient, whereas lists are more flexible. We will not use tuples often, but you must know what they are.
You can do most of the operations on a tuple that you can also do on a list. The following exercises should be easy if you remember how to do the same thing on lists:
Exercise 23-1
Find the number of elements in the fruits tuple using the len function.
Exercise 23-2
Extract the second element of the fruits tuple ("banana") using indexing.
Exercise 23-3
Try to change the second element of the fruits tuple to "apple" and see what happens. It should be something like this:
%%sandbox
fruits = ("apple", "banana", "cherry")
fruits[1] = "apple"You cannot change elements of a tuple because they are immutable (once made, they stay that way).
Tuple assignment
Python lets you assign a tuple of values to a tuple of variables like this:
%%sandbox
father, mother, son = ("Donald", "Ivana", "Eric")It does the same as the following three assignments:
%%sandbox
father = "Donald"
mother = "Ivana"
son = "Eric"When a tuple is made, the values are “packed” in sequence:
%%sandbox
family = ("Donald", "Ivana", "Eric")Using the same analogy, values can be “unpacked” using tuple assignment:
%%sandbox
family = ("Donald", "Ivana", "Eric")
father, mother, son = familyThe only requirement is that the number of variables equals the number of values in the tuple.
Once in a while, it is useful to swap the values of two variables. With conventional assignment statements, we have to use a temporary variable. For example, to swap a and b:
temp = a
a = b
b = tempExercise 23-4
Try this and read the error message:
%%sandbox
family = ("Donald", "Ivana", "Eric")
father, mother = familyPaste the error into the assistant and ask it to say, in one sentence, what Python was trying to do when it failed. Then run the fixed version, with three names on the left, to confirm that the sentence was right.
Exercise 23-5
Try this and read the error message:
%%sandbox
family = ("Donald", "Ivana", "Eric")
father, mother, son, daughter = familyCompare to the error message in the previous exercise.
Ask the assistant to explain this one too, and then ask it whether it is the same error as the previous one. If it tells you that they are the same, decide in what sense that is true: the type of error is the same, the message is not, and knowing which of the two you are being told matters more than it sounds.
Exercise 23-6
Say you want to swap the values of two variables, a and b. To do that, you would need to keep one of the values in an extra variable like this:
temp = a
a = b
b = tempUsing what you have learned in this chapter, can you devise a simple and pretty way of swapping a and b in one statement? It may occur to you before you realize how it works, so make sure you can connect your solution to the rules of tuples and tuple assignment.
%%puzzle (2, 1)
a, b = b, a
b = 2
(a, b)
a = 1