Skip to content

Beginner · Lists, tuples, dictionaries and sets · topic 15 of 50 · 15 min

Tuples: fixed groups of values

Explanation

A tuple is like a list that cannot be changed after it is made. You write it with round brackets, like ("Asha", 88). Tuples are good for small fixed records, and you can unpack them into separate variables in one line. A one-item tuple needs a comma: (5,).

A tuple: a list that cannot change

A tuple is an ordered collection, like a list, but it cannot be changed after it is made. We say it is immutable, which means "cannot be changed". You write a tuple with round brackets ( ) and commas. Indexing, slicing, len and for loops work just like with lists.

python
ticket = ("12951", "Mumbai", "Delhi")
print(ticket[1])
print(len(ticket))
for part in ticket:
    print(part)
Shows Mumbai, then 3, then each part on its own line.
Think of it like this: A tuple is like a printed train ticket. The train number, the from station and the to station are fixed once it is printed. You can read every detail, but you cannot rub one out and write a new one. A list is more like a shopping list on a whiteboard that you keep changing.

Packing and unpacking

Packing means putting several values into one tuple. When you write values with commas, Python packs them into a tuple even without brackets. Unpacking means taking them out again into separate variables in one line. The number of variables must match the number of items.

python
student = "Asha", 88
print(student)
name, marks = student
print(name)
print(marks)
The first line packs the tuple ('Asha', 88). The third line unpacks it into name and marks.

Returning two values

python
def min_max(numbers):
    return min(numbers), max(numbers)

low, high = min_max([45, 90, 72])
print("Lowest:", low, "Highest:", high)
A function can return a tuple, and you unpack it straight away. Shows Lowest: 45 Highest: 90. You will learn functions fully in the next module.

Tuples as records

A record is a group of facts about one thing, like a student's name and marks. Tuples are good for records because the facts belong together and should stay together. A list of tuples makes a simple table.

python
results = [("Asha", 88), ("Ravi", 72), ("Meena", 95)]
for name, marks in results:
    print(name, "scored", marks)
The for loop unpacks each tuple into name and marks.

Why "cannot change" is useful

If a group of values should never change, a tuple protects it: no other part of the program can change it by mistake. Tuples can also be used as dictionary keys (you will see dictionaries in the next lesson), while lists cannot.

output
ticket = ("12951", "Mumbai", "Delhi")
ticket[2] = "Pune"
Running this gives TypeError: 'tuple' object does not support item assignment.
Careful: A tuple with one item needs a comma. (5,) is a tuple, but (5) is just the number 5 inside brackets. Python needs the comma to know that you mean a tuple.
python
a = (5)
b = (5,)
print(type(a))
print(type(b))
Shows <class 'int'> and then <class 'tuple'>.
Tip: Use a list when the items will change, like a cart or a queue. Use a tuple when the group is fixed, like a date, a point on a map, or a name with its marks.

Watch the lesson

Tuples: fixed groups of values · 1/7

Tuples

In this lesson you will learn about tuples, groups of values that stay fixed.

Prefer a video by a teacher? Find free videos on “python tuples packing unpacking tutorial for beginners” on YouTube ↗

In real life: Cricket scorecard records

A scorer keeps each batter's innings as a record: name, runs and balls faced. These facts never change after the innings, so tuples fit well. Unpacking each tuple in a loop makes it easy to work out the strike rate, which is runs per 100 balls.

python
innings = [("Rohit", 45, 30), ("Virat", 82, 60), ("Hardik", 20, 8)]
for name, runs, balls in innings:
    strike_rate = round(runs * 100 / balls, 1)
    print(name, runs, "runs, strike rate", strike_rate)
Shows the strike rate of each batter: 150.0, 136.7 and 250.0.

Practice (0/3 done)

Task 1 · Code · Python

Write a function make_record(name, marks) that returns a TUPLE with the name first and the marks second. Example: make_record("Asha", 88) returns ("Asha", 88).

Task 2 · Code · Python

Write a function min_max(numbers) that returns a tuple (smallest, largest). Example: min_max([4, 9, 1, 7]) returns (1, 9).

Task 3 · Code · Python

records is a list of (name, marks) tuples. Write a function best_student(records) that returns the name of the student with the highest marks. If two students tie, return the one who comes first.

Quick quiz

Question 1

1.Which of these is a tuple?

Question 2

2.What happens when you run t = (1, 2) and then t[0] = 5?

Question 3

3.After a, b = (3, 8), what is b?

Question 4

4.What is the type of (7,)?

Question 5

5.When is a tuple a better choice than a list?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: List methods and slicing