Skip to content

Dictionaries (dict)

In a phone book you look up a number by name, not by row number. Python has a dictionary for exactly this: each key maps to a value.

Curly braces { } plus key: value pairs:

Inputs (for input())

One value per line — read in order by input().

Output
 

player["name"] — access by key, not by index: “the value of the name key in the player dictionary.”

Inputs (for input())

One value per line — read in order by input().

Output
 

Assigning to an existing key updates its value; assigning to a new key adds a pair. This is exactly how character data is stored in video games. 🎮

Mission

Player card

+10 XP

Build a player dictionary (with name and score keys) and print the info like Aysel — 120 points. Tip: inside an f-string, write the dictionary key with single quotes: {player['name']}.

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Score boost

+15 XP

Increase the player’s score by 50 (building on the old value!) and print the new score.

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Phone book

+15 XP

Find and print the number of the person the user asked about, from the dictionary. (The inputs include Aysel.)

Inputs (for input())

One value per line — read in order by input().

Output
 

🏅 You finished this module! Lists and dictionaries — a program’s memory store. In the next module we tidy up the code: you’ll write your own functions. Continue →