What this topic tests
- Pack and unpack values, including the one-item tuple comma.
- Predict what happens when code tries to change a tuple.
- Use tuples where a fixed shape or a hashable key is useful.
Common mistakes
- Writing (1) when you need a one-item tuple. The comma in (1,) is required.
- Trying to assign to tuple[index]. Tuples do not support item assignment.
- Assuming a tuple is always faster or always better; choose based on whether the data should change.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming stores a milk tea recipe ratio in a tuple. What is the biggest difference between a tuple and a list?
Show answer
Answer
B. tuples are immutable; lists are mutableWhy: A tuple is ordered and immutable; a list is ordered and mutable. Ordering is shared, so immutability is the real difference.
-
2. 🧋 What does it mean that a tuple cannot be changed?
Show answer
Answer
B. the tuple's elements cannot be modifiedWhy: Immutability means the contents cannot be modified in place; the name can still be rebound. The tuple's identity and length are fixed.
-
3. 🧋 What is the type of (42,)?
Show answer
Answer
A. tupleWhy: The comma after 42 makes this a one-element tuple, so type() reports tuple; this is the smallest legal tuple. Its length is 1.
-
4. Which method counts how often a value appears in a tuple?
Show answer
Answer
A. countWhy: count(value) is available on tuples and returns the number of equal elements. index gives a position, find belongs to strings, and len counts everything.
-
5. Ming splits the tuple (name, price) = ("milk tea", 15) into two variables. Which tuple mechanism is this?
Show answer
Answer
unpackingWhy: The right side packs two values and the left side unpacks them position by position. This is tuple unpacking, not slicing or copying.
-
6. 🧋 Swap variables with tuple unpacking: a, b = ___
Show answer
Answer
b, aWhy: The right side is evaluated first, building the tuple (b, a), then it is unpacked back into a and b. That swaps the values without a temporary.
-
7. Which standard-library factory creates a lightweight tuple with named fields?
from collections import ____ Point = namedtuple('Point', ['x', 'y'])Show answer
Answer
namedtupleWhy: namedtuple builds a tuple subclass whose fields can also be read by name, so point.x works alongside point[0]. It is still a tuple, hence immutable.
-
8. What is printed after unpacking and tuple addition?
t = (1, 2, 3) a, b, c = t print(b, t + (4,))Show answer
Answer
2 (1, 2, 3, 4)Why: Unpacking binds 1, 2, 3 to a, b, c in order, so b is 2; adding (4,) returns a new tuple (1, 2, 3, 4) without touching t. Nothing is mutated anywhere.
-
9. Read the code and write the output.
for i, ch in enumerate("ab"): print(i, ch)Show answer
Answer
0 a 1 b
Why: enumerate yields (index, character) pairs, and print separates them with a space, so the output is 0 a then 1 b. The index starts at 0.
-
10. Read the inner tuple's first element. What is printed?
t = (1, (2, 3)) print(t[1][0])Show answer
Answer
2Why: t[1] returns the inner tuple (2, 3), and [0] then takes its first element, so 2 is printed. Chained indexing walks one level at a time.
Common questions
Why does a one-item tuple need a comma?
Parentheses group an expression; the comma creates the tuple. (1) is just the integer 1, while (1,) is a tuple.
Can a tuple contain a list?
Yes. The tuple cannot be reassigned, but a mutable object inside it can still change.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.