Skip to the content

Unpacking

Python · Unit 8: Tuples and sets

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. 1. Predict the output

    What does this code print?

  2. 2. Find the bug

    This program should print Luke's name and class. Which line has the bug?

  3. 3. Complete the code

    Complete it to print each name with their points.

  4. 4. Predict the output

    Here someone tries to swap in two lines. What does it print?

  5. 5. Complete the code

    Complete it so Amy and Leo swap places.

  6. 6. Predict the output

    What does this code print?

  7. 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. 8. Predict the output

    What does this code print?

  9. 9. Multiple choice

    What does this code do?

  10. 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. 11. Predict the output

    This loop swaps with a comma. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →