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

FlyFeditv

📘 Introduction In this project, you’ll create a command-line Expense Tracker that allows users to: ✅ Add expenses with category, amount, and description✅ View total spending and category breakdown✅ Automatically…

Read more

NetCinFly

📘 Introduction In this project, you’ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

🌐 Introduction In this project, you’ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.You’ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

🌦️ Introduction In this project, you’ll build a Weather App in Python that retrieves live weather information from an online API.You’ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: 🎯 Objective: Create a simple, text-based contact book application that allows users to: 💡 Code (contact_book.py): 🧠 Example Usage Console Output Example: 💾 Notes ⬇️ Download Show7-Pro تحميل…

Read more

Rapid_tv

🔹 Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. 👉 Rules: 🔹 Code Example 🔹 Example Run 🔹 Concepts Learned…

Read more