Python Learning Series · @onlysamarrr · Day 17 / 30
Day 17 teaches dictionaries. A dictionary stores key-value pairs: you look up a value by its key. The .get() method returns a default value instead of an error when a key is missing.
Phase: Intermediate · Reels: 81/450 – 85/450
Day 17 teaches dictionaries. A dictionary stores key-value pairs: you look up a value by its key. The .get() method returns a default value instead of an error when a key is missing.
marks = {"Arhan": 92, "Riya": 85}
print(marks["Arhan"])
print(marks.get("Aman", 0))
marks.update({"Riya": 88})
print(marks)
for name, m in marks.items():
print(name, m)
print(list(marks))
92
0
{'Arhan': 92, 'Riya': 88}
Arhan 92
Riya 88
['Arhan', 'Riya']
Phonebooks, user profiles and JSON data from APIs are all dictionaries.
Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.
Q1. What does {"a": 1}.get("b", 0) return?
Q2. What does {"a": 1}["b"] do?
Q3. What does list({"a": 1, "b": 2}) give?
| 81/450 | Concept | Key-value pairs |
| 82/450 | Live demo | Dict banao aur access karo |
| 83/450 | Common mistake | KeyError se bacho |
| 84/450 | Quiz | Quiz: student marks dict |
| 85/450 | Interview / real life | Real life: phonebook |