Dictionaries
Dictionaries
Sometimes data belongs in pairs.
For example:
- A name and phone number
- A product and price
- A country and capital
- A username and password
Python gives us dictionaries for this.
What Is a Dictionary?
A dictionary stores data as key: value pairs.
Think of a real dictionary:
- Word = key
- Meaning = value
In Python:
"name"= key"Tom"= value
Dictionaries use curly brackets {}.
user = {
"name": "Tom",
"age": 25
}
print(user)
Output
{'name': 'Tom', 'age': 25}
Why Dictionaries Matter
Dictionaries help you:
- Store related data clearly
- Find values by name
- Update values easily
- Add new data
- Remove data quickly
Creating a Dictionary
student = {
"name": "Sara",
"score": 90,
"passed": True
}
Reading Values
Use the key name.
student = {
"name": "Sara",
"score": 90
}
print(student["name"])
print(student["score"])
Output
Sara
90
Using get()
get() is a safe way to read a key.
print(student.get("name"))
print(student.get("grade"))
Output
Sara
None
If the key does not exist, get() returns None.
Updating Values
student["score"] = 95
print(student)
Output
{'name': 'Sara', 'score': 95}
Adding New Items
Use a new key.
student["city"] = "Toronto"
print(student)
Output
{'name': 'Sara', 'score': 95, 'city': 'Toronto'}
Removing Items with pop()
student.pop("city")
print(student)
Output
{'name': 'Sara', 'score': 95}
Removing Last Item with popitem()
student.popitem()
print(student)
Output
{'name': 'Sara'}
Clearing All Items
student.clear()
print(student)
Output
{}
Dictionary Length
Use len().
data = {"a": 1, "b": 2, "c": 3}
print(len(data))
Output
3
Loop Through Keys
user = {"name": "Tom", "age": 25}
for key in user:
print(key)
Output
name
age
Loop Through Values
for value in user.values():
print(value)
Output
Tom
25
Loop Through Keys and Values
for key, value in user.items():
print(key, value)
Output
name Tom
age 25
Code Along
product = {
"name": "Laptop",
"price": 900
}
product["stock"] = 5
for key, value in product.items():
print(key, value)
Output
name Laptop
price 900
stock 5
Mini Challenge
Build a profile dictionary.
Steps:
- Create a dictionary with:
name,age,city - Print the name
- Update the age
- Add
job - Remove
city - Print the final dictionary
Expected output:
Tom
{'name': 'Tom', 'age': 26, 'job': 'Designer'}
Build a student manager.
Steps:
-
Create a dictionary with:
name:Tomage:25city:New York
-
Print the value of
name -
Update
ageto26 -
Add a new key called
coursewith valuePython -
Print the value of
course -
Remove
city -
Print all keys and values using a loop
-
Print the final dictionary
Expected output:
Tom
Python
name Tom
age 26
course Python
{'name': 'Tom', 'age': 26, 'course': 'Python'}
Real World Use Case
Programs use dictionaries for user accounts, product data, settings, APIs, and storing records with labels.
Quiz
- What does a dictionary store?
- Which brackets are used for dictionaries?
- What does
get()do? - Which method loops through key and value pairs?
Assignment
Create a dictionary for a book with title, price, and author. Update one value and print all keys and values.
Summary
You learned that dictionaries store key-value pairs and how to create, read, update, delete, and loop through dictionary data.