Python Tutorial: Understanding Dictionaries in Python
🔑 Python Tutorial: Understanding Dictionaries in Python
Introduction
Dictionaries are Python’s built-in data structure for storing key-value pairs. They’re perfect for representing structured data like user profiles, settings, and JSON objects.
1. Creating a Dictionary
person = {"name": "Koikoi", "age": 25, "is_student": True}
print(person)
Try it yourself: Create a dictionary for your favorite book with title, author, and genre.
2. Accessing Values
print(person["name"]) # Koikoi
print(person["age"]) # 25
Exercise: Print the genre of your book dictionary.
3. Modifying Values
person["age"] = 26
print(person)
Challenge: Update one value in your dictionary.
4. Adding and Removing Keys
person["location"] = "Kenya"
del person["is_student"]
print(person)
Try it: Add a new key and remove an old one.
5. Looping Through Dictionaries
for key, value in person.items():
print(key, ":", value)
Exercise: Loop through your book dictionary and print each key-value pair.
6. Real‑World Example: User Profile
user = {
"username": "koikoi",
"email": "koikoi@example.com",
"verified": True
}
user["last_login"] = "2026-03-24"
for k, v in user.items():
print(f"{k}: {v}")
Challenge: Add a new field like “subscription” and print the updated profile.
Want to dive deeper? Check out these related tutorials:
➡
Python Lists Explained
➡
Python Interactive Tutorial: From Basics to Mastery
➡
Python Tutorial: Mastering Fuzzy Buzzy (FizzBuzz)
💻 Programming & Science Books — Only $1 Each
Learn coding, algorithms, and data science with my easy-to-follow tutorials.
Buy on Gumroad Buy on Amazon KDP


Comments
Post a Comment