Syntax
return valueExamples
Basic Return
Returning a single computed value from a function.
def square(n):
return n ** 2
result = square(6)
print(result) # 36Returning Multiple Values
Python lets you return several values at once - they are automatically packed into a tuple.
def get_min_max(numbers):
return min(numbers), max(numbers)
low, high = get_min_max([4, 1, 9, 2, 7])
print(f"Min: {low}, Max: {high}")Early Return
Using return to exit a function as soon as a condition is met, avoiding deeply nested code.
def get_discount(is_member, total):
if not is_member:
return 0 # exit early, no discount
if total > 100:
return 0.2
return 0.1
print(get_discount(True, 150)) # 0.2
print(get_discount(False, 150)) # 0Function Without Explicit Return
A function with no return statement implicitly returns None.
def log(message):
print(f"LOG: {message}")
# no return statement here
result = log("Server started")
print(result) # NoneBest practices
- Use early returns to handle edge cases at the top of a function, keeping the main logic less deeply nested
- Return multiple values as a tuple when they are closely related, rather than using a list or dictionary for just two or three values
- Be consistent about what a function returns - avoid returning a value in some branches and None (implicitly) in others, as it can confuse callers
- A function stops executing the moment return runs - any code written after it in the same branch is unreachable
At a glance
- Purpose
- Scripting and general-purpose applications
- File extension
- .py
- Runs in
- Python interpreter
- Usually used with
- Python standard library and packages
Specifications & further reading
Related Python documentation
def
The def keyword in Python is used to define functions - reusable blocks of code that perform specific tasks. Functions are fundamental to organizing code, avoiding repetition, and making programs more maintainable. A function can accept inputs (parameters), perform operations, and return outputs. Functions are one of the core building blocks of clean, modular Python code.lambda
A lambda is a small, anonymous, single-expression function, created without the def keyword or a name. Lambdas are restricted to a single expression whose result is automatically returned - they can't contain multiple statements or assignments. They're most useful as short, throwaway functions passed as arguments to other functions like sorted(), map(), and filter().*args and **kwargs
*args and **kwargs let a function accept a variable number of arguments. *args collects any extra positional arguments into a tuple, while **kwargs collects extra keyword arguments into a dictionary. The names 'args' and 'kwargs' are just convention - what matters is the * and ** prefixes. This pattern is common in wrapper functions, decorators, and APIs that need to stay flexible about their inputs.Decorators
A decorator is a function that wraps another function to extend or modify its behavior, without changing its actual source code. Decorators use the @decorator_name syntax placed directly above a function definition, which is shorthand for passing the function into the decorator and reassigning the result. They're widely used for logging, timing, access control, caching, and validation.
The def keyword in Python is used to define functions - reusable blocks of code that perform specific tasks. Functions are fundamental to organizing code, avoiding repetition, and making programs more maintainable. A function can accept inputs (parameters), perform operations, and return outputs. Functions are one of the core building blocks of clean, modular Python code.lambda
A lambda is a small, anonymous, single-expression function, created without the def keyword or a name. Lambdas are restricted to a single expression whose result is automatically returned - they can't contain multiple statements or assignments. They're most useful as short, throwaway functions passed as arguments to other functions like sorted(), map(), and filter().*args and **kwargs
*args and **kwargs let a function accept a variable number of arguments. *args collects any extra positional arguments into a tuple, while **kwargs collects extra keyword arguments into a dictionary. The names 'args' and 'kwargs' are just convention - what matters is the * and ** prefixes. This pattern is common in wrapper functions, decorators, and APIs that need to stay flexible about their inputs.Decorators
A decorator is a function that wraps another function to extend or modify its behavior, without changing its actual source code. Decorators use the @decorator_name syntax placed directly above a function definition, which is shorthand for passing the function into the decorator and reassigning the result. They're widely used for logging, timing, access control, caching, and validation.