← Python Crash Course

PCC·06 · Chapter record

Dictionaries

Chapter 6 — storing and connecting related information

Done

Five sessions on Python's other core data structure: building a dictionary, looping through it three different ways, checking membership, and nesting lists and dictionaries inside each other. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.

6.1

A Simple Dictionary

  • A dictionary is a collection of key-value pairs, wrapped in braces {} — each key connects to a value, separated by a colon.
  • Access a value with dict[key]; add or modify one with dict[key] = value — a dictionary can start empty ({}) and grow one pair at a time.
  • del dict[key] removes a key-value pair permanently.
  • Asking for a key that doesn't exist raises a KeyError.get(key, default) is the safe alternative, returning your default instead of crashing.
dictionary
a collection of key-value pairs, written with { }
key-value pair
a key connected to its value by a colon
KeyError
raised when you ask for a key that doesn't exist
.get(key, default)
safely reads a value, returning default if the key is missing
Activity 1

Store a person's first name, last name, age, and city in a dictionary, then print each piece of information.

Activity 2

Build a small glossary: use five programming terms as keys and their meanings as values, then print each term and meaning.

▶ Alien Dictionary Builder