๐
STUDENT NOTES ยท DAY 11 OF 24
The Magic Register ๐
A list numbers its wagons. A dictionary NAMES them โ like the class register: name on the left, marks on the right.
1Keys and values
๐
Py: A dictionary stores pairs: key โ value. Ask by NAME, get the answer instantly โ no counting wagons.
2Add, update, remove
๐ค
Bittu: Assigning to a NEW key adds it. Assigning to an OLD key replaces it. I never keep two entries with the same key.
3Bugly's trap: the missing key
๐
Bugly: Ask for someone who is not in the registerโฆ hehehe:
The polite fix โ .get() with a backup answer:
Register lookup โ try a name!
Renu โ 78
register = {"Renu": 78, "Akash": 92, "Priya": 65}
4Looping through the register
๐ฆ
Professor Hoot: .items() hands you both halves of each pair. The most useful dict loop there is.
5Quick quiz! ๐ง
d = {"a": 1}. What does d["z"] do?
๐ก Square brackets on a missing key crash with KeyError. Use d.get("z", "backup") to stay safe.
Which loop gives you keys AND values?
๐ก .items() yields (key, value) pairs โ unpack them into two names.
6Your mission ๐
Mission 1: Make a menu dict: {"samosa": 15, "chai": 10, "momo": 40}. Print the price of momos.
Mission 2: Add "dosa": 50, then print the whole menu with a .items() loop like "samosa costs โน15".
Mission 3: Use .get() to ask for "pizza" with the backup answer "not on the menu!".
Day 11 done โ registrar of power! ๐
Next: tuples that never change and sets that hate duplicates.
Go to Day 12: Tuples & Sets โ