Skip to the content

Looping

Python · Unit 7: Dictionaries

A for over a dictionary goes through its keys, in the order they were added.

With the key in hand, you read its value with [].

ages = {"Amy": 15, "Leo": 17}
for who in ages:
    print(who, ages[who])

Prints

Amy 15
Leo 17

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. Complete the code

    Complete it to print each name with its grade.

  3. 3. Predict the output

    What does this code print?

  4. 4. Multiple choice

    In for x in prices.values():, what is x on each round?

  5. 5. Predict the output

    What does this code print?

  6. 6. Find the bug

    This program should print each name with its mark. Which line has the bug?

  7. 7. Complete the code

    Complete it to print each day with its temperature.

  8. 8. Put the lines in order

    Put in order the function that returns the list of people who passed, with 6 or more.

  9. 9. Multiple choice

    What does this function do?

  10. 10. Find the case that fails

    average(grades) should return the average of the grades in the dictionary, or 0 if it's empty. Which call does it fail with?

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 →