Python Dictionaries & Key-Value Stores
A dictionary is like a real dictionary or phone book: instead of looking up an item by a number index (like in a list), you look up a value by its key label. Defined with curly braces and key: value pairs: user = {"name": "Alex", "level": 10}.
"Think of a contact card on your smartphone. The keys are "Name", "Phone", "Email", and the values are the actual contact details. You look up a contact by their name, not by "contact #42"."
Deep Dive: How It Works
Dictionaries store data in key: value pairs separated by commas inside curly braces {}.
Accessing values: Use square brackets with the key name: user["name"], or safely with user.get("name", "Default").
Keys must be unique and immutable (strings and integers make great keys). Values can be anything, including lists and nested dictionaries.
Modifying dictionaries: Add or update keys simply by assigning: user["level"] = 11. Delete keys with del user["key"] or user.pop("key").
Syntax Blueprint
user = {"name": "Alex", "role": "Admin"}
name = user["name"] # Access by key ("Alex")
user["xp"] = 500 # Add new key
role = user.get("role") # Safe accessKeys and values are paired with colons : and separated by commas.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
KeyError when accessing a non-existent key: user["email"]Why it happens: If the key does not exist in the dictionary, bracket notation crashes with a KeyError.
How to fix: Use user.get("email", "Not provided") to return a safe fallback value instead of crashing.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Create a dictionary and access a key
Create a dictionary server with keys "host": "localhost" and "port": 8080.
Print the value of server["port"].
Finished reading and practicing?
Mark this lesson as completed to update your course progress.