👋 Work With Me

I help startups and teams build production-ready apps with Django, Flask, and FastAPI.

Let’s Talk →

I'm always excited to take on new projects and collaborate with innovative minds.

Address

No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory

Social Links

Tutorials

Python Tutorial Series – Lesson 7: Dictionaries in Python

Python Tutorial for Beginners (Lesson 7): Learn dictionaries in Python with examples, screenshots, and exercises – key-value pairs, methods, nesting, and practical use cases explained.

Python Tutorial Series – Lesson 7: Dictionaries in Python

Welcome back to the Python Tutorial Series for Beginners! 🎉

In Lesson 6, we explored tuples – immutable collections of items.

Now in Lesson 7, we’re diving into one of the most powerful data types in Python: dictionaries.

By the end of this lesson, you’ll understand:

  • What dictionaries are
  • How to create and use them
  • Accessing and modifying values
  • Dictionary methods
  • Nesting dictionaries
  • Practical exercises

🔹 What is a Dictionary?

A dictionary in Python is a collection of key-value pairs.

  • Keys must be unique
  • Values can be of any type
  • Dictionaries are unordered (Python 3.7+ keeps insertion order, but logically we don’t rely on it)

👉 Think of a dictionary as a real dictionary: you look up a word (key) to find its definition (value).

 
# Example of a dictionary
person = {
   "name": "John",
   "age": 25,
   "job": "Developer"
}

print(person["name"])  # John

🔹 1. Creating a Dictionary

Dictionaries are created using curly braces {}.

 
# Empty dictionary
my_dict = {}

# Dictionary with values
car = {
   "brand": "Toyota",
   "model": "Corolla",
   "year": 2020
}

🔹 2. Accessing Values

You can access values using the key.

 
car = {"brand": "Toyota", "model": "Corolla", "year": 2020}

print(car["brand"])   # Toyota
print(car.get("year"))  # 2020

👉 .get() is safer because it doesn’t throw an error if the key doesn’t exist.


🔹 3. Adding & Modifying Items

 
car = {"brand": "Toyota", "model": "Corolla", "year": 2020}

# Add new key-value
car["color"] = "Blue"

# Modify existing key-value
car["year"] = 2022

print(car)

🔹 4. Removing Items

 
car = {"brand": "Toyota", "model": "Corolla", "year": 2020}

car.pop("year")       # Remove by key
del car["model"]      # Another way
car.clear()           # Remove all items

🔹 5. Useful Dictionary Methods

 
person = {"name": "Alice", "age": 30, "city": "Lagos"}

print(person.keys())    # dict_keys(['name', 'age', 'city'])
print(person.values())  # dict_values(['Alice', 30, 'Lagos'])
print(person.items())   # dict_items([('name', 'Alice'), ('age', 30), ('city', 'Lagos')])

🔹 6. Looping Through a Dictionary

 
person = {"name": "Alice", "age": 30, "city": "Lagos"}

for key, value in person.items():
   print(key, ":", value)

🔹 7. Nested Dictionaries

Dictionaries can contain other dictionaries (like JSON).

 
students = {
   "student1": {"name": "John", "age": 20},
   "student2": {"name": "Mary", "age": 22}
}

print(students["student1"]["name"])  # John

🔹 Exercises for Beginners

Try these in a new file (lesson7.py):

  1. Create a dictionary about yourself with keys: "name", "age", "hobby". Print your hobby.
  2. Add a new key "country" to your dictionary.
  3. Remove the "age" key.
  4. Loop through the dictionary and print all keys and values.
  5. Challenge: Create a nested dictionary of 2 students with their names and scores. Print the score of one student.

🎯 Recap

In this lesson, you learned:
✅ Dictionaries store data in key-value pairs
✅ You can add, modify, and remove items
✅ Useful methods: .keys(), .values(), .items()
✅ Dictionaries can be nested (like JSON structures)

Next up: Lesson 8 – Sets in Python 🟢 (another collection type with unique items).

💼 Need a Developer?

I'm Kingsley Odume, a Django, Flask, and FastAPI developer with experience building SaaS platforms, APIs, and modern web apps. If you're a recruiter or business owner looking for a reliable software developer, let's connect!

🚀 Hire Me

Python, Python tutorials, Python Beginners series, Python Step-byStep
3 min read
Aug 20, 2025
By Kingsley Odume
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Aug 29, 2025 • 3 min read
Step-by-Step: Building a SaaS App with Django + Stripe Payments

Learn how to build a real-world SaaS app with Django and Stripe paymen...

Aug 21, 2025 • 3 min read
Final Wrap-Up: Python Tutorial Series for Beginners

Congratulations on completing the Python Tutorial Series! 🚀 In this w...

Aug 20, 2025 • 4 min read
Final Lesson: Mini Project – Putting It All Together in Python

In this final lesson, we’ll bring all those concepts together in a Min...

Your experience on this site will be improved by allowing cookies. Cookie Policy