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)