Functions4 lessons and their Study toolkit.
Watch each lesson, answer its quiz, then do its practice. After the last lesson: the module quiz, the project, and the interview questions.
The lessonsin the order to take them.
- 4.1Defining FunctionsPackage code into reusable functions with inputs and outputs. Open
- 4.2Arguments and ScopePass arguments every way Python allows, and know where names live. Open
- 4.3RecursionSolve a problem by calling the same function on a smaller version of it. Open
- 4.4Lambdas and Functions as ValuesPass functions around, the pattern behind `sorted(key=...)` and `apply`. Open
Glossaryevery term the module introduces.
Each with the lesson that introduces it.
- argument
- A value passed to a function in a call:
12.50inwith_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 namespace one call runs in. It holds the parameters and the names the body binds, and is removed when the call returns. 4.1
- function definition
- A
defstatement; running it creates a function object and binds its name. The body does not run until a call. 4.1 - parameter
- The name in a function's definition that a value will be bound to:
priceindef with_tax(price):. 4.1 - return value
- The value a call gives back. A function with no
returngives backNone. 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 **kwargs- A parameter with two stars, which collects extra keyword arguments into a dictionary:
def order(**quantities). 4.2 *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 - local variable
- A name bound inside a function, which exists only during a call. 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 a name up in the local frame, then the global namespace, then the built-ins. 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(), which is1000. 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 - higher-order function
- A function that takes a function as an argument, or returns one:
sortedwithkey=,map,filter. 4.4 - iterator
- An object that gives its values one at a time, when asked for the next one.
mapandfilterreturn iterators, which are used up after one pass. 4.4 - key function
- A function passed as
key=, whose result is whatsorted,minandmaxcompare. 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.
sortedandlist.sortare stable. 4.4
Module quiz15 questions across it all.
Take it after the last lesson. Your first pick on each question is the one that counts.
Project: The Word Counter, as Functionsbuild it without a template.
Stated as a problem, with no step-by-step instructions. Working out the steps is the point.
The task
Write these four functions, in one Colab notebook, each with a docstring and
type hints. 03_module_project.ipynb checks them.
all_reviews(section)returns every review under a section, however deeply the sections are nested;tokenise(review)returns the review's words, lowercase, with.,,,!and?removed;count_words(tokens, stop_words=frozenset())returns a dictionary from each word to its count, leaving out any word instop_words;top_words(counts, n=5)returns thenmost common words as(word, count)pairs, most common first, sorted withsortedand a key.
Then print the top five words for the whole catalogue, with and without the
common words, and the top three for catalogue["kitchen"], without them.
Notebooksthat check your answers.
Open them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
- Notebook 1 · Functions and ArgumentsLessons 4.1 and 4.2 Courses plan
- Notebook 2 · Recursion and Functions as ValuesLessons 4.3 and 4.4 Courses plan
- Notebook 3 · Project: The Word Counter, as FunctionsThe module project, with checks Courses plan
Referencefor revising and for interviews.
The cheat sheet is one page of the module’s terms, rules and gotchas. The interview questions come with model answers.