Python theorytheory 0/50 · 0%
Collections · easy

6. Tuples and immutability

Fixed-size, immutable sequences good for records.

Tuples look like lists but are immutable — once created, their elements cannot be reassigned. They are ideal for fixed records like coordinates or (key, value) pairs.

python
point = (10, 20)
x, y = point          # unpacking
single = (5,)          # trailing comma required for one-element tuples

Because tuples are immutable and hashable (if their contents are), they can be used as dictionary keys, unlike lists.

python
balances = {("alice", "ETH"): 3.5}

Check your understanding

  1. 1. What makes `(5,)` different from `(5)`?

  2. 2. Why can tuples be dictionary keys but lists cannot?

  3. 3. What does `x, y = (10, 20)` do?