11-15-2025, 10:36 PM
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:
Keys are like labels.
You don’t search by number — you search by name.
---
13.1 Creating a Dictionary
---
13.2 Accessing Values
Use square brackets:
Or safer:
---
13.3 Adding New Key–Value Pairs
---
13.4 Changing Values
---
13.5 Removing Items
Remove a specific key:
Remove the last added item:
---
13.6 Checking if a Key Exists
---
13.7 Looping Through Dictionaries
Loop through keys:
Loop through values:
Loop through both:
---
13.8 Nested Dictionaries
Dictionaries can contain other dictionaries:
Access:
---
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
This is why dictionaries are everywhere online.
---
13.11 Mini Project — Contact Book
Create a dictionary storing:
• name
• phone number
• email
Then print a formatted contact card.
Example:
---
13.12 Challenge — Student Grades Manager
Ask user for:
• student name
• 3 test scores
Store in a dictionary like:
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
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 = {
"name": "Mia",
"age": 14,
"city": "Liverpool"
}Keys are like labels.
You don’t search by number — you search by name.
---
13.1 Creating a Dictionary
Code:
car = {
"brand": "Tesla",
"model": "Model 3",
"year": 2020
}---
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:
print("Brand found!")---
13.7 Looping Through Dictionaries
Loop through keys:
Code:
for key in car:
print(key)Loop through values:
Code:
for value in car.values():
print(value)Loop through both:
Code:
for key, value in car.items():
print(key, value)---
13.8 Nested Dictionaries
Dictionaries can contain other dictionaries:
Code:
students = {
"001": {"name": "Sam", "score": 88},
"002": {"name": "Ella", "score": 93}
}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 = {
"username": "astro_john",
"posts": 120,
"reputation": 87,
"member_since": "2024-01-12"
}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
Phone: 07700 123456
Email: mia@example.com---
13.12 Challenge — Student Grades Manager
Ask user for:
• student name
• 3 test scores
Store in a dictionary like:
Code:
{
"name": "Ella",
"scores": [82, 91, 88],
"average": 87
}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
