Lesson 6 Dealing with Sequences of Objects Chapter 1: Introduction Chapter 2: Lists Chapter 3: More on lists Chapter 4: for loops Chapter 5: Applied problem solving Chapter 6: An army of robots Chapter 7: Tuples [7.1] Tuples [7.2] Initialising tuples [7.3] Accessing tuple elements [7.4] Quiz [7.5] Tuples in robot project Chapter 8: Strings as a sequence Chapter 9: Testing and debugging Chapter 10: Sequences style guide Chapter 11: Summary 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 2point = (5, 8, 2, 7, 9) print(point[3]) Output 7 Check answer! Question 2 What is the output of the following code snippet? Type Error if it results in an error. 1 2point = (5, 8, 2, 7, 9) print(point[-3]) Output 2 Check answer! Question 3 What is the output of the following code snippet? Type Error if it results in an error. 1 2point = ("p", "y", "t", "h", "o", "n") print(point[:2]) Output ("p", "y") Explanation: You can slice a tuple just like a list. Check answer! Question 4 What is the output of the following code snippet? Type Error if it results in an error. 1 2point = (3, 5, 1, 2, 4) print(point[3:1:-1]) Output (2, 1) Explanation: You can also return a sliced and reversed copy of a tuple, like in lists. Check answer! Question 5 What is the output of the following code snippet? Type Error if it results in an error. 1 2bounding_box = ((2,1), (8,9)) print(bounding_box[1][0]) Output 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. Check answer! Question 6 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3bounding_box = ((2,1), (8,9)) bounding_box[0][1] = 3 print(bounding_box[0]) Output Error Explanation: Remember the tuples are immutable, so you cannot modify the elements in a tuple. Check answer! Question 7 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3bounding_box = ((2,1), (8,9)) bounding_box[0] = (1,2) print(bounding_box[0]) Output Error Explanation: Again, you cannot modify elements inside a tuple. Check answer! Question 8 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3bounding_box = [(2,1), (8,9)] bounding_box[0] = (1,2) print(bounding_box[0]) Output (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). Check answer! Question 9 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3bounding_box = [(2,1), (8,9)] bounding_box[0][1] = 3 print(bounding_box[0]) Output Error Explanation: While you can modify the element in a list, you cannot modify the tuple itself. Check answer! Question 10 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3 4x = [1, 2] y = (1, x, 3, 4) x.append(3) print(y[1]) Output [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. Check answer! Question 11 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3x = (1, 2) y = (3) print(x + y) Output 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. Check answer! Question 12 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3x = (1, 2) y = (3,) print(x + y) Output (1, 2, 3) Explanation: You can concatenate two tuples use the + operator to create a new tuple. Check answer! Question 13 What is the output of the following code snippet? Type Error if it results in an error. 1 2 3x = (1, 2) y = [3, 4] print(x + y) Output Error Explanation: You cannot concatenate a tuple with a list. Check answer! 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 7sample = (2, 3, 1, 4, 6) count = 1 for element in sample: if count % 2 == 0: print(element) count += 1 Output 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. Check answer! 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 4sample = (2, 3, 1, 4, 6) for element in sample: if element in (4, 6, 8): print(element) Output 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. Check answer!