Skip to the content

Creating and reading

Python · Unit 7: Dictionaries

A dictionary stores data by name: each key points to its value. It goes inside curly braces {}, with key: value pairs separated by commas.

To read a value, put its key in square brackets. When you print the whole thing, Python uses single quotes, just like with lists.

prices = {"rice": 35, "tea": 60}
print(prices["rice"])
print(prices)

Prints

35
{'rice': 35, 'tea': 60}

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. Multiple choice

    Which line creates a dictionary with Amy's age and Leo's age?

  2. 2. Predict the output

    What does this code print?

  3. 3. Complete the code

    Complete it to print the price of the tea.

  4. 4. Find the bug

    This program should print Emma's grade. Which line has the bug?

  5. 5. Predict the output

    What does this code print?

  6. 6. Predict the output

    What does this code print?

  7. 7. Complete the code

    Complete it so price returns 0 when the item isn't in the dictionary.

  8. 8. Multiple choice

    What does this code do?

  9. 9. Put the lines in order

    Put in order the function that charges for n pieces of an item. If the item has no price, it costs 0.

  10. 10. Find the case that fails

    nickname(name) should return the person's nickname, or the same name if they don't have one. 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 →