Technical skill practice
Python Tuple: Unpack Coordinates and Check Nested Mutability
A Python tuple is an ordered sequence whose own item positions cannot be reassigned. It is useful for a fixed grouping such as a coordinate pair. That does not make every object inside it immutable: a tuple can contain a list whose contents still change.
Start with a small, explicit task
Store a fictional grid coordinate, unpack its two values, and build a one-item tuple. Then attach a mutable annotation list to a tuple and change the annotation. Compare that successful inner change with an attempted reassignment of a tuple slot.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_tuple.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
4 9
(4,)
('point-A', ['draft', 'checked'])
tuple slot rejected
point = (4, 9)
x, y = point
single = (4,)
annotations = ("point-A", ["draft"])
annotations[1].append("checked")
try:
point[0] = 5
except TypeError:
result = "tuple slot rejected"
else:
raise AssertionError("expected tuple immutability")
assert (x, y) == (4, 9)
assert isinstance(single, tuple) and len(single) == 1
assert annotations == ("point-A", ["draft", "checked"])
print(x, y)
print(single)
print(annotations)
print(result)
Explain the result
Unpacking binds x and y to the two coordinate values. The trailing comma makes single a one-item tuple; parentheses around 4 alone would simply group the integer expression. The failed assignment tries to replace the first position in point, so TypeError is the expected outcome.
The annotation example changes the list object already referenced by the tuple’s second slot. It does not replace that slot. This distinction explains why an immutable outer structure can still expose changing nested state. It also matters for hashing: merely being a tuple does not guarantee that its contents are suitable for use as a dictionary key or set element.
Avoid these mistakes
Unpacking two names requires exactly two items unless you explicitly use a starred target. Do not choose a tuple only to imply deep immutability when the design still includes mutable children. If an independent snapshot is required, decide how each nested value should be copied or represented.
Questions, answered
What creates a one-item tuple?
The comma: (4,) is a tuple, while (4) is the integer 4.
Can a tuple contain a list?
Yes. This example changes that inner list while the tuple’s own positions remain fixed.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.