Module 12 of 15 · Step 1 of 12

Dictionaries

A dictionary keeps related pieces of information under named keys. A student's name and age can be stored together and read by their labels instead of positions.

By the end of this lesson, you can:
  • Create key/value pairs
  • Read and update a value
  • Add a new key
  • Check keys and loop through pairs
Dictionaries lesson progress0%

Course progress: 0% · 0 of 12 steps completed in this module

Learn · Pair keys with values
Concept 1 of 5

Pair keys with values

Inside braces, each key is followed by a colon and a value. Keys such as 'name' describe what a value means. This is different from a list, where positions are numbers.

Pattern
student = {"name": "Adam", "age": 18}
Code
student = {"name": "Adam", "age": 18}
print(student["name"])
Output
Adam

Why it works

  1. name and age are keys.
  2. Adam and 18 are their values.
  3. Square brackets with a key read one value.

Common mistake

Not this
student[0]  # expecting name
Try this
student["name"]

A dictionary is read by key, not list position.