Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Creating Coordinates

Which is the correct way to create a tuple for coordinates (x=5, y=3)?

Options
• 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] = 120
Options
• TypeError: tuples are immutable
• The HP changes to 120
• A new tuple is created
• The tuple becomes a list

Hint : 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.

Items to match
• tuple
• list
• ()
• []
Definitions
• Immutable (cannot be changed)
• Mutable (can be modified)
• Tuple syntax
• List syntax

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_info
Options
• name = "Aquachu", level = 25
• name = 25, level = "Aquachu"
• name = ("Aquachu", 25), level = None
• Error: cannot unpack tuple

Hint : 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?

Options
• RGB color values (255, 100, 50) - fixed data
• Inventory items - can add/remove items
• Battle log - continuously updated
• Player scores - changes over time

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
Options
• (1, 2, 3, 4)
• (4, 6)
• [(1, 2), (3, 4)]
• Error: cannot add tuples

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.

Items to match
• .count()
• .append()
• .index()
• .remove()
Definitions
• Both tuples and lists
• Only lists
• Both tuples and lists
• Only lists

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))
Options
• 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"]
Options
• 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?

Options
• Tuples are immutable - they prevent accidental changes
• Tuples can store more data than lists
• Tuples have more methods available
• Tuples use different syntax

Hint : Think about mutable and immutable.

Exercise 16 — Tuple Operations Results

Match each tuple operation with its result.

Items to match
• (1, 2) + (3, 4)
• (1, 2) * 2
• len((5, 10, 15))
• ("A", "B")[1]
Definitions
• (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?

Options
• 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")
Options
• len(team)
• team.size()
• team.count()
• team.length

Hint : Think about built-in methods.

Exercise 21 — Data Storage Choices

Match each data type with the best storage choice.

Items to match
• Map coordinates (x, y)
• Shopping cart items
• RGB color (255, 128, 0)
• Battle log entries
Definitions
• tuple - fixed position data
• list - add/remove items
• tuple - color values don't change
• list - continuously updated

Exercise 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]
Options
• name="Aquachu", level=25, is_rare=True
• All variables become strings
• Error: mixed types not allowed
• All variables become the same type

Hint : 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?

Options
• Tuples are faster and use less memory for fixed data
• Tuples can store more elements than lists
• Tuples have more built-in methods
• Tuples work better with loops

Hint : Think about Tuple vs List. This is a logical answer.

Back to the course