Unpack Student Records
Advanced · Write the code · from Python Unpacking: Starred Expressions, Swapping, and Destructuring
From the lesson: Python Unpacking: Starred Expressions, Swapping, and Destructuring
Imagine you order a package online that contains three items: a book, headphones, and a phone case. When the box arrives, you open it and put each item where it belongs. The book goes on the shelf, headphones go on the desk, and the case goes on your phone.
Read the lessonYou have a list of student records. Each record is a tuple of (name, (math_score, english_score)). Use nested unpacking in a for loop to calculate and print each student's average score.
Print each line in the format: Name: average where average is rounded to 1 decimal place using :.1f.
students = [
('Alice', (90, 80)),
('Bob', (70, 90)),
('Charlie', (85, 95)),
]
# Loop with nested unpacking and print averages
More Unpacking exercises
- Unpack a CoordinateBeginner
- Swap Two VariablesBeginner
- Head and Tail with StarIntermediate
- Predict the Unpacking OutputIntermediate
All Python exercises · 50 practice problems · Data Structures quiz