A tuple keeps a fixed group of related values together.
You already know lists, indexes, and list operations. A list is a collection whose contents can change; a tuple is a fixed group of values that belong together. Use a list for changing data, such as colors = ["red", "green", "blue"]. Use a tuple for one stable whole, such as point = (10, 20).
coordinates = (45.26, 19.83)
print(coordinates[0])
print(coordinates[1])A tuple usually uses comma-separated values, often inside (). Its values are ordered items with familiar positions: index 0 selects the first item and index 1 selects the second. Indexing works just as it did for lists.
45.26
19.83The important difference is that a tuple is immutable: you can read, index, and unpack it, but you cannot replace an existing item. This makes tuples useful for stable groups such as coordinates, a date value, or an RGB color.
Choose by intent: Use a list when items need to be added, removed, or replaced. Use a tuple when values form one fixed group, their positions have meaning, and you do not intend to replace individual items.
coordinates = (45.26, 19.83)
latitude, longitude = coordinates
print(latitude)
print(longitude)Python matches tuple values to variables by position: the first value goes to latitude and the second goes to longitude. The number of values and variables must match, so x, y = point works for point = (10, 20), while x, y, z = point does not.
coordinates = (45.26, 19.83)
coordinates[0] = 50This raises TypeError. The tuple can be read, indexed, and unpacked, but its existing items cannot be replaced with assignment.
point = (10, 20)
x, y = point
x = 99
print(point)
print(x)x first receives the value 10. After x = 99, the name x refers to 99; it does not reach back and change point. The tuple is still (10, 20), just as assignment in the Variables lesson changes what a name refers to.
not_a_tuple = (5)
one_item_tuple = (5,)The comma creates the one-item tuple. (5) is only the value 5 inside parentheses, while (5,) is a tuple containing one item.
point = (4, 7)
print(point[0])
x, y = point
x = 10
print(x)
print(y)
print(point)Pause before running it. point[0] is 4; unpacking gives x the value 4 and y the value 7. Then x = 10 changes only that variable. The final output is 4, 10, 7, and (4, 7).
Common beginner mistakes
- Treating a tuple like a mutable list
point[0] = 5raisesTypeErrorbecause an existing tuple item cannot be replaced. - Thinking parentheses alone make a one-item tuple
(5)is the value5;(5,)is a one-item tuple. - Unpacking into the wrong number of variablesA two-value tuple needs two receiving variables. Python cannot match
x, y, zto(10, 20). - Thinking an unpacked variable changes the tupleChanging
xafterx, y = pointchanges the namex, not the original tuple.
