Python Dictionaries

1. What is a Dictionary?

A dictionary is a collection of data stored as key : value pairs.

In simple words:
👉 A dictionary is like a real dictionary

  • WordMeaning

  • KeyValue

2. Why Use Dictionary?

We use dictionary when:

  • Data has names (keys)

  • We want fast access to values

Example:

  • Student name → marks

  • Product name → price

3. Creating a Dictionary

Dictionaries use curly brackets { }.

Example

student = {
"name": "Amit",
"age": 20,
"city": "Delhi"
}
print(student)

4. Access Dictionary Items

Use the key name to get value.

Example

print(student["name"])
print(student["age"])

5. Access Using get()

get() is safer because it does not give error.

Example

print(student.get("city"))
print(student.get("marks"))

6. Change Dictionary Value

Example

student["age"] = 21
print(student)

7. Add New Item to Dictionary

Example

student["marks"] = 85
print(student)

8. Remove Dictionary Items

Remove Using pop()

student.pop("city")
print(student)

Remove Last Item (popitem())

student.popitem()
print(student)

Delete Using del

del student["age"]

9. Loop Through Dictionary

Loop Keys

for key in student:
print(key)

Loop Values

for value in student.values():
print(value)

Loop Key & Value

for key, value in student.items():
print(key, value)

10. Check if Key Exists

Example

print("name" in student)
print("phone" in student)

11. Dictionary Length

Example

print(len(student))

12. Dictionary with Different Data Types

Example

data = {
"name": "Python",
"version": 3,
"easy": True
}
print(data)

13. Common Beginner Mistakes

❌ Using Index Instead of Key

print(student[0])

❌ Error.

❌ Accessing Missing Key

print(student["phone"])

✔ Use:

print(student.get("phone"))

14. Simple Practice Examples

Example 1: Product Dictionary

product = {
"name": "Laptop",
"price": 50000
}
print(product[“price”])

Example 2: Update Value

product["price"] = 48000
print(product)

Example 3: Add Item

product["brand"] = "HP"
print(product)

15. Summary (Python Dictionaries)

✔ Stores data in key-value form
✔ Keys are unique
✔ Values can change
✔ Fast and useful
✔ Very important in Python

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Access Dictionary Items

  • Change Dictionary Items

  • Dictionary Methods

  • Dictionary Exercises

Just tell me 😊

You may also like...