Python Basics — Tuples: Practice Exercises
Tuples — Practice Exercises
Test your understanding of tuple creation, unpacking, named tuples, and tuple vs list usage.
Exercise 1 — Tuple Operations
Given t = (10, 20, 30, 40, 50, 20, 30, 20), write code to:
- Count how many times
20appears - Find the index of first
30 - Get the last 3 elements as a new tuple
- Check if
60is in the tuple
Show Solution
t = (10, 20, 30, 40, 50, 20, 30, 20)
# 1. Count occurrences of 20
print(t.count(20)) # 3
# 2. Index of first 30
print(t.index(30)) # 2
# 3. Last 3 elements
print(t[-3:]) # (20, 30, 20)
# 4. Check membership
print(60 in t) # False
Exercise 2 — Unpacking Practice
Write a function head_tail(lst) that takes a list and returns the first element and a list of the remaining elements using star unpacking.
# Expected:
head_tail([1, 2, 3, 4, 5]) # → (1, [2, 3, 4, 5])
head_tail([10]) # → (10, [])
Show Solution
def head_tail(lst):
first, *rest = lst
return first, rest
print(head_tail([1, 2, 3, 4, 5])) # (1, [2, 3, 4, 5])
print(head_tail([10])) # (10, [])
print(head_tail(["a", "b", "c"])) # ('a', ['b', 'c'])
Exercise 3 — Named Tuple Student Record
Create a named tuple Student with fields name, age, and gpa. Then:
- Create 3 students
- Find the student with the highest GPA
- Convert the top student to a dictionary
Show Solution
from collections import namedtuple
Student = namedtuple("Student", "name age gpa")
students = [
Student("Alice", 20, 3.9),
Student("Bob", 22, 3.7),
Student("Charlie", 21, 3.95),
]
# Find highest GPA
top = max(students, key=lambda s: s.gpa)
print(top) # Student(name='Charlie', age=21, gpa=3.95)
# Convert to dict
print(top._asdict())
# {'name': 'Charlie', 'age': 21, 'gpa': 3.95}
Exercise 4 — Swap Multiple Variables
Without using any temporary variables, swap the values of three variables so that a gets b’s value, b gets c’s value, and c gets a’s value.
a, b, c = 1, 2, 3
# After swap: a=2, b=3, c=1
Show Solution
a, b, c = 1, 2, 3
a, b, c = b, c, a
print(a) # 2
print(b) # 3
print(c) # 1
This works because Python evaluates the entire right side (b, c, a) as a tuple first, then unpacks it.
Exercise 5 — Tuple as Dictionary Key
Create a distance lookup table using (city1, city2) tuples as keys. Then write a function that returns the distance between any two cities regardless of the order they’re given.
Show Solution
distances = {
("New York", "London"): 5570,
("New York", "Tokyo"): 10838,
("London", "Tokyo"): 9566,
("Paris", "London"): 344,
}
def get_distance(city1, city2):
# Check both orderings
if (city1, city2) in distances:
return distances[(city1, city2)]
elif (city2, city1) in distances:
return distances[(city2, city1)]
else:
return None
print(get_distance("New York", "London")) # 5570
print(get_distance("London", "New York")) # 5570 (reverse order works)
print(get_distance("Tokyo", "London")) # 9566
print(get_distance("Paris", "Tokyo")) # None (not in table)
Scoring Guide
| Score | Level |
|---|---|
| 5/5 | Excellent — tuples mastered |
| 3-4/5 | Good — review unpacking and named tuples |
| 1-2/5 | Keep practicing — re-read the chapter |
Next chapter: Dictionaries →