Unpacking
Python · Unit 8: Tuples and sets
- Full plan
- Python
- 11 exercises
With for name, grade in grades.items(): you already unpacked: each round a tuple arrives and Python splits its values among the names.
It works in any assignment: one name per value, in the same order.
student = ("Amy", 15)
name, age = student
print(name)
print(age)Prints
Amy 15
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Find the bug
This program should print Luke's name and class. Which line has the bug?
3. Complete the code
Complete it to print each name with their points.
4. Predict the output
Here someone tries to swap in two lines. What does it print?
5. Complete the code
Complete it so Amy and Leo swap places.
6. Predict the output
What does this code print?
7. Put the lines in order
Put in order the function that returns the total and the average of a list, in that order.
8. Predict the output
What does this code print?
9. Multiple choice
What does this code do?
10. Find the case that fails
extremes(nums)should return the smallest and the largest number in the list, or(0, 0)if it's empty. Which call makes it fail?11. Predict the output
This loop swaps with a comma. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.