Today, I focused on one of Python’s most versatile structures: Dictionaries.
🔹 What is a Dictionary?
Dictionaries store data as key-value pairs.
student = {"name": "Alice", "age": 22}
print(student["age"]) # 22
🔹 Dictionary Comprehensions
A Pythonic way to build dictionaries in one line.
squares = {x: x**2 for x in range(5)}
print(squares)
{0:0, 1:1, 2:4, 3:9, 4:16}
🔹 Merging Dictionaries
Python 3.5+ allows merging with unpacking.
d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}
merged = {**d1, **d2}
print(merged)
{'a':1, 'b':2, 'c':3, 'd':4}
🎯 Why Dictionaries Matter?
• Fast lookups
• Perfect for structured data
• Comprehensions & merging make them concise and powerful
⚡ Tomorrow → I’ll explore sets and set operations in Python.
Top comments (0)