
Fill in
Tuples & Unpacking
A tuple is like a list that can never change. It uses round brackets and is perfect for small, fixed groups like coordinates:
camp = (4, 7)
print(camp[0]) # 4
# camp[0] = 5 -> TypeError: tuples can't be changed
Unpacking
You can split a tuple (or list) into separate variables in one go:
x, y = camp # x is 4, y is 7
a, b = b, a # swap two variables!
Unpacking also works in a for loop over a list of pairs:
for name, age in [("Ada", 12), ("Bo", 11)]:
print(name, "is", age)
Assignment
Replace every ___:
- Unpack
campinto variablesxandy. - Swap
aandbin a single line. - Loop over
shop, unpacking each pair intoitemandprice.
Replace every ___:
- Unpack
campinto variablesxandy. - Swap
aandbin a single line. - Loop over
shop, unpacking each pair intoitemandprice.
main.py Python sleeping
Press Run to try your code, then Check to test it.