Tuples
A tuple is like a list, but immutable: once created it cannot be changed. Tuples are perfect for fixed groups of values that belong together, like a coordinate (x, y) or an RGB color.
What You'll Learn
- Creating tuples and accessing items
- Why immutability is useful
- Unpacking a tuple into variables
Creating Tuples
Tuples use parentheses. A single-item tuple needs a trailing comma:
Loading Python Playground...
Loading Python Exercise...
Immutable on Purpose
Trying to change a tuple raises an error. That is a feature: it protects data that should not change, and it lets tuples be used as dictionary keys (you will see this soon).
Loading Python Playground...
Unpacking
You can pull a tuple apart into separate variables in one line. This is clean and very common:
Loading Python Playground...
Loading Python Exercise...
Functions Return Tuples
When a function gives back more than one value, it is returning a tuple. Unpacking makes it readable:
Loading Python Playground...
Key Takeaways
- Tuples use parentheses:
(1, 2, 3)and cannot be changed - Access items with the same indexing as lists
- Unpack with
a, b = my_tuple - Use tuples for fixed data and for returning several values at once
Quiz
Question 1 of 425% Complete
0 of 4 questions answered

