3.4 Tuples
Tuples are like lists except:
- You use parentheses instead of square brackets, and the parentheses are often optional.
- You can’t change the structure of a tuple: they are immutable.
We saw tuples when we did multiple assignments. We did not need parentheses then, but we could have included them if we wished.
a, b = 3, -2
print(f"{a = } {b = }")
a, b = (9, 0.3)
print(f"{a = } {b = }")
(a, b) = ("a", "b")
print(f"{a = } {b = }")
a = 3 b = -2
a = 9 b = 0.3
a = 'a' b = 'b'
Exercise 3.18
What exception does Python raise if you try to assign a value to an item in a tuple via an index?
Use tuple to turn a list into a tuple and list to turn a tuple into a list.
t = 2, -4, 9
t
list(t)
tuple([-16, 25, -36])
(-16, 25, -36)
Exercise 3.19
...