Python Learning Series · @onlysamarrr · Day 17 / 30

Day 17: Dictionary Basics

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

Quick Answer

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.

What You Will Learn

Code

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))

Output

92
0
{'Arhan': 92, 'Riya': 88}
Arhan 92
Riya 88
['Arhan', 'Riya']

Code Explained, Line by Line

  1. marks = {"Arhan": 92} creates a dict inside curly brackets, in key: value pairs.
  2. marks["Arhan"] returns 92 by looking up the key.
  3. marks.get("Aman", 0) returns 0 when the key is missing, instead of raising KeyError.
  4. update() adds a new key or changes the value of an existing key.
  5. .items() gives key and value together in a loop; list(marks) gives only the keys.
Common Mistake: Looking up a missing key directly, like marks["Aman"], raises KeyError. The safe way is .get().

Real-Life Use

Phonebooks, user profiles and JSON data from APIs are all dictionaries.

Practice Task: Make a student marks dict and a phonebook dict (name to number).

Test Yourself — MCQs

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?

Today's 5 Reels (from the 90-Day Plan Excel)

81/450ConceptKey-value pairs
82/450Live demoDict banao aur access karo
83/450Common mistakeKeyError se bacho
84/450QuizQuiz: student marks dict
85/450Interview / real lifeReal life: phonebook
← Day 16Day 18 →