Students will understand that a dictionary is a data structure that maps keys to values, written as {key: value}. They will learn how it differs from lists—unordered and accessed by keys rather than by numeric indices.
In lists, we use indexes (0, 1, 2…) to access items. But what if we want to access data by name, such as finding a student’s score by their name or looking up a price by product name? That’s when we use a dictionary — a powerful Python structure that stores key–value pairs.
Syntax & Key Points:
Dictionary = {key: value, key2: value2, ...}
# Example:
student = {"name": "Alice", "age": 12, "grade": 6}
# Access:
print(student["name"]) → Alice
print(student["age"]) → 12
{} to define a dictionary.: and commas between pairs.student["school"] = "STEM Academy".KeyError; get() is a safer choice.get(key, default) returns a default value instead of crashing if the key is missing.Example A: Create and Access
# Create a dictionary
student = {"name": "Alice", "age": 12, "grade": 6}
# Access values
print(student["name"]) # Alice
print(student["age"]) # 12
# Add new key-value pair
student["school"] = "STEM Academy"
print(student)
Example B: Safe Access
student = {"name": "Bob", "age": 13}
print(student.get("grade"))
print(student.get("grade", "N/A"))
A dictionary maps keys to values and is defined with {}. Access values using their keys, and use get() for safe lookups. It helps organize structured data clearly and efficiently. Now that we can create and access dictionaries, let’s learn how to add, modify, and delete data — completing the full “CRUD” (Create, Read, Update, Delete) cycle.