Eagle

Lesson 9: Dictionaries in Depth

The current image has no alternative text. The file name is: IMG_20250823_133730_edit_57992904186462-scaled.jpg

1. Introduction to Dictionaries

Dictionaries are one of the most powerful and flexible data structures in Python.
They store data as key-value pairs and are extremely useful when we need to access data quickly using a unique key.

Example:

student = {
    "name": "Alice",
    "age": 22,
    "major": "Computer Science"
}
print(student["name"])  # Output: Alice

2. Dictionary Properties

  • Unordered (before Python 3.7), but insertion-ordered in Python 3.7+.
  • Mutable: values can be changed.
  • Unique keys: no duplicates allowed.

3. Creating Dictionaries

# Using curly braces
person = {"name": "John", "age": 30}

# Using dict() function
person2 = dict(name="Emma", age=25)

4. Accessing Dictionary Values

car = {"brand": "Tesla", "model": "Model S"}
print(car["brand"])   # Tesla
print(car.get("model"))  # Model S

5. Modifying Dictionaries

car["color"] = "red"   # Adding a new key-value pair
car["model"] = "Model X"  # Updating existing value

6. Dictionary Methods

MethodDescriptionExample
keys()Returns all keyscar.keys()['brand','model','color']
values()Returns all valuescar.values()['Tesla','Model X','red']
items()Returns key-value pairscar.items()
update()Updates dictionarycar.update({"year": 2023})
pop()Removes keycar.pop("color")
clear()Removes all itemscar.clear()

7. Looping Through Dictionaries

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

# Output:
# brand : Tesla
# model : Model X
# year : 2023

8. Nested Dictionaries

Dictionaries can hold other dictionaries inside them.

students = {
    "student1": {"name": "Alice", "age": 20},
    "student2": {"name": "Bob", "age": 22}
}
print(students["student1"]["name"])  # Output: Alice

9. Dictionary Comprehension

squares = {x: x*x for x in range(5)}
print(squares)  # {0:0, 1:1, 2:4, 3:9, 4:16}

10. Summary

  • Dictionaries store key-value pairs.
  • Keys are unique and immutable (e.g., strings, numbers, tuples).
  • Methods like .keys(), .values(), .items(), and .update() make dictionaries powerful.
  • Useful for storing structured data like JSON.

⬇️ Download Eagle-2026 تحميل Server 1 ⬇️ Download Eagle-2026 تحميل Server 2 ⬇️ Download Eagle-2026تحميل Server 3

Related Posts

NovaPlayer

Lesson 8: Lists, Tuples, and Sets in Python 1. What is a Dictionary in Python? A dictionary in Python is a collection of key-value pairs. 📌 Example: 2. Creating Dictionaries…

Read more

Akratv

Lesson 7 – Functions and Scope in Python 📘 Introduction In Python, lists, tuples, and sets are three important ways to store collections of items. Each has unique features and…

Read more

FivG_Pro

🔰 What Is a Function? A function is a reusable block of code that performs a specific task.Instead of repeating code, we can write it once and call it whenever…

Read more

Show7_Pro

🟢 Lesson 6 – Intermediate Level 🔰 What Is a Module? A module is simply a Python file (.py) containing code (functions, variables, classes) you can reuse in other programs….

Read more

GoldiptvPro

🟢 Lesson 5 – Intermediate Level 🔰 What Are Exceptions? An exception is an error that occurs during the execution of a program.If not handled, it will stop the program…

Read more

Domingo

🟢 Lesson 4 🔰 What Is OOP? Object-Oriented Programming (OOP) is a programming style where you organize code into objects that contain: Python supports OOP using: 🧱 Example: Why Use…

Read more