practicepython.in
python exercises
171 exercises

Functions that take functions

A function is a value: you can pass one in, return one, and store one in a dict. This is the step where Python stops feeling like a list of instructions and starts feeling like a language you can build with — and where retries, callbacks and decorators stop being magic.

10 exercisesdifficulty 2–4~212 min in total
Transform every value in a mapping functions as arguments, dict comprehensions ~15 min Sorting when some of the keys are missing decorate-sort-undecorate, None does not compare ~18 min Chain two functions into one returning a function, closures ~20 min A decorator that keeps a tally attributes on a function, functools.wraps ~22 min Find the first one that fits next with a default, generator expressions ~20 min Run it the first time and never again state that outlives a call, None is a real result ~20 min Both halves, one pass higher-order functions, calling a predicate once ~20 min Remember what you already worked out decorators, closures over a cache ~28 min Steps in the order you read them returning a function, left to right ~24 min Retry something that fails sometimes functions as arguments, try/except in a loop ~25 min