🌍 Python Programming · subject

Python Programming Functions and Functional Programming Syllabus

The Functions and Functional Programming syllabus for Python Programming in full: 5 chapters and 16 topics, every one listed below, plus 50 Functions and Functional Programming flashcards with answers.

5Chapters
16Topics
50Flashcards

Functions and Functional Programming syllabus: all 5 chapters

Expand any chapter to see its topics and sub-topics.

  1. Defining Functions

    3 topics
    • Function Syntax and return
    • Parameters and Arguments
    • Docstrings
  2. Scope and Namespaces

    3 topics
    • Local and Global Scope
    • The global and nonlocal Keywords
    • LEGB Rule
  3. Lambda and Higher-Order Functions

    3 topics
    • Lambda Expressions
    • map(), filter() and reduce()
    • Functions as First-Class Objects
  4. Iterators and Generators

    3 topics
    • The Iterator Protocol
    • Generator Functions and yield
    • Generator Expressions
  5. Closures and Decorators

    4 topics
    • Closures and Free Variables
    • Writing Decorators
    • Decorators with Arguments
    • functools Utilities

Python Programming Functions and Functional Programming flashcards — 50 questions with answers

  1. What keyword defines a function in Python, and what does a function return if no return statement is executed?

    Functions are defined with the def keyword. If no return statement runs (or return has no value), the function returns None.

  2. What is the difference between a parameter and an argument?

    A parameter is the variable named in the function definition; an argument is the actual value passed to the function when it is called.

  3. Distinguish positional arguments from keyword arguments in a function call.

    Positional arguments are matched to parameters by order/position; keyword arguments are matched by explicitly naming the parameter (e.g. func(x=5)).

  4. What do *args and **kwargs collect in a function definition?

    *args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dict.

  5. Why can using a mutable object (like a list) as a default parameter value be dangerous?

    The default is evaluated once at definition time and shared across all calls, so mutations persist between calls. Use None as the default and create the object inside the function instead.

  6. What is a docstring and how do you access it programmatically?

    A docstring is a string literal placed as the first statement in a module, function, class, or method to document it. It is accessed via the object's __doc__ attribute (or help()).

  7. Which quoting style is conventionally used for docstrings and why?

    Triple double-quotes ("""..."""), because they allow multi-line text and can contain single quotes without escaping.

  8. Define local scope and global scope.

    Local scope is the namespace inside a function, existing only during its execution. Global scope is the module-level namespace accessible throughout the module.

  9. What happens if you assign to a variable inside a function that also exists globally?

    By default the assignment creates a new local variable that shadows the global; the global is unchanged unless you declare it with the global keyword.

  10. What does the global keyword do?

    It tells Python that a name inside a function refers to the module-level (global) variable, so assignments modify the global rather than creating a local.

  11. What does the nonlocal keyword do?

    It binds a name to the nearest enclosing (non-global) function scope, allowing an inner function to reassign a variable in an outer enclosing function.

  12. What error occurs if you use nonlocal without a matching enclosing variable?

    A SyntaxError, because nonlocal requires an existing binding in an enclosing function scope (it cannot create one and cannot refer to global scope).

  13. State the LEGB rule and what each letter stands for.

    LEGB is the order Python searches names: Local, Enclosing (enclosing functions), Global (module), Built-in. The first match found is used.

  14. In the LEGB rule, where does Python look last for a name?

    In the Built-in scope (the builtins module, e.g. len, print, range), which is searched after Local, Enclosing, and Global fail.

  15. What is a lambda expression and how does it differ from a def function?

    A lambda is an anonymous function written inline as 'lambda args: expression'. It has no name, its body is a single expression (no statements), and it returns that expression's value automatically.

  16. Can a lambda contain multiple statements or assignments?

    No. A lambda body must be a single expression; it cannot contain statements, assignments, or annotations (though it can use conditional expressions).

  17. What does map(function, iterable) do and what does it return?

    It applies function to each element of the iterable and returns a lazy map object (an iterator) yielding the results. Wrap in list() to materialize.

  18. What does filter(function, iterable) return?

    A lazy iterator yielding only the elements for which function(element) is truthy. If function is None, it keeps the truthy elements.

  19. What does functools.reduce(function, iterable[, initializer]) do?

    It applies a two-argument function cumulatively across the iterable, reducing it to a single value (e.g. reduce(lambda a,b: a+b, [1,2,3]) gives 6). With an initializer, it starts from that value.

  20. From which module must reduce be imported in Python 3?

    From functools (functools.reduce); it is no longer a built-in as it was in Python 2.

Show the other 30 Functions and Functional Programming flashcards
  1. What does it mean that functions are first-class objects in Python?

    Functions can be assigned to variables, stored in data structures, passed as arguments, and returned from other functions, just like any other object.

  2. What is a higher-order function?

    A function that takes one or more functions as arguments and/or returns a function as its result (e.g. map, filter, and decorators).

  3. What two methods must an object implement to follow the iterator protocol?

    __iter__() (returns the iterator itself) and __next__() (returns the next item or raises StopIteration when exhausted).

  4. What is the difference between an iterable and an iterator?

    An iterable defines __iter__() and can produce an iterator (e.g. a list). An iterator defines __next__() and produces values one at a time and is itself iterable; it is consumed as you iterate.

  5. What exception signals the end of iteration, and how does a for loop use it?

    StopIteration. A for loop repeatedly calls __next__() and stops silently when StopIteration is raised.

  6. What is a generator function and what makes it different from a normal function?

    A generator function contains at least one yield statement. Calling it returns a generator (an iterator) without running the body; execution advances lazily on each next() call, pausing at each yield.

  7. 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().

  8. 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.

  9. 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.

  10. 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))).

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. How does @my_decorator above a function definition desugar?

    @my_decorator over def f(): ... is equivalent to f = my_decorator(f) after the definition.

  17. 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.

  18. 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.

  19. 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'.

  20. 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).

  21. 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.

  22. 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).

  23. 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).

  24. 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.

  25. 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.

  26. 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.

  27. 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.

  28. 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.

  29. 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.

  30. 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.

Python Programming Functions and Functional Programming FAQ

What is in the Python Programming Functions and Functional Programming syllabus?

Python Programming Functions and Functional Programming is split into 5 chapters: Defining Functions, Scope and Namespaces, Lambda and Higher-Order Functions, Iterators and Generators and Closures and Decorators. Together they contain 16 topics.

How many chapters are in Python Programming Functions and Functional Programming?

5 chapters, containing 16 topics. The largest chapter is Closures and Decorators, with 4 topics.

Are there flashcards for Python Programming Functions and Functional Programming?

Yes — a 50-card Functions and Functional Programming deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.