A tuple is an ordered collection like a list, but immutable — once created it can't change. That constraint is a feature: it signals "this grouping is fixed", makes the value hashable (usable as a dict key), and prevents accidental modification.
Creating & reading tuples
point = (3, 4)point[0] # 3 index like a listlen(point) # 2# point[0] = 9 # ERROR — tuples can't be changedsingle = (5,) # a one-item tuple needs the trailing commaempty = ()
Unpacking & multiple return
Tuples power two everyday idioms: assigning several names at once, and returning multiple values from a function:
x, y = (3, 4) # x=3, y=4a, b = b, a # swap — the right side is a tupledef min_max(xs):return min(xs), max(xs) # returns a tuplelo, hi = min_max([5, 2, 9]) # unpack it → lo=2, hi=9first, *rest = [1, 2, 3, 4] # first=1, rest=[2,3,4]
Why immutability matters
Key takeaways
- 01Tuples are ordered and immutable — they cannot change after creation.
- 02A one-item tuple needs a trailing comma: (5,).
- 03Tuple unpacking assigns several names at once; functions return multiple values as tuples.
- 04Being immutable/hashable, tuples can be dict keys and set members — lists cannot.
- 05Use tuples for fixed records; use lists for collections that change.
