SchoolWhool
Quizzes and saved progress, freeSign in freeSign in
Python · Module 4Module material

Python functions interview questions

25 questions interviewers ask on functions, from first principles to the follow-ups. The first 3 model answers are free to read; Pro opens the rest.

Defining and calling

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

    A parameter is the name in the function's definition, price in def with_tax(price):. An argument is the value passed in a call, 12.50 in with_tax(12.50). At the call, the parameter is bound to the argument's object in the call's frame.

  2. What does a function return if it has no return statement?●

    None, if its body finishes normally. A function that prints its result instead of returning it looks right in a notebook, but total = show_tax(12.50) leaves total as None, and total + 1 gives TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'.

  3. How do you return several values from a function?●

    Return them separated by commas; Python packs them into a tuple, and the caller unpacks it. to_hms(7384) returns (2, 3, 4), and h, m, s = to_hms(7384) binds the three names.

  4. What is the difference between with_tax and with_tax(12.50)?●●
  5. Are type hints enforced?●●
  6. What is a docstring, and how do you read one?●

Arguments

  1. What is the difference between positional and keyword arguments?●
  2. What are *args and **kwargs?●●
  3. What does a bare * or a / mean in a signature?●●
  4. What is the mutable default argument problem?●●
  5. Does Python pass arguments by value or by reference?●●

Scope

  1. What is the LEGB rule?●
  2. Why does count += 1 inside a function raise UnboundLocalError when count is a global?●●
  3. What happens to a function's local variables when it returns?●●

Recursion

  1. What are the two parts of a recursive function?●
  2. What is a RecursionError, and what causes it?●●
  3. When would you choose recursion over a loop?●●
  4. What is the call stack?●●

Functions as values

  1. What is a lambda, and what can it not contain?●
  2. How does key= work in sorted, min and max?●●
  3. What does it mean that Python's sort is stable, and why does it matter?●●
  4. What do map and filter return? Would you use them?●●
  5. What is a higher-order function? Give an example.●●
  6. What does this print, and why? fs = [lambda: i for i in range(3)], then [f() for f in fs].●●
  7. What does functools.cache do?●●