Creating and reading
Python · Unit 7: Dictionaries
- Full plan
- Python
- 10 exercises
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. Multiple choice
Which line creates a dictionary with Amy's age and Leo's age?
2. Predict the output
What does this code print?
3. Complete the code
Complete it to print the price of the tea.
4. Find the bug
This program should print Emma's grade. Which line has the bug?
5. Predict the output
What does this code print?
6. Predict the output
What does this code print?
7. Complete the code
Complete it so
pricereturns 0 when the item isn't in the dictionary.8. Multiple choice
What does this code do?
9. Put the lines in order
Put in order the function that charges for
npieces of an item. If the item has no price, it costs 0.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?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.