Functions and Modules
Core Python

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.

  1. 1Python FunctionsA function is a named, reusable block of code that takes inputs (arguments), does some work, and can hand a r…
  2. 2Python Function ArgumentsA function argument is the value you pass in a call, and Python matches each argument to a parameter either b…
  3. 3Python *args and **kwargs*args collects any extra positional arguments into a tuple and **kwargs collects any extra keyword arguments …
  4. 4Python Lambda FunctionsA lambda is a small anonymous function written as a single expression, lambda parameters: expression, that re…
  5. 5Python RecursionRecursion is when a function solves a problem by calling itself on a smaller version of that problem, stoppin…
  6. 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…
  7. 7Python ClosuresA closure is a function that remembers the variables of the enclosing function where it was created, so it ca…
  8. 8Python DecoratorsA decorator is a function that takes another function, wraps extra behaviour around it and returns the wrappe…
  9. 9Python Type HintsA type hint is a label on a parameter, return value or variable that says what type is expected; Python store…
  10. 10Python Modules and ImportsA module is a file of Python code (functions, classes and values) that you load into your program with import…
  11. 11Python math and statisticsPython's built-in operators, the math module and the statistics module handle everyday calculations such as r…
  12. 12Python random ModuleThe random module draws pseudo-random numbers and picks from a seeded generator, so you can simulate dice, sh…
  13. 13Python datetime ModuleThe datetime module gives you date, time, datetime and timedelta objects that understand the calendar, so you…

Start with Python Functions →

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