🐍 Python for kids · Class 5 · Lesson 11 of 18 · Free
Dictionaries – Keys and Values
⏱ About 25 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Make a dictionary and read a value using its key.
- Add new pairs and change old values.
- Loop over keys and values with items() and use get() safely.
In a list, you find things by number, like item 0 or item 3. But a canteen menu works by name: "How much is a samosa?" A dictionary lets Python look things up by name.
What is a dictionary in Python?
A dictionary stores pairs. Each pair has a key and a value, joined by a colon.
Think of your school canteen board. The food name is the key, and the price is the value.
A dictionary uses curly brackets { }. Commas separate the pairs.
{'samosa': 15, 'idli': 20, 'juice': 25}
Items on the menu: 3
💡 len() counts the pairs, not the separate keys and values.
How do I get a value from a dictionary?
Put the key inside square brackets, like menu["idli"]. Python gives back its value.
With a list you use an index number. With a dictionary you use the key, which is easier to remember.
Keys must match exactly. "Idli" with a capital I is a different key from "idli".
▶marks = {"Maths": 88, "Science": 76, "English": 91}2maths = marks["Maths"]3print("Maths marks:", maths)4subject = input("Which subject? ")5print(f"You scored {marks[subject]} in {subject}.")
No variables yet.
▌✏️ Change this code and run it yourself
Maths marks: 88 Which subject? English You scored 91 in English.
How do I add or change items in a dictionary?
To add a new pair, write dict[new_key] = value. Python makes the new pair for you.
If the key is already there, the same line changes its value instead. A key can appear only once.
Below, the canteen adds dosa and then raises the price of juice.
▶menu = {"samosa": 15, "juice": 25}2menu["dosa"] = 403menu["juice"] = 304print(menu)5menu["samosa"] = menu["samosa"] + 56print("New samosa price:", menu["samosa"])
No variables yet.
▌✏️ Change this code and run it yourself
{'samosa': 15, 'juice': 30, 'dosa': 40}
New samosa price: 20
💡 The fifth line reads the old price and adds 5. Then it stores the new price back.
How do I safely look up a key with get()?
If you ask for a key that is missing, Python stops with a KeyError. That can crash your program.
get() is the polite way. It returns None if the key is missing, instead of an error.
You can also give get() a second value. That value comes back when the key is missing.
12 None 0 Yes, we have erasers.
💡 in checks the keys of a dictionary, just like it checked the items of a list.
How do I loop over a dictionary?
A plain for loop over a dictionary gives you each key. You can then use the key to get its value.
items() is even better. It gives you the key and the value together, so you can name both.
keys() and values() give only the keys or only the values. sum() works on values() for totals.
▶runs = {"Rohit": 45, "Virat": 82, "Hardik": 30}2for player, score in runs.items():3 print(f"{player} scored {score} runs.")4print("Team total:", sum(runs.values()))
No variables yet.
▌✏️ Change this code and run it yourself
Rohit scored 45 runs. Virat scored 82 runs. Hardik scored 30 runs. Team total: 157
When should I use a dictionary instead of a list?
Use a list when order matters and items are all the same kind, like a row of marks.
Use a dictionary when each value has a name, like a student's details.
Here one dictionary holds a whole report card. Each detail has its own clear label.
name: Anaya class: 5 city: Pune house: Blue
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
ages = {"Riya": 10, "Sam": 11}
print(ages["Sam"])Question 2. What does this print?
price = {"pen": 10}
price["pen"] = 12
price["book"] = 50
print(len(price), price["pen"])Question 3. What does this print?
colours = {"sky": "blue", "grass": "green"}
print(colours.get("sun", "unknown"))🧩 Your challenge
Build a canteen bill. The menu has prices for samosa, idli and juice. Ask which item the student wants and how many, then print the bill. If the item is not on the menu, say so.
What would you like? samosa How many? 3 3 samosa = 45 rupees
⚠️ Common mistakes (and how to fix them)
❌ Wrong
marks = {"Maths": 88, "Science": 76}
print(marks["maths"])KeyError: 'maths'
✅ Right
marks = {"Maths": 88, "Science": 76}
print(marks["Maths"])Keys must match exactly, including capital letters. Use get() if a key might be missing.
❌ Wrong
menu = {"tea": 10, "coffee": 15}
print(menu[0])KeyError: 0
✅ Right
menu = {"tea": 10, "coffee": 15}
print(menu["tea"])A dictionary has no index numbers. You must look things up by key.
❌ Wrong
menu = {"tea" = 10, "coffee" = 15}
print(menu)SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
✅ Right
menu = {"tea": 10, "coffee": 15}
print(menu)Inside { }, a colon joins each key to its value. The = sign is only for variables.
📌 Remember
- A dictionary stores key: value pairs inside curly brackets { }.
- Read a value with dict[key]; the key must match exactly.
- dict[key] = value adds a new pair or changes an old one.
- get() returns None, or your default value, when a key is missing.
- for key, value in dict.items(): visits every pair.
- Use a list for a row of similar things, a dictionary for named things.
Finished the quiz and the challenge?
Questions students ask
What is the difference between a list and a dictionary in Python?
A list finds items by position number, starting at 0. A dictionary finds values by a key, like a name or word.
Can two keys in a dictionary be the same?
No. Each key appears only once. If you set the same key again, the old value is replaced.
What is a KeyError in Python?
It happens when you ask for a key that is not in the dictionary. Check with in first, or use get().
Can a dictionary value be a number and a word?
Yes. Values can be numbers, words, True/False, or even lists. Keys are usually words or numbers.