Defining and Calling a Function
Use thedef keyword, a name, parentheses, and a colon. The function body must be indented.
snake_case — lowercase words separated by underscores. Choose names that describe what the function does, not how.
Parameters and Arguments
Parameters are the variable names listed in the function definition. Arguments are the actual values you pass when calling the function.Positional Arguments
By default, Python matches arguments to parameters by their position (left to right):Default Parameter Values
Give a parameter a default value to make it optional at call time. Parameters with defaults must come after parameters without defaults:Keyword Arguments
Pass arguments by name to improve readability and ignore order:Return Values
Usereturn to send a value back to the caller. Python exits the function immediately when it hits return.
return returns None.
Returning Multiple Values
Separate multiple return values with commas — Python packs them into a tuple, and you can unpack them on the calling side:Docstrings
A docstring is a string literal on the first line of the function body. It documents what the function does, its parameters, and its return value. IDEs andhelp() display it automatically.
Variable Scope
A variable’s scope is the region of code where it is visible. Python follows the LEGB rule — it searches for a name in this order:- Local — inside the current function
- Enclosing — inside any outer (enclosing) function
- Global — at the top level of the module
- Built-in — Python’s built-in names (
len,print, …)
Modifying a Global Variable
Use theglobal keyword inside a function to write to a module-level variable:
Modifying an Enclosing Variable
Usenonlocal in a nested function to write to a variable in the enclosing function’s scope:
Avoid modifying global state inside functions when possible. Pass values in as arguments and return new values instead — this keeps functions predictable and easy to test.
Lambda Functions
A lambda is a nameless function written as a single expression. Use it for short, throwaway logic — especially as arguments tosorted(), map(), or filter().
Common Lambda Patterns
Use lambdas only for simple one-line expressions. If your logic needs more than one line, write a regular
def function instead.*args and **kwargs
Accept a variable number of arguments using *args (positional) and **kwargs (keyword):
Practical Example: Processing a List of Numbers
The following example brings together parameters, defaults, return values, docstrings, and lambdas to build a small number-crunching utility:Key Takeaways
One responsibility
Each function should do one thing. If you find yourself writing a function that does three unrelated things, split it into three functions.
Parameters over globals
Pass data in through parameters and return new data. Avoid reading or writing global variables inside functions.
Defaults for convenience
Use default parameter values to make common cases easy without removing flexibility for advanced callers.
Docstrings always
Write a docstring for every function that isn’t completely obvious. Future you will be grateful.