foundation Estimated learning time: 5 hCommon in syllabi

Included in at least one reviewed higher-education syllabus.

2.3 Functions, scope and arguments

You can write functions others can call safely.

Before:00. Orientation & SetupUnlocks:03. Data Handling & Analysis04. Classical AI — Agents, Search & Knowledge Representation

Functions are where code stops being a script and starts being reusable, and Python's argument system — defaults, star-args, keyword arguments — is richer than most languages'. Scope and closures explain a whole family of confusing behaviours at once. It sits early because everything after is built from functions. The classic bug gets its own item: a mutable default argument is created once and shared across every call, which is never what the author meant.

Work through these

  • Positional, keyword, default, *args, **kwargs

    The several ways a function can accept arguments, including collecting extra positional and keyword arguments. Knowing the order they must be declared in prevents a class of confusing errors.

  • Scope, closures and the mutable-default bug

    Where a name is visible, functions that capture values from their surroundings, and the well-known trap of a mutable value used as a default. That trap catches everyone once.

  • Lambdas, map/filter, and when not to use them

    Small anonymous functions and the functional operations that pair with them, along with the cases where a comprehension is clearer. Python's own style guidance is relevant here.

  • Docstrings and readable signatures

    A short description inside the function and a signature that says what it needs. These are what let somebody else call your function without reading its body.

Sign in to keep your progress.

Free resources

Links last checked 29 Aug 2026.

Stuck here?

Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.

Checking your session…

Topics shown in module order.