import sandbox_widget
import puzzle_widget22 Tuples
Grouping values
A tuple is a fixed sequence of values. Unlike a list, the elements of a tuple can not be changed (it is “immutable”). Once created, a tuple is as fixed as the value 4. You cannot append to a tuple, either. You make a tuple like you make a list, except with round brackets instead of square bracketsÆ
%%sandbox
fruits = ("apple", "banana", "cherry")It may seem like overkill to have 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 22-1
Find the number of elements in the fruits tuple using the len function.
Exercise 22-2
Extract the second element of the fruits tuple ("banana") using indexing.
Exercise 22-3
Try to put the code below in a code cell and run it to see what happens. Which rule does it violate?
%%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.
Exercise 22-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 22-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 22-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