Lesson 05
Python return Statement — Explicit, Implicit, and Multiple Return Values
Learn how return exits a Python function, sends values back to the caller, returns None implicitly, and packs multiple values into tuples.
What you will learn
- What
returndoes: stop the function and send a value back - Explicit return, implicit
None, and barereturn - The early return pattern
- Returning any object, including multiple values
- The difference between
print()andreturn
What is return?
A return statement does two jobs at the same time:
- Stops the function immediately
- Sends a value back to the caller
This continues from Local and Global Variables.
def add(a, b):
return a + b
result = add(10, 20)
print(result)Output:
30Explicit return
An explicit return means you deliberately send a value back.
def return_42():
return 42
x = return_42()
print(x)Returned values are reusable anywhere an expression is valid:
print(return_42()) # 42
print(return_42() * 2) # 84
print(return_42() + 8) # 50return is only valid inside a def (or lambda). Outside a function it is a SyntaxError.
return immediately stops execution
This is one of the most important rules.
def my_function():
print("Start")
return 6
print("End") # never executes
print(my_function())Output:
Start
6Everything after return on the same path becomes dead code.
Early return pattern
Professional developers use return to exit early when input is invalid.
def safe_divide(a, b):
if b == 0:
return "Cannot divide"
return a / b
print(safe_divide(10, 2))
print(safe_divide(10, 0))Output:
5.0
Cannot divideImplicit return (None)
If you forget return, Python automatically returns None.
def add_one(x):
y = x + 1
r = add_one(5)
print(r)Output:
NoneBare return
A return can exist without a value. It still exits immediately and sends None.
def greet(name):
if not name:
return
print("Hello", name)
greet("Alice")
print(greet(""))| Input | Output |
|---|---|
"Alice" |
Hello Alice |
"" |
Returns None |
Return any Python object
Everything in Python is an object — so everything can be returned.
def student():
return {"id": 101, "name": "Alice"}
info = student()
print(info["name"])Output:
AliceYou can also return a function:
def multiplier(factor):
return lambda x: x * factor
double = multiplier(2)
print(double(5)) # 10Returning multiple values
Python does not really return multiple values — it returns one tuple.
def stats():
return 10, 20, 30
x, y, z = stats()
print(x)
print(z)Output:
10
30Python packs on return and unpacks on assignment.
| Packing | Unpacking | |
|---|---|---|
| Idea | Many values become one tuple | Tuple becomes many variables |
| Code | return 1, 2, 3 |
a, b, c = stats() |
| Result | (1, 2, 3) |
a=1 b=2 c=3 |
print() vs return
This is the #1 beginner mistake.
def double(x):
print(x * 2)
print(double(5))Output:
10
Noneprint() shows a value on screen. It does not send it back. Using return:
def double(x):
return x * 2
print(double(5)) # 10 — and the caller can reuse itMemory rule: Print displays. Return delivers.
Real project example
def calculate_price(base, tax):
total = base + tax
return total
price = calculate_price(500000, 25000)
print(price)Why return instead of print? Another function may need the value:
invoice = calculate_price(500000, 25000)
# save_to_database(invoice)
print("Invoice total:", invoice)Reusable code always favors return.
Common mistakes
| Mistake | Problem | Fix |
|---|---|---|
Forget return |
Returns None |
return value |
Code after return |
Never executes | Move it above |
return outside a function |
SyntaxError |
Use inside def |
print(print()) |
Inner print returns None |
Return the value instead |
Classic trap:
def compute(x):
print(x * 2)
value = compute(5)
print(value)Output:
10
NoneCorrect:
def compute(x):
return x * 2
value = compute(5)
print(value) # 10Interview cheat sheet
| Question | Answer |
|---|---|
What does return do? |
Exit + send a value |
No return statement? |
Returns None |
Bare return? |
Returns None |
Code after return? |
Dead code |
| Can you return a list or dict? | Yes |
| Can you return a function? | Yes |
| Multiple values? | One tuple |
Mini challenge
Predict the output without running it:
def demo(x):
if x > 5:
return x * 2
return
a = demo(10)
b = demo(3)
print(a)
print(b)demo(10)→ returns20demo(3)→ hits barereturn→None
Output:
20
NoneIf you understand explicit return, implicit None, early exit, and tuple return, you have mastered return.
Practice in the playground
The playground at the bottom already runs explicit return, early exit, None, packing, and print vs return. Try:
- Change
return_42()toreturn 7and confirm* 2prints14. - Swap
safe_divide(10, 0)tosafe_divide(9, 3)— expect3.0. - Replace
returnindouble_returnwithprintand watchNoneappear.
Next: Lambda Functions.
Loading playground…
Check your understanding
Return statement quiz
What does the `return` keyword do in a function?
1 / 10