Skip to main content
  1. Notes/
  2. Python 🐍/

πŸ“– Dictionaries in Python | Lesson 07

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 7: This Article

πŸ“– Dictionaries in Python
#

Dictionaries are one of the most powerful and versatile data types in Python. They allow you to store data in key-value pairs, making it super easy to organize and look up information. Let’s dive into how dictionaries work! 🌟


What is a Dictionary? πŸ”
#

A dictionary is a collection of key-value pairs enclosed in curly braces { }. Each key points to a value.

student = {
    "name": "Ravi",
    "age": 21,
    "city": "Mumbai"
}
  • “name”, “age”, “city” are keys.
  • “Ravi”, 21, and “Mumbai” are their corresponding values.

Creating Dictionaries ✍️
#

empty_dict = {}
person = {"first_name": "Asha", "last_name": "Sharma", "age": 25}

Accessing Values πŸ”‘
#

Use the key inside square brackets or the .get() method.

print(person["first_name"])   # Asha
print(person.get("age"))      # 25

Adding & Modifying Items ✏️
#

Add or change a key’s value by simple assignment:

person["age"] = 26           # Modify existing key
person["city"] = "Delhi"     # Add new key-value pair

Removing Items ❌
#

Several methods to remove:

  • del person["city"] β€” deletes the key and value
  • person.pop("age") β€” removes key and returns value
  • person.clear() β€” empties the whole dictionary

Useful Dictionary Methods πŸ› οΈ
#

MethodDescriptionExample
.keys()Returns all keysperson.keys()
.values()Returns all valuesperson.values()
.items()Returns pairs (key, value)person.items()
.update()Updates dictionary with anotherperson.update(new_info)

Looping Through Dictionaries πŸ”„
#

Iterate through keys, values, or both:

for key in person:
    print(key, ":", person[key])

# Or

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

Practice Questions with Solutions 🎯
#

  1. Practice: Create a dictionary for a book with keys title, author, and year. Print the author.
    • Solution:
book = {"title": "Python 101", "author": "John Smith", "year": 2020}
print(book["author"])   # John Smith
  1. Practice: Update the year of the book to 2021.
    • Solution:
book["year"] = 2021
  1. Practice: Remove the year key from the dictionary.
    • Solution:
del book["year"]

Step-by-Step Mini Project: Contact Book πŸ“’
#

Create a dictionary to store contact details like name, phone, and email, and print all details.

contact = {
    "name": "Sita",
    "phone": "9998887777",
    "email": "sita@example.com"
}

for key, value in contact.items():
    print(f"{key.title()}: {value}")

Output:

Name: Sita
Phone: 9998887777
Email: sita@example.com

Checklist for This Chapter βœ…
#

  • Created and accessed dictionaries
  • Added, modified, and removed items
  • Used common dictionary methods
  • Looped through keys and values effectively

You are now ready to organize data smartly with dictionariesβ€”think of them as your real-world data maps! Keep practicing by building address books, inventory systems, and more! 🎯

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 7: This Article

Related

⏰ Working with Date and Time in Python | Lesson 16
Python Progmramming Course
🎬 Strings in Python | Lesson 04
Python Progmramming Course
πŸ—οΈ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15
Python Progmramming Course
πŸ‘Ά Python Basics | Lesson 02
Python Progmramming Course
πŸ“„ File Handling in Python | Lesson 14
Python Progmramming Course
πŸ“‹ Lists in Python | Lesson 05
Python Progmramming Course