Py

Functions

Module 8 of 14, Intermediate
🧩

Functions

Package code into reusable functions: parameters, return values, defaults, *args and **kwargs, scope, lambdas and docstrings.

0 of 6 lessons complete

6 lessons, 60 min

Start module

Lessons

1

Defining your own functions

Name a block of code with def and run it whenever you need it.

9 min
2

Returning values

Send results back with return, and avoid the print-vs-return trap.

10 min
3

Default and keyword arguments

Make parameters optional and calls readable, and dodge the mutable default trap.

11 min
4

*args and **kwargs

Accept any number of arguments, and spread lists and dicts into calls.

9 min
5

Scope: where variables live

Local vs global variables, UnboundLocalError, and why returning beats global.

10 min
6

Lambdas, docstrings and functions as values

Pass functions around, write one-line lambdas and document your code.

11 min
⚡
Next module

Comprehensions & Iteration