1.2.23 · D5Introduction to Programming (Python)

Question bank — Tuples — immutability, use cases

1,136 words5 min readBack to topic

Parent: Tuples — immutability, use cases · Related: Mutable vs Immutable objects, Hashing in Python, Lists — mutability, methods.


True or false — justify

True or false: (3) is a one-element tuple.
False. Parentheses only group here, so (3) is just the integer 3; the comma makes a tuple, so you need (3,).
True or false: A tuple can hold different data types at once.
True. Tuples are heterogeneous, so ("Asha", 21, 5.6) is perfectly valid — order is fixed but types may differ.
True or false: t = t + (9,) modifies the original tuple t.
False. + builds a brand-new tuple and rebinds the name t; the old object was never touched, which is why immutability still holds.
True or false: Every tuple can be used as a dictionary key.
False. Only tuples whose items are all hashable qualify; (1, [2]) contains a list, so it stays unhashable and raises TypeError.
True or false: Tuples are always faster and lighter than lists.
Mostly true but nuanced. Tuples carry less overhead and construct slightly faster, but you pick them for immutability and intent, not raw speed.
True or false: x, y = t requires t to have exactly two items.
True. Unpacking matches count exactly; a size mismatch raises ValueError: too many/not enough values to unpack.
True or false: A tuple containing a list is immutable.
Half true. The tuple's slots are frozen, but the list living in a slot can still be mutated — immutability is shallow.
True or false: 3, 4 without any parentheses is a tuple.
True. The comma alone builds it; Python evaluates 3, 4 to (3, 4).
True or false: () is a valid empty tuple.
True. The empty tuple is the one case where parentheses (not a comma) are needed, since there's nothing to comma-separate.

Spot the error

t = (1, 2, 3); t[0] = 99 — what's wrong?
Item assignment overwrites a slot, which tuples forbid; this raises TypeError: 'tuple' object does not support item assignment.
d = {[0, 0]: "origin"} — what's wrong?
A list is mutable and therefore unhashable, so it can't be a dict key; you get TypeError: unhashable type: 'list'. Use (0, 0) instead.
single = (42) — why isn't this a tuple?
There's no comma, so the parentheses only group 42; type(single) is int. Write (42,).
t.append(4) on a tuple — what fails and why?
AttributeError — tuples have no append (it would mutate); their only methods are count and index.
t = (1, [2, 3]); t[1] = [9] — which line fails?
This one fails with TypeError: replacing the slot is forbidden even though the inner list is mutable. t[1].append(9) would have succeeded.
a, b, c = (1, 2) — what breaks?
Unpacking demands equal counts; three names but two values gives ValueError: not enough values to unpack.
x = 5, then print(len(x)) — beginner expects an error; what really happens?
No error — the trailing comma silently made x the tuple (5,), so len(x) is 1. A stray comma is a classic accidental-tuple bug.

Why questions

Why does Python require a stable hash for dictionary keys?
The dict locates a key by its hash first; if the hash could change after insertion, the entry would be filed under a hash the dict can no longer compute, so it'd be lost forever. See Hashing in Python.
Why are tuples hashable but lists are not?
Hashability requires the value never change; tuples of immutable items can't change so their hash is permanent, whereas lists can be edited at any time and would break the equal objects ⇒ equal hashes rule.
Why is return a, b from a function actually returning a tuple?
The comma packs a and b into a single tuple object, which the caller then unpacks with lo, hi = f() — see Functions — return values.
Why does swapping a, b = b, a need no temporary variable?
The right side b, a is evaluated into a temporary tuple before any assignment, so both new values are already captured when the unpacking writes them back.
Why choose a tuple over a list for a 2D coordinate?
A coordinate is a fixed record whose identity shouldn't change, and it may serve as a dict key or set element — all needs a list (mutable, unhashable) can't meet.
Why does concatenating tuples not violate immutability?
Because + never edits an existing tuple; it allocates a new one, leaving every original object exactly as it was.
Why do tuples expose only count and index?
Every other sequence method (append, sort, remove, insert) mutates in place, which immutability forbids; only read-only queries survive.

Edge cases

What is type((5,)) versus type((5))?
(5,) is tuple (comma present); (5) is int (comma absent). The comma is the deciding factor.
Can a tuple be empty, and how is it written?
Yes — (). This is the sole case where bare parentheses (no comma) produce a tuple, because there's nothing to separate.
Is (1, [2, 3]) allowed as a set element?
No — the inner list makes the whole tuple unhashable, so adding it to a set raises TypeError, exactly as with dict keys (Sets — hashing & uniqueness).
Does t * 0 or t[5:5] on a tuple error out?
No — both simply return the empty tuple (); repeat-by-zero and out-of-range slices are safe read operations, unlike out-of-range indexing which raises IndexError.
If t = (1, [2, 3]) and you do t[1].append(4), what is t afterward, and is that a bug?
t becomes (1, [2, 3, 4]) — legal because only the inner list mutated, not the tuple's slots. It's a common surprise, not an error.
What happens with t = 1, (comma, nothing after)?
You get the one-element tuple (1,); a trailing comma with a single value is enough to build a tuple without any parentheses.