Keep the shape, lock the slots.
Represent fixed groups and unpack values into meaningful names.
01 The idea
A tuple is an ordered sequence whose slots cannot be reassigned after creation. Tuples are useful for small groups that naturally belong together, such as coordinates. Unpacking assigns each item to a corresponding name in a single statement.
02 The experiment
Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.
position = (12, 8)
x, y = position
print(f"x={x}, y={y}")
print(position[0])
print(("solo",))x=12, y=8
12
('solo',)03 Under the hood
- 1
The tuple stores two coordinates in a known order. x, y = position unpacks both values.
- 2
Indexing and slicing work much as they do with lists, but position[0] = 5 would fail.
- 3
The comma creates a tuple. A one-item tuple needs a trailing comma: ("solo",). Parentheses by themselves just group an expression.
04 Your turn, builder
Unpack the tuple ("Orbit", 3) into project and version, then print "Orbit v3".
Reveal one possible solution +
project, version = ("Orbit", 3)
print(f"{project} v{version}")05 Check your signal
Which expression creates a one-item tuple?
Experiment. Logged.
When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.
Keep exploring
Go deeper with the official Python guide ↗.