![]() |
|
CHAPTER 13 — Dictionaries: Storing Information With Keys - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 13 — Dictionaries: Storing Information With Keys (/showthread.php?tid=231) |
CHAPTER 13 — Dictionaries: Storing Information With Keys - Leejohnston - 11-15-2025 Chapter 13 — Dictionaries: Storing Information With Keys Dictionaries let you store information in a structured, searchable way — just like a mini database. A dictionary stores values using keys, not indexes. Example: Code: person = {Keys are like labels. You don’t search by number — you search by name. --- 13.1 Creating a Dictionary Code: car = {--- 13.2 Accessing Values Use square brackets: Code: print(car["brand"]) # TeslaOr safer: Code: print(car.get("year"))--- 13.3 Adding New Key–Value Pairs Code: car["colour"] = "red"--- 13.4 Changing Values Code: car["year"] = 2024--- 13.5 Removing Items Remove a specific key: Code: car.pop("model")Remove the last added item: Code: car.popitem()--- 13.6 Checking if a Key Exists Code: if "brand" in car:--- 13.7 Looping Through Dictionaries Loop through keys: Code: for key in car:Loop through values: Code: for value in car.values():Loop through both: Code: for key, value in car.items():--- 13.8 Nested Dictionaries Dictionaries can contain other dictionaries: Code: students = {Access: Code: print(students["001"]["score"])--- 13.9 Dictionary vs List — When to Use Which Use a list when: • order matters • you just store items • you want easy loops Use a dictionary when: • you need labelled data • you search by name/key • you want structured info --- 13.10 Real-World Example — User Profiles Code: profile = {This is why dictionaries are everywhere online. --- 13.11 Mini Project — Contact Book Create a dictionary storing: • name • phone number Then print a formatted contact card. Example: Code: Name: Mia--- 13.12 Challenge — Student Grades Manager Ask user for: • student name • 3 test scores Store in a dictionary like: Code: {Then print: • name • each score • final average BONUS: Mark the grade: • A (≥ 90) • B (≥ 80) • C (≥ 70) • D (≥ 60) • F (< 60) --- 13.13 Chapter Summary • dictionaries store data using keys • access values with dictionary["key"] • .get(), .keys(), .values(), .items() are essential • dictionaries can contain lists and other dictionaries • use dictionaries for structured, labelled data Next: Chapter 14 — Functions: Reusable Code Blocks This is where your programs become modular, organised, and professional. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |