This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 7: Tuples

Quiz

face Josiah Wang

Let’s see whether you really understand tuples!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Question 1

What is the output of the following code snippet? Type Error if it results in an error.

1
2
point = (5, 8, 2, 7, 9)
print(point[3])
7

Question 2

What is the output of the following code snippet? Type Error if it results in an error.

1
2
point = (5, 8, 2, 7, 9)
print(point[-3])
2

Question 3

What is the output of the following code snippet? Type Error if it results in an error.

1
2
point = ("p", "y", "t", "h", "o", "n")
print(point[:2])
("p", "y")
Explanation:

You can slice a tuple just like a list.

Question 4

What is the output of the following code snippet? Type Error if it results in an error.

1
2
point = (3, 5, 1, 2, 4)
print(point[3:1:-1])
(2, 1)
Explanation:

You can also return a sliced and reversed copy of a tuple, like in lists.

Question 5

What is the output of the following code snippet? Type Error if it results in an error.

1
2
bounding_box = ((2,1), (8,9))
print(bounding_box[1][0])
8
Explanation:

As you would expect, you can have a nested tuple. In this example, bounding_box[1] is (8,9), and bounding_box[1][0] would be the first element 8. This is an example useful in Computer Vision or Image Processing tasks, such as object detection, where (2,1) represents the coordinates of the top left corner of a box, and (8,9) represents the coordinates of the bottom right corner of a box.

Question 6

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
bounding_box = ((2,1), (8,9))
bounding_box[0][1] = 3
print(bounding_box[0])
Error
Explanation:

Remember the tuples are immutable, so you cannot modify the elements in a tuple.

Question 7

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
bounding_box = ((2,1), (8,9))
bounding_box[0] = (1,2)
print(bounding_box[0])
Error
Explanation:

Again, you cannot modify elements inside a tuple.

Question 8

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
bounding_box = [(2,1), (8,9)]
bounding_box[0] = (1,2)
print(bounding_box[0])
(1,2)
Explanation:

This piece of code works because you are modifying the element inside a list (the first element in the list is now pointing to a different tuple).

Question 9

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
bounding_box = [(2,1), (8,9)]
bounding_box[0][1] = 3
print(bounding_box[0])
Error
Explanation:

While you can modify the element in a list, you cannot modify the tuple itself.

Question 10

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
4
x = [1, 2]
y = (1, x, 3, 4)
x.append(3)
print(y[1])
[1, 2, 3]
Explanation:

Beware of side-effects! While you cannot modify a tuple, you can modify the value of an element if the element itself is mutable, in this case a list. After Line 2, y is (1, [1,2], 3, 4). After Line 3, y is (1, [1,2,3], 3, 4) since y[1] points to whatever object x is pointing.

Question 11

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
x = (1, 2)
y = (3)
print(x + y)
Error
Explanation:

You cannot concatenate a tuple with an int. Remember that you need to add a comma if you have only one element in a tuple, otherwise Python will treat the element as an integer inside a pair of parentheses.

Question 12

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
x = (1, 2)
y = (3,)
print(x + y)
(1, 2, 3)
Explanation:

You can concatenate two tuples use the + operator to create a new tuple.

Question 13

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
x = (1, 2)
y = [3, 4]
print(x + y)
Error
Explanation:

You cannot concatenate a tuple with a list.

Question 14

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

1
2
3
4
5
6
7
sample = (2, 3, 1, 4, 6)
count = 1
for element in sample:
    if count % 2 == 0:
        print(element)

    count += 1
3 4
Explanation:

Just like a list, you can iterate over tuples with a for loop. This piece of code only prints out even-positioned elements in sample.

Question 15

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

1
2
3
4
sample = (2, 3, 1, 4, 6)
for element in sample:
    if element in (4, 6, 8):
        print(element)
4 6
Explanation:

This piece of code will iterate over the elements in sample. For each element, it will print out the element if it is either 4, 6, or 8.