Python · Intermediate · 10 min
Tuples and unpacking
Use fixed groups of values and unpack them into clear names.
Keep values together
A tuple is an ordered collection that cannot be changed after creation. It is useful for a small fixed record such as a coordinate.
point = (4, 7)
print(point[1])Output
7
Unpack by position
Unpacking assigns each tuple item to a variable in one step. The number of names must match the number of items.
name, score = ("Mina", 92)
print(f"{name}: {score}")Output
Mina: 92
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Unpack a booking
Unpack booking into guest, nights, and room. Display Ada stays 3 nights in 204.
Place three variable names before = booking.
Tuples and unpacking
- Read both tuples and unpacking examples before answering.
- Run the task, compare its exact output, and revise the code if needed.