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

Functions

Take each lesson, answer its quiz, then do its practice. After the last lesson come the module quiz, the project and the interview questions.

Lessons

  1. 4.1Defining FunctionsPackage code into reusable functions with inputs and outputs.
  2. 4.2Arguments and ScopePass arguments every way Python allows, and know where names live.
  3. 4.3RecursionSolve a problem by calling the same function on a smaller version of it.
  4. 4.4Lambdas and Functions as ValuesPass functions around, the pattern behind `sorted(key=...)` and `apply`.

Module material

Take these after the last lesson, in this order. Next on each page leads through them.

  1. Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
  2. Project: The Word Counter, as FunctionsStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
  3. NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
  4. Cheat sheetOne page of the module’s terms, rules and gotchas, with the output each rule produces.
  5. Interview questions25 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.

Glossary

Every term the module introduces, with the lesson that introduces it.

argument
A value passed to a function in a call: 12.50 in with_tax(12.50). 4.1
call
Running a function's body, by writing its name and round brackets: with_tax(12.50). 4.1
docstring
A string on the first line of a function's body that says what the function does. help() and __doc__ show it. 4.1
frame
The execution state for one call, including its local namespace and current instruction; its active call-stack entry ends when the call returns. 4.1
function definition
A def statement; running it creates a function object and binds its name. The body does not run until a call. 4.1
function object
An object created by a function definition or lambda, which can be passed, stored and called. 4.1
parameter
The name in a function's definition that a value will be bound to: price in def with_tax(price):. 4.1
return value
The value a call gives back. Reaching the end of the body normally without an explicit return gives back None. 4.1
type hint
A note in a function's header of the type a parameter should have, or the type it returns: review: str, -> list[str]. Python does not check it when the code runs. 4.1
*args
A parameter with one star, which collects extra positional arguments into a tuple: def total(*prices). 4.2
default value
The value a parameter takes when a call does not pass that argument: rate=0.08. 4.2
global variable
A name bound at the top level, which any function can read. Rebinding it inside a function needs global. 4.2
keyword argument
An argument passed as name=value, matched to the parameter with that name: rate=0.2. 4.2
keyword-only parameter
A parameter after * or *args, which must be passed by keyword; it can be required even after a parameter with a default. 4.2
**kwargs
A parameter with two stars, which collects extra keyword arguments into a dictionary: def order(**quantities). 4.2
local variable
A name in a function's local scope; enclosing bindings can survive through a returned closure. 4.2
nonlocal
A declaration that assignments use a binding in an enclosing function scope, rather than creating a local binding. 4.2
positional argument
An argument matched to a parameter by its position. 4.2
scope
The part of a program where a name can be used. Python looks up names in local, enclosing function, global and built-in scopes; reading an unbound local raises UnboundLocalError. 4.2
signature
A function's name and its parameters, as the documentation lists them: sorted(iterable, /, *, key=None, reverse=False). 4.2
base case
The part of a recursive function that returns without calling the function again: if n <= 1: return 1. 4.3
call stack
The frames of every call that has started and not yet returned, the newest on top. 4.3
recursion
A function calling itself. 4.3
RecursionError
The error Python raises when the call stack goes past its limit, sys.getrecursionlimit(), typically 1000 in our Python session, but configurable. 4.3
recursive case
The part of a recursive function that calls the function on a smaller input: return n * factorial(n - 1). 4.3
closure
A function that retains access to bindings in an enclosing function scope after the outer call returns. 4.4
higher-order function
A function that takes a function as an argument, or returns one: sorted with key=, map, filter. 4.4
iterator
An object that gives its values one at a time, when asked for the next one. map and filter return iterators, which are used up after one pass. 4.4
key function
A function passed as key=, whose result is what sorted, min and max compare. 4.4
lambda
A function written as one expression, without a name: lambda price: round(price * 1.08, 2). 4.4
stable sort
A sort that keeps items with equal keys in their original order. sorted and list.sort are stable. 4.4