from itertools import chain
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
for num in chain(list1, list2, list3):
print(num, end=' ') # 1 2 3 4 5 6 7 8 9
from itertools import product
colors = ['red', 'blue']
sizes = ['S', 'M', 'L']
for color, size in product(colors, sizes):
print(f"{color}-{size}", end=' ')
# red-S red-M red-L blue-S blue-M blue-L
from itertools import groupby
data = sorted([
('A', 1), ('B', 2), ('A', 3), ('B', 4)
])
for key, group in groupby(data, key=lambda x: x[0]):
print(f"{key}: {list(group)}")
# A: [('A', 1), ('A', 3)]
# B: [('B', 2), ('B', 4)]
from itertools import islice
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Get first 10 Fibonacci numbers
first_10 = list(islice(fibonacci(), 10))
print(first_10) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
from collections import Counter
words = "the cat sat on the mat the cat".split()
count = Counter(words)
print(count.most_common(3))
# [('the', 3), ('cat', 2), ('sat', 1)]
from collections import defaultdict
# Group items by category
items = [("fruit", "apple"), ("veg", "carrot"), ("fruit", "banana")]
grouped = defaultdict(list)
for category, item in items:
grouped[category].append(item)
print(dict(grouped))
# {'fruit': ['apple', 'banana'], 'veg': ['carrot']}
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(3, 4)
print(p.x, p.y) # 3 4
print(p) # Point(x=3, y=4)
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def celsius(self):
return self._celsius
@celsius.setter
def celsius(self, value):
if value < -273.15:
raise ValueError("Below absolute zero!")
self._celsius = value
@property
def fahrenheit(self):
return self._celsius * 9/5 + 32
temp = Temperature(25)
print(temp.celsius) # 25
print(temp.fahrenheit) # 77.0
temp.celsius = 100 # Uses the setter
print(temp.fahrenheit) # 212.0
# temp.celsius = -300 # Raises ValueError!
# ❌ Old way — might not close if error occurs
f = open('file.txt', 'r')
data = f.read()
f.close()
# ✅ with statement — always closes!
with open('file.txt', 'r') as f:
data = f.read()
# File is closed here automatically
with open('input.txt') as src, open('output.txt', 'w') as dst:
dst.write(src.read().upper())
from contextlib import contextmanager
@contextmanager
def timer():
import time
start = time.time()
yield
print(f"Took {time.time() - start:.2f}s")
with timer():
sum(range(10_000_000))
# Output: Took 0.32s
try:
result = 10 / 0
except ZeroDivisionError:
print("Can't divide by zero!")
try:
value = int(input("Enter a number: "))
result = 100 / value
except ValueError:
print("That's not a valid number!")
except ZeroDivisionError:
print("Can't divide by zero!")
except Exception as e:
print(f"Unexpected error: {e}")
finally:
print("This always runs!")
# ❌ Look Before You Leap (LBYL)
if key in my_dict:
value = my_dict[key]
# ✅ EAFP — more Pythonic!
try:
value = my_dict[key]
except KeyError:
value = default_value
nums = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, nums))
# [1, 4, 9, 16, 25]
# With a named function
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
temps_c = [0, 20, 37, 100]
temps_f = list(map(celsius_to_fahrenheit, temps_c))
# [32.0, 68.0, 98.6, 212.0]
nums = range(1, 21)
evens = list(filter(lambda x: x % 2 == 0, nums))
# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
words = ["hello", "", "world", "", "python"]
non_empty = list(filter(None, words))
# ["hello", "world", "python"]
# Get squares of even numbers only
nums = range(1, 11)
result = list(map(lambda x: x**2, filter(lambda x: x % 2 == 0, nums)))
# [4, 16, 36, 64, 100]
[x**2 for x in range(1, 11) if x % 2 == 0]
lambda arguments: expression
# Simple lambda
double = lambda x: x * 2
double(5) # 10
# Multiple arguments
add = lambda x, y: x + y
add(3, 4) # 7
students = [
{"name": "Alice", "gpa": 3.8},
{"name": "Bob", "gpa": 3.5},
{"name": "Charlie", "gpa": 3.9}
]
# Sort by GPA descending
students.sort(key=lambda s: s["gpa"], reverse=True)
# Charlie (3.9), Alice (3.8), Bob (3.5)
nums = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, nums))
# [1, 4, 9, 16, 25]
evens = list(filter(lambda x: x % 2 == 0, nums))
# [2, 4]
def add(*args):
return sum(args)
add(1, 2, 3) # 6
add(10, 20) # 30
add(1, 2, 3, 4, 5) # 15
def greet(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
greet(name="Alice", age=30, city="NYC")
# name: Alice
# age: 30
# city: NYC
def super_func(required, *args, **kwargs):
print(f"Required: {required}")
print(f"Args: {args}")
print(f"Kwargs: {kwargs}")
super_func("hello", 1, 2, 3, color="red")
# Required: hello
# Args: (1, 2, 3)
# Kwargs: {"color": "red"}
{key: value for item in iterable}
# Square mapping
squares = {x: x**2 for x in range(6)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# Invert a dictionary
original = {"a": 1, "b": 2, "c": 3}
inverted = {v: k for k, v in original.items()}
# {1: "a", 2: "b", 3: "c"}
# Filter a dictionary
scores = {"Alice": 95, "Bob": 67, "Charlie": 82}
high_scorers = {k: v for k, v in scores.items() if v >= 80}
# {"Alice": 95, "Charlie": 82}
# Count word frequency
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
freq = {w: words.count(w) for w in set(words)}
# {"apple": 3, "banana": 2, "cherry": 1}
names = ["Alice", "Bob", "Charlie"]
scores = [95, 87, 92]
for i in range(len(names)):
print(f"{names[i]}: {scores[i]}")
for name, score in zip(names, scores):
print(f"{name}: {score}")
# Unzip with zip(*)
pairs = [("a", 1), ("b", 2), ("c", 3)]
letters, numbers = zip(*pairs)
# letters = ("a", "b", "c")
# numbers = (1, 2, 3)
# Create a dict from two lists
names = ["Alice", "Bob"]
scores = [95, 87]
result = dict(zip(names, scores))
# {"Alice": 95, "Bob": 87}