Python Return Statements
Table of Contents
Description
The return statement is used in a Python function to send back a result to the caller. It marks the end of the function execution and can return:
A single value
Multiple values (as a tuple)
No value (None by default)
Prerequisites
- Understanding of functions
- Familiarity with data types
- Concept of function call and response
Examples
Here's a simple program in Python:
✅ Returning a Single Value
def square(x):
# Returns the square of a number
return x * x
print(square(5)) # Output: 25
✅ Returning Multiple Values
def operations(a, b):
# Returns sum, difference, and product of two numbers
return a + b, a - b, a * b
result = operations(10, 5)
print(result) # Output: (15, 5, 50)
✅ Return Without a Value (defaults to None)
def say_hello():
print("Hello!")
return # No value is returned explicitly
x = say_hello()
print(x) # Output: None
✅ Using Returned Values in Expressions
def get_price():
return 250
total = get_price() * 2 # Function return used in calculation
print("Total price:", total) # Output: Total price: 500
Real-World Applications
APIs return JSON responses using return
Machine Learning: Return predictions or evaluation metrics
Data Analysis: Return processed results or stats from functions
Banking Apps: Functions return balances, interest, etc.
Games: Functions return score, status, or object states
Where topic Can Be Applied
Web backends (Flask, Django): Returning HTTP responses
Data processing pipelines: Return transformed data
Script automation: Return system status or results
Embedded systems: Return sensor or state values
NLP and AI: Return processed tokens, embeddings, outputs
Resources
Topic video source
A comprehensive video
Watch
Python pdf
pdf on topic
Visit
Interview Questions
What is the purpose of a return statement in a function?
Can a Python function return multiple values?
What happens if you don’t use a return statement?
Can you return a function from another function?
How is the return keyword different from print()?