Dictionaries · About 10 minutes

Build a character card

The big little idea

A dictionary stores pairs of keys and values. You can look up a value by its key instead of remembering its position. It is perfect for a game character’s information.

hero = {"name": "Bit", "level": 1}
hero["level"] = 2
print(hero["name"])
print(hero["level"])

Use curly braces for a dictionary and a colon between each key and value. hero["level"] reads a value; hero["level"] = 2 updates it. The key must exist when you look it up with square brackets.

Predict, then try: read the example from top to bottom. What do you think it will print? Copy it into the editor below to test your prediction.

Your turn, code explorer.

Give Bit 10 more energy points. Print the new energy value: 60.

60
my_adventure.pyPython · in your browser

Edit the code. Then run it. Ctrl / ⌘ + Enter also works.

OutputYOUR IDEAS, ALIVE
Your program’s output will appear here.

Ready when you are. ✦

The first run loads Python. This can take a moment.

Program input For programs that use input()

Add answers before you run. Each input() reads the next line. Use made-up names and details.

Real Python, powered by Pyodide. Standard-library programs work here; desktop windows, turtle graphics, and package installation are not supported by this editor. Programs stop after 10 seconds of execution.

Back to the starting code?

This replaces your edits for this activity. Download your code first if you want to keep it.

A little hint, please

Use hero["energy"] = hero["energy"] + 10 before printing.

Show one possible solution

There can be more than one way. Read this version, then try writing it yourself.

hero = {"name": "Bit", "energy": 50}
hero["energy"] = hero["energy"] + 10
print(hero["energy"])
Pack this idea for later: Dictionaries organize related information using meaningful labels.