Completed
Available
Locked · open anytime

Level 3: Advanced Python Exercises

Level 3 opens the hood of Python. You'll learn how for loops really work, write generators that process endless data lazily, build closures and decorators, create your own with statements, let dataclasses write boilerplate for you and make slow functions instant with caching.

These are the features behind the libraries you use every day — Flask routes, pytest fixtures, functools, contextlib and itertools are all built on them. The Plugin Registry capstone combines decorators and generators into the same registration pattern that powers web frameworks.

The 11 exercises in this level

  1. 1 Iterators The iteration protocol See what a for loop really does: make your own objects iterable with __iter__ and __next__. Start here — no prerequisites
  2. 2 Closures Functions that remember A function defined inside another one remembers the variables around it — the foundation of decorators and callbacks. Start here — no prerequisites
  3. 3 Generators Lazy sequences with yield Produce values one at a time with yield — handle huge or endless sequences without building them in memory. Recommended first: Iterators
  4. 4 Context Managers The with statement Guarantee setup and cleanup around a block of code — the same protocol that makes "with open(...)" close files for you. Recommended first: Iterators
  5. 5 Decorators Wrapping functions Add behaviour to any function without changing its code — the pattern behind logging, caching and access checks. Recommended first: Closures
  6. 6 Dataclasses Classes without boilerplate Let a decorator write __init__, __repr__ and comparisons for you, and keep your classes focused on what matters. Recommended first: Decorators
  7. 7 Memoization Cache results with lru_cache Make slow recursive functions instant by remembering results you've already computed — with one decorator from the standard library. Recommended first: Decorators
  8. 8 Lazy Chunking Generators on endless data Process data in batches without loading it all into memory — even data that never ends. Recommended first: Generators
  9. 9 Plugin Registry Capstone — decorators meet generators Build the pattern behind Flask routes and pytest fixtures: a decorator that registers functions, and a generator that runs them lazily. Recommended first: Decorators, Generators
  10. 10 Properties Attributes with logic behind them Run code when an attribute is read or set with @property, to compute values on the fly and reject invalid ones. Start here — no prerequisites
  11. 11 Magic Methods Make objects work with + and == Teach your own classes to work with +, == and print() by defining __add__, __eq__ and __repr__. Recommended first: Properties

Frequently asked questions

Is Level 3 only for professional developers?
No. If you've completed Level 2 or have similar experience, you're ready. Each exercise still starts with a short lesson and a small, focused task.
Where are decorators and generators used in practice?
Decorators register routes in Flask and FastAPI, add caching with lru_cache and wrap tests in pytest. Generators stream large files, database results and API pages without loading everything into memory.
Why learn the iterator protocol if I can just use for loops?
Knowing how iteration works lets you make your own objects iterable, understand generators deeply and read library code with confidence. It's the foundation for the rest of this level.
What should I learn after Level 3?
Apply what you've learned in a real project — a command-line tool, a small web app or a data-processing script. Type hints, asyncio and testing with pytest are natural next topics.