A function is a named block of code that does one job. You write it once and call it from anywhere. Functions are how we keep programs small, readable, and testable.
From this section onwards, every example will include type hints — labels that tell Python what types a function expects and returns. Type hints are optional in Python, but we’ll treat them as required, because they catch bugs before your program runs.
What’s in this section
- Defining and calling functions —
def, parameters, return values - Keyword and default arguments — flexible function calls
*argsand**kwargs— accept any number of arguments- Scope — where variables live and how long they last
- Lambda functions — tiny anonymous functions for one-line jobs
- Type hints and docstrings — write functions other people (and your future self) can understand
Functions are the unit you’ll build everything else from. Take your time here.