🌍 Python Programming · flashcards
Python Programming Functions and Functional Programming Flashcards
50 question-and-answer cards covering Functions and Functional Programming as it is examined in Python Programming. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.
24 sample cards from the Functions and Functional Programming deck
Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.
What does the yield statement do?
It produces a value to the caller and suspends the function, preserving its local state so execution resumes right after the yield on the next call to next().
What is the key advantage of generators over building and returning a list?
Generators are lazy and memory-efficient: they compute values one at a time on demand instead of storing the whole sequence in memory, enabling infinite or very large sequences.
What is a generator expression and how does it differ syntactically from a list comprehension?
A generator expression uses parentheses instead of square brackets, e.g. (x*x for x in range(10)). It produces a lazy iterator rather than a fully built list.
When you pass a generator expression as the sole argument to a function, what syntax simplification is allowed?
You may omit the extra parentheses, e.g. sum(x*x for x in range(10)) instead of sum((x*x for x in range(10))).
What is a closure?
A closure is a nested function that captures and remembers variables from its enclosing scope, so it can access those free variables even after the enclosing function has returned.
What is a free variable in the context of closures?
A free variable is a variable used in a function that is neither a local variable nor a parameter of that function, but is bound in an enclosing scope.
How can you inspect the free variables captured by a closure?
Through the function's __closure__ attribute (a tuple of cell objects); each cell's cell_contents holds the captured value. The names are in function.__code__.co_freevars.
What three conditions are required to create a closure in Python?
There must be a nested (inner) function, the inner function must reference a variable from the enclosing function, and the enclosing function must return the inner function.
What is a decorator in Python?
A decorator is a callable that takes a function (or class) and returns a modified/wrapped version, typically to add behavior without changing the original code. Applied with the @decorator syntax above a definition.
How does @my_decorator above a function definition desugar?
@my_decorator over def f(): ... is equivalent to f = my_decorator(f) after the definition.
What is the standard structure of a simple decorator?
A function that takes func, defines an inner wrapper(*args, **kwargs) that calls func and adds behavior, and returns wrapper.
Why should a decorator's wrapper accept *args and **kwargs?
So the wrapper can transparently accept and forward any arguments the original function takes, making the decorator work on functions with any signature.
What problem does @functools.wraps solve in a decorator?
It copies the wrapped function's metadata (__name__, __doc__, __module__, etc.) onto the wrapper, so the decorated function retains its original identity instead of appearing as 'wrapper'.
How does a decorator that takes arguments differ in structure from a plain decorator?
It needs three nested levels: an outer factory that accepts the arguments and returns the actual decorator, which accepts the function and returns the wrapper. Effectively f = factory(args)(f).
What does @repeat(3) above def f() desugar to (decorator with arguments)?
It desugars to f = repeat(3)(f): repeat(3) returns a decorator, which is then applied to f.
What does functools.lru_cache do?
It memoizes a function by caching results keyed on the arguments, so repeated calls with the same arguments return the stored result. It is applied as a decorator (@lru_cache(maxsize=None) for unbounded).
What does functools.partial create?
A new callable with some of the original function's arguments pre-filled (fixed), reducing the number of arguments you must supply at call time, e.g. partial(int, base=2).
What does functools.reduce require and return compared to a comprehension?
reduce takes a binary function and an iterable and folds them into one accumulated value; unlike a comprehension it returns a single scalar result, not a new sequence.
What is the purpose of functools.cache (Python 3.9+)?
It is a simple unbounded memoization decorator, equivalent to lru_cache(maxsize=None), storing all past call results without eviction.
What does the functools.wraps decorator use internally?
It is itself implemented via functools.update_wrapper, which copies attributes like __module__, __name__, __qualname__, __doc__, and __dict__, and sets __wrapped__ to the original function.
What is the difference between map() and a list comprehension?
map(f, it) applies an existing function lazily and returns an iterator; a list comprehension [f(x) for x in it] can include inline expressions and conditions and eagerly builds a list. Comprehensions are often more readable.
Can you iterate over the same generator object twice? Why or why not?
No. A generator is a one-time iterator; once exhausted it yields nothing more. You must create a new generator to iterate again.
What does functools.reduce(lambda a, b: a if a > b else b, [3, 7, 2]) compute?
It computes the maximum, 7, by cumulatively keeping the larger of each pair across the list.
How do you convert the result of map() or filter() into a concrete list?
Wrap the call in list(), e.g. list(map(str, nums)) or list(filter(is_even, nums)), because both return lazy iterators in Python 3.
What this deck covers
The Functions and Functional Programming deck follows the Python Programming Functions and Functional Programming syllabus — 5 chapters and 16 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 10.0 cards per chapter.
Answers are written to be recallable, not just readable — averaging about 157 characters, which is long enough to carry the reasoning and short enough to say out loud.
A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.
Functions and Functional Programming flashcards FAQ
How many Functions and Functional Programming flashcards are in this Python Programming deck?
50 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.
Are these Python Programming flashcards free?
Yes. The preview here is free to read with no signup, and the full 50-card deck is free inside the Examius app.
What do the Functions and Functional Programming cards cover?
They follow the Python Programming Functions and Functional Programming syllabus — 5 chapters and 16 topics — so the questions track what is actually examinable.
How should I use these flashcards?
Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.