Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Creating Coordinates
Which is the correct way to create a tuple for coordinates (x=5, y=3)?
position = (5, 3)position = [5, 3]position = {5, 3}position = ((5, 3))Hint : [] is for lists, {} is for sets, and () is for tuples.
Exercise 2 — Codémon Stats Mutability
What happens when you try to change a Codémon's base stats stored in a tuple?
base_stats = (100, 85, 90) # HP, Attack, Defense
base_stats[0] = 120Hint : Think about it, are tuples mutable or immutable?
Exercise 3 — Accessing Codémon Position
Complete the code to get the x and y coordinates from a Codémon's position tuple.
Hint : Declaring tuples and accesing their elements are not the same.
Exercise 4 — Tuple vs List Properties
Match each data structure with its key characteristic for Codémon data storage.
tuplelist()[]Exercise 5 — Codémon Tuple Unpacking
What will be the values of name and level after this Codémon tuple unpacking?
codemon_info = ("Aquachu", 25)
name, level = codemon_infoname = "Aquachu", level = 25name = 25, level = "Aquachu"name = ("Aquachu", 25), level = NoneHint : Think about tuple unpacking.
Exercise 6 — Codémon Tuple Methods
Complete the code to count how many times "Fire" appears in a Codémon's type history tuple.
Hint : The method to count occurrences in tuples is the same than for lists.
Exercise 7 — When to Use Tuples vs Lists
Which data should be stored in a tuple rather than a list?
Hint : Think about the difference of use between tuples and lists.
Exercise 8 — Single Element Codémon Tuple
Create a tuple containing only one Codémon name. Is this code correct and just need a space?
Hint : Single-element tuples are a bit special..
Exercise 9 — Combining Codémon Tuples
What is the result of combining these two Codémon coordinate tuples?
pos1 = (1, 2)
pos2 = (3, 4)
combined = pos1 + pos2(1, 2, 3, 4)(4, 6)[(1, 2), (3, 4)]Hint : Think about concatenation.
Exercise 10 — Tuple vs List Methods
Match each method with the data structure(s) that support it for Codémon data.
.count().append().index().remove()Exercise 11 — Codémon Stats Unpacking
Unpack the Codémon stats tuple into separate variables for HP, Attack, and Defense.
Hint : Think about tuples unpacking.
Exercise 12 — Nested Codémon Tuples
How do you access Pyrochu's attack stat from this nested tuple? (attack is the third element in Pyrochu's tuple)
codemon_data = (("Aquachu", 100, 80), ("Pyrochu", 90, 110))codemon_data[1][2]codemon_data[2][1]codemon_data[0][2]codemon_data[1][1]Hint : Think about indexing.
Exercise 13 — Converting Lists to Tuples
How do you convert a Codémon inventory list to a tuple to make it unchangeable?
invent_l = ["potion", "crystal", "gem"]invent_t = tuple(invent_l)invent_t = (invent_l)invent_t = list.to_tuple()invent_t = tuple.convert(invent_l)Hint : Think about casting.
Exercise 14 — Repeating Codémon Tuples
Complete the code to create a tuple with 3 times the position in it. (5, 10, 5, 10, 5, 10)
Hint : Think about repetition of tuples.
Exercise 15 — Immutable Data
What makes tuples better than lists for storing base coordinates that should never change?
Hint : Think about mutable and immutable.
Exercise 16 — Tuple Operations Results
Match each tuple operation with its result.
(1, 2) + (3, 4)(1, 2) * 2len((5, 10, 15))("A", "B")[1](1, 2, 3, 4)(1, 2, 1, 2)3"B"Exercise 17 — Codémon Tuple Slicing
Complete the code to get the first two Codémon names from the tuple.
Hint : Think about slicing.
Exercise 18 — Modifying Codémon Data
Which operation is possible with an inventory list but NOT with a tuple?
inventory.append("new_item")inventory[0]len(inventory)inventory.count("potion")Hint : Think about the main difference between lists and tuples.
Exercise 19 — Checking Codémon in Tuple
Complete the code to check if "Aquachu" is in the Codémon team tuple.
Hint : Think how to check membership in tuples and list!
Exercise 20 — Codémon Team Size
How do you find out how many Codémon are in this team tuple?
team = ("Aquachu", "Pyrochu", "Florachu")len(team)team.size()team.count()team.lengthHint : Think about built-in methods.
Exercise 21 — Data Storage Choices
Match each data type with the best storage choice.
tuple - fixed position datalist - add/remove itemstuple - color values don't changelist - continuously updatedExercise 22 — Converting Data
Complete the code to convert a tuple to a list for modification.
Hint : Think about casting.
Exercise 23 — Mixed Data Types in Tuples
What is the result of accessing different elements from this mixed Codémon data tuple?
codemon = ("Aquachu", 25, True, 98.5)
name = codemon[0]
level = codemon[1]
is_rare = codemon[2]name="Aquachu", level=25, is_rare=TrueHint : Think about unpacking.
Exercise 24 — Stat Analysis
Complete the code to find the highest and lowest stats from a stat tuple.
Hint : Think about methods.
Exercise 25 — Tuple vs List Performance
Why might you choose tuples over lists for storing stats that never change?
Hint : Think about Tuple vs List. This is a logical answer.