What is a Tuple?
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A tuple is an ordered group of values that cannot be changed after it is made. Use it for a small, fixed record, such as a product or a point on a map.
Making a tuple
Tuple is an ordered group of values written in round brackets, such as (4, 9). Items keep their position, can be of any type, and cannot be changed after the tuple is created.
A fixed record, such as a product with a name, a price and a stock count, needs a safe home. A tuple keeps values in order, written inside round brackets.
product = ("Keyboard", 49.5, 3)print(product[0])print(product[-1])print(len(product))single = (7,)print(type(single))print(type((7)))Example 1 · Storing a customer record
customer = ("Asha", "Pune", 29)print(customer[0])print(len(customer))print(customer)Asha
3
('Asha', 'Pune', 29)What this code does
customer[0]reads the first item, the name.len(customer)counts the items.print(customer)shows the whole tuple in round brackets.
Indexes work as in a list: 0 is the first item and -1 is the last. A one-item tuple needs a trailing comma. (7,) is a tuple, but (7) is just the number 7 in brackets.
Immutable: no changes in place
Immutable means that an object cannot be changed after it is created. Tuples, strings and numbers are immutable. Lists are mutable.
A list can be edited in place. A tuple cannot, so a record cannot change by accident. To get a different tuple, build a new one.
sizes = (38, 40, 42)bigger = sizes + (44,)print(sizes)print(bigger)as_list = list(sizes)as_list[0] = 36print(tuple(as_list))Example 2 · Building a new tuple
prices = (100, 200)with_extra = prices + (300,)print(prices)print(with_extra)(100, 200) (100, 200, 300)
What this code does
+joins two tuples into a new one.- The old tuple
pricesis unchanged, because tuples cannot change.
sizes + (44,) makes a new tuple and leaves sizes alone. Writing sizes[0] = 36 would raise a TypeError, because a tuple does not allow item assignment.
Reading a tuple
Every list tool that only reads works on tuples too: loops, in, slices, count and index. Only the methods that change items, such as append, are missing.
marks = (72, 85, 72, 90)for m in marks: print(m)print(85 in marks)print(marks.count(72))print(marks.index(90))print(marks[1:3])Example 3 · Counting repeated marks
marks = (72, 85, 72, 90, 72)print(marks.count(72))print(marks.index(85))print(90 in marks)3 1 True
What this code does
count(72)finds three 72s.index(85)is 1, the position of the first 85.inchecks if a value is inside the tuple.
count(72) counts how often 72 appears. index(90) gives the position of the first 90. A slice of a tuple is a new tuple.
A tuple that holds a list
Immutable has a limit. A tuple fixes which objects it holds, not what happens inside them.
order = ("A-101", ["pen", "book"])order[1].append("bag")print(order)The tuple still points to the same list, but that list was changed. The slots of a tuple cannot be replaced, yet a mutable object inside can still change.
Next step
Check your understanding
10 quick questions