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)