Tuples
Name the parts of a small fixed record and unpack a variable middle section.
On this page
The concept
A tuple is ordered, but its slots cannot be reassigned. It is useful for a small group of related values, such as a coordinate. The comma is decisive: (10) is just an integer in parentheses, while (10,) is a one-item tuple.
Unpacking binds items to several names at once. A starred target collects the remaining items into a list. Assignment evaluates the right-hand side before updating targets, which makes a, b = b, a a safe swap. Tuple immutability does not freeze a mutable object stored inside one of its slots.
Worked example
point = (10, 20)
x, y = point
print(x, y)
single = (10,)
print(type(single))
first, *middle, last = (1, 2, 3, 4, 5)
print(first, middle, last)
a, b = (3, 7)
a, b = (b, a)
print(a, b)Trace the result
- x and y receive the two coordinate values.
- The trailing comma creates a one-item tuple.
- The starred middle target receives 2, 3, and 4; the final assignment swaps the current bindings of a and b.
10 20
<class 'tuple'>
1 [2, 3, 4] 5
7 3Line-by-line walkthrough
Line 1: point = (10, 20)
Evaluate (10, 20), then bind or update point with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 2: x, y = point
Evaluate point, then bind or update (x, y) with the result. Assignment itself does not imply a deep copy.
Line 3: print(x, y)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 4: single = (10,)
Evaluate (10,), then bind or update single with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 5: print(type(single))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 6: first, *middle, last = (1, 2, 3, 4, 5)
Evaluate (1, 2, 3, 4, 5), then bind or update (first, *middle, last) with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 7: print(first, middle, last)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 8: a, b = (3, 7)
Evaluate (3, 7), then bind or update (a, b) with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 9: a, b = (b, a)
Evaluate (b, a), then bind or update (a, b) with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 10: print(a, b)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Ordinary unpacking requires matching lengths. Too many or too few values raise ValueError unless a starred target accounts for the remainder.