Learn with diagrams, code, systems and practical examples.
Tuples
Represent small ordered groups of values that should not be changed in place.
Tuples are useful when a group of values belongs together and should behave like a stable record or key.
Start with the idea
A tuple is ordered like a list but immutable. That makes tuples useful for fixed coordinates, function returns, and composite dictionary keys when their contents are hashable.
Small working example
Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.
location = (34.05, -118.24)
lat, lon = location
print(lat)
print(lon) A tuple is ordered like a list but immutable. That makes tuples useful for fixed coordinates, function returns, and composite dictionary keys when their contents are hashable.
- Tuples are ordered.
- Tuples are immutable.
- Unpacking gives names to tuple positions.
34.05
-118.24The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.
Common beginner mistake
Choosing a tuple merely because it uses fewer characters than a small class or dataclass.
Use tuples for simple positional groups; use named structures when field meaning should be explicit.
Quick recap
- Tuples are ordered.
- Tuples are immutable.
- Unpacking gives names to tuple positions.
Try it yourself
These are deliberately small. If you can complete them without copying the example, you are ready to continue.
- Create a three-value tuple and unpack it.
- Compare when a tuple versus a list better describes your data.