Lesson 9 of 30

Lesson 09 — Dictionaries for tiny worlds

Title: Store named facts with dictionaries

Description: Use dictionaries for game characters, pets, backpacks, and simple profiles.

Why it matters for kids: Dictionaries help code remember facts by name, not just by position.


1. First dictionary

pythonpython
player = {
    "name": "Ava",
    "level": 1,
    "power": "speed",
}

print(player["name"])
print(player["level"])
print(player["power"])

A dictionary has keys and values.


2. Change a value

pythonpython
pet = {
    "name": "Mochi",
    "animal": "cat",
    "age": 2,
}

pet["age"] = 3

print(f"{pet['name']} is now {pet['age']} years old.")

3. Add a new fact

pythonpython
backpack = {
    "pencils": 3,
    "notebooks": 2,
}

backpack["stickers"] = 10

print(backpack)

4. Loop through keys and values

pythonpython
robot = {
    "name": "Beep",
    "color": "yellow",
    "battery": "full",
}

for key, value in robot.items():
    print(f"{key}: {value}")

5. Easy examples

pythonpython
monster = {
    "name": "Tiny Troll",
    "health": 30,
    "weakness": "sunlight",
}

print(f"{monster['name']} has {monster['health']} health.")
pythonpython
book = {
    "title": "Space Cats",
    "pages": 120,
}

print(f"I am reading {book['title']}.")
pythonpython
ice_cream = {
    "flavor": "strawberry",
    "scoops": 2,
    "has_sprinkles": True,
}

print(ice_cream)

6. Mini challenge

Click each example to open it.

Example 1

Create a dictionary for one player.

Example 2

Print the player name.

Example 3

Print the player level.

Example 4

Change the player score.

Example 5

Add a new key named power.

Example 6

Loop through dictionary items.

Example 7

Create a pet dictionary.

Example 8

Create a backpack dictionary.

Example 9

Print one sentence from dictionary values.

Example 10

Create a superhero dictionary.