0–5 min
Real Hook
5–25 min
Core Concept
25–45 min
Live Coding
45–55 min
You Code It
55–60 min
Cheat Sheet
🔥 Real-World Hook (0–5 min)
A Netflix engineer filters 10,000 movies to your 20 recommendations in one Python line. A Swiggy data scientist processes 1 million orders in a pipeline that reads like a sentence.
List comprehensions, generators, decorators. The features that let Python code be shorter, faster, and more readable than Java. These are what interviewers look for to tell a Python beginner from a Python developer.
▶ 3 Power Features (5–25 min)
💡 Mental Model
List Comprehension — A for-loop that returns a new list, written in one line. Replace 5 lines with 1.
Generator — Like a list comprehension but lazy — produces values one at a time. Use it for huge datasets that don't fit in RAM.
Decorator — A function that wraps another function. Add logging, timing, or auth without touching the original code.
💻 Live Coding — Student Data Pipeline (25–45 min)
students = [
{"name": "Priya", "marks": 92, "dept": "CS"},
{"name": "Ravi", "marks": 45, "dept": "IT"},
{"name": "Anita", "marks": 78, "dept": "CS"},
{"name": "Kumar", "marks": 33, "dept": "EC"},
{"name": "Meena", "marks": 88, "dept": "CS"},
]
# ── List Comprehension ─────────────────────────────────────────────────────
# Normal loop (5 lines):
passed = []
for s in students:
if s["marks"] >= 60:
passed.append(s["name"])
# Comprehension (1 line — same result):
passed = [s["name"] for s in students if s["marks"] >= 60]
print(passed) # ['Priya', 'Anita', 'Meena']
# CS students, sorted by marks, top 3 names
top_cs = [s["name"] for s in
sorted(students, key=lambda s: s["marks"], reverse=True)
if s["dept"] == "CS" and s["marks"] >= 60][:3]
print(top_cs) # ['Priya', 'Meena', 'Anita']
# ── Generator ──────────────────────────────────────────────────────────────
# List: loads ALL marks into memory at once
all_marks = [s["marks"] for s in students]
# Generator: produces one mark at a time — use for 1 million records
marks_gen = (s["marks"] for s in students) # () not []
avg = sum(marks_gen) / len(students)
print(f"Average: {avg}")
# ── Decorator ──────────────────────────────────────────────────────────────
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f"{func.__name__} took {time.time()-start:.4f}s")
return result
return wrapper
@timer # apply decorator — no code change inside process_data!
def process_data(students):
return [s["name"] for s in students if s["marks"] >= 60]
process_data(students) # prints: "process_data took 0.0001s"
🎯 You Code It (45–55 min)
- Create a
products list of 8 dicts: name, price, in_stock (bool), category (Electronics/Clothing)
- Use a list comprehension: get names of all in-stock Electronics, sorted by price ascending
- Use a generator +
sum(): total value of all in-stock products
- Write a
@logger decorator that prints "Calling {function_name}" before every function call
- Apply
@logger to your filter function — call it and see the log
- Show: Names list + total value + decorator log output
📄 Day 5 Cheat Sheet — Advanced Python
List Comprehension
| [x for x in items] | Simple copy |
| [x for x in items if cond] | Filter |
| [f(x) for x in items] | Transform |
| {k:v for k,v in d.items()} | Dict comprehension |
| {x for x in items} | Set comprehension |
Lambda & sorted
| lambda x: x*2 | Anonymous function |
| sorted(lst, key=lambda x: x[1]) | Sort by field |
| sorted(lst, reverse=True) | Descending |
| min/max(lst, key=lambda x: x.gpa) | Min/max by field |
| filter(lambda x: x>0, lst) | Filter lazy |
Generator
| (x for x in items) | Generator expression |
| def gen(): yield x | Generator function |
| next(gen) | Get next value |
| list(gen) | Convert to list |
| sum(x for x in items) | Sum without list |
Decorator Pattern
| def dec(func): | Outer function |
| def wrapper(*a,**k): | Wrapper with passthrough |
| return func(*a,**k) | Call original |
| return wrapper | Return wrapper |
| @dec | Apply decorator |
| @functools.wraps(func) | Preserve name/docs |