Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 10 Dictionaries in Python

Updated
2 min read
S

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))

Python Basics

Part 10 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 11 Tuples & Sets in Python

01. Tuples ✅ Features Store multiple values in order (similar to lists) Use parentheses ( ) Immutable (cannot be changed) → good for read-only data fruits = ("Apple", "Banana", "Grape") print(fruits[0]) # Apple print(fruits[-1]) # Grape ✅ Tu...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.