Python · Module 4

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.

4of 4 lessons ready
35quiz questions
27key terms
3self-checking notebooks

The lessonsin the order to take them.

4 lessons ready to study
  1. 4.1Defining FunctionsPackage code into reusable functions with inputs and outputs. Open
  2. 4.2Arguments and ScopePass arguments every way Python allows, and know where names live. Open
  3. 4.3RecursionSolve a problem by calling the same function on a smaller version of it. Open
  4. 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.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 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 def statement; 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: price in def with_tax(price):. 4.1
return value
The value a call gives back. A function with no 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
**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 is 1000. 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: 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

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.

  1. all_reviews(section) returns every review under a section, however deeply the sections are nested;
  2. tokenise(review) returns the review's words, lowercase, with ., ,, ! and ? removed;
  3. count_words(tokens, stop_words=frozenset()) returns a dictionary from each word to its count, leaving out any word in stop_words;
  4. top_words(counts, n=5) returns the n most common words as (word, count) pairs, most common first, sorted with sorted and 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.