Python Basics — Day 10 Dictionaries in Python
Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey
Building skills in Python and full-stack development, with a focus on web apps and system design
Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering
From Python basics to full-stack projects, sharing my progress as I grow into a developer
Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development
01. What is a Dictionary?
A dictionary is a data structure that stores data as Key:Value pairs.
Defined using curly braces
{}.
student = {
"name": "Sabin",
"age": 35,
"major": "Computer Science"
}
👉 Key Points:
Keys must be immutable (e.g., strings, numbers, tuples).
Values can be of any data type.
02. Accessing Values
print(student["name"]) # Sabin
print(student["age"]) # 35
# Using .get() (returns None instead of error if key doesn’t exist)
print(student.get("major")) # Computer Science
print(student.get("grade")) # None
print(student.get("grade", "N/A")) # Default value if key is missing
03. Modifying & Adding Values
student["age"] = 31 # Modify existing value
student["grade"] = "A" # Add new key
print(student)
04. Deleting Values
del student["major"] # Delete specific key
print(student)
grade = student.pop("grade", "N/A") # Remove and return value
print("Removed value:", grade)
05. Dictionary Methods
person = {"name": "Anna", "age": 22}
print(person.keys()) # dict_keys(['name', 'age'])
print(person.values()) # dict_values(['Anna', 22])
print(person.items()) # dict_items([('name', 'Anna'), ('age', 22)])
# Loop through dictionary
for k, v in person.items():
print(k, ":", v)
06. Nested Dictionaries
Dictionaries can contain other dictionaries.
students = {
"s1": {"name": "Tom", "age": 20},
"s2": {"name": "Anna", "age": 22}
}
print(students["s1"]["name"]) # Tom
07. Practice Examples
Example 1: Print Student Info
student = {"name": "Sabin", "age": 35, "major": "CS"}
for k, v in student.items():
print(f"{k}: {v}")
Example 2: Manage Scores
scores = {"German": 90, "English": 85, "Math": 80}
print("Total:", sum(scores.values()))
print("Average:", sum(scores.values()) / len(scores))