Functions and Modules
Turn repeated code into reusable tools, then go deeper: flexible arguments, closures and decorators, type hints, and the math, random and datetime modules.
- 1Python FunctionsA function is a named, reusable block of code that takes inputs (arguments), does some work, and can hand a r…✓
- 2Python Function ArgumentsA function argument is the value you pass in a call, and Python matches each argument to a parameter either b…✓
- 3Python *args and **kwargs*args collects any extra positional arguments into a tuple and **kwargs collects any extra keyword arguments …✓
- 4Python Lambda FunctionsA lambda is a small anonymous function written as a single expression, lambda parameters: expression, that re…✓
- 5Python RecursionRecursion is when a function solves a problem by calling itself on a smaller version of that problem, stoppin…✓
- 6Python Variable ScopeScope is the region of a program where a name can be seen; Python looks a name up in the Local, Enclosing, Gl…✓
- 7Python ClosuresA closure is a function that remembers the variables of the enclosing function where it was created, so it ca…✓
- 8Python DecoratorsA decorator is a function that takes another function, wraps extra behaviour around it and returns the wrappe…✓
- 9Python Type HintsA type hint is a label on a parameter, return value or variable that says what type is expected; Python store…✓
- 10Python Modules and ImportsA module is a file of Python code (functions, classes and values) that you load into your program with import…✓
- 11Python math and statisticsPython's built-in operators, the math module and the statistics module handle everyday calculations such as r…✓
- 12Python random ModuleThe random module draws pseudo-random numbers and picks from a seeded generator, so you can simulate dice, sh…✓
- 13Python datetime ModuleThe datetime module gives you date, time, datetime and timedelta objects that understand the calendar, so you…✓
About Functions and Modules
Functions let you name a piece of logic and reuse it. This topic covers defining functions, positional, keyword and default arguments, *args and **kwargs, lambda, recursion, scope, closures, decorators and type hints, then shows how modules and the standard library share code between files. Read the lessons in order, because closures and decorators rely on scope.
Related reference: print vs return · Functions cheat sheet · Built-in functions
Further reading (official documentation)
Practice for this topic
Python Functions exercises
4 exercises · checked in your browser
Python Function Arguments exercises4 exercises · checked in your browser
Python *args and **kwargs exercises4 exercises · checked in your browser
Python Lambda Functions exercises4 exercises · checked in your browser
Python Recursion exercises4 exercises · checked in your browser
Python Variable Scope exercises4 exercises · checked in your browser
Python Closures exercises4 exercises · checked in your browser
Python Decorators exercises4 exercises · checked in your browser
Python Type Hints exercises4 exercises · checked in your browser
Python Modules and Imports exercises4 exercises · checked in your browser
Python math and statistics exercises4 exercises · checked in your browser
Python random Module exercises4 exercises · checked in your browser
Python datetime Module exercises4 exercises · checked in your browser