Beginner · Lists, tuples, dictionaries and sets · topic 15 of 50 · 15 min
Tuples: fixed groups of values
Explanation
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.
ticket = ("12951", "Mumbai", "Delhi")
print(ticket[1])
print(len(ticket))
for part in ticket:
print(part)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.
student = "Asha", 88
print(student)
name, marks = student
print(name)
print(marks)Returning two values
def min_max(numbers):
return min(numbers), max(numbers)
low, high = min_max([45, 90, 72])
print("Lowest:", low, "Highest:", high)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.
results = [("Asha", 88), ("Ravi", 72), ("Meena", 95)]
for name, marks in results:
print(name, "scored", 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.
ticket = ("12951", "Mumbai", "Delhi")
ticket[2] = "Pune"a = (5)
b = (5,)
print(type(a))
print(type(b))Watch the lesson
Tuples: fixed groups of values · 1/7
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.
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)Practice (0/3 done)
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).
Write a function min_max(numbers) that returns a tuple (smallest, largest). Example: min_max([4, 9, 1, 7]) returns (1, 9).
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
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.