Lesson 5 of 19 · 2 min
The return Statement — Getting Results Back
print() inside a function only displays something on screen — the value disappears afterwards. return sends the value back to the caller so it can be stored, used in calculations, or passed to another function.
def add(a, b):
result = a + b # calculate
return result # send the result back to whoever called this function
total = add(5, 3) # add() returns 8 → stored in total
print(total) # 8
grand = add(5, 3) + add(10, 20) # use returned values in expressions
print(grand) # 38
print(f"5 + 3 = {add(5, 3)}") # embed function call in an f-string
Output: 8 38 5 + 3 = 8
print vs return — The Most Important Distinction
def bad_multiply(a, b):
print(a * b) # shows result but RETURNS NOTHING (implicitly returns None)
def good_multiply(a, b):
return a * b # sends result back to caller
x = bad_multiply(4, 5) # prints 20, stores None in x
print(x) # None — the value was lost!
y = good_multiply(4, 5) # stores 20 in y, nothing printed yet
print(y) # 20 — value preserved for use
Output: 20 None 20
Rule: Use
returnwhen you need the result later. Useprint()only when the function's entire job is display (like a report formatter).
Returning Multiple Values
def min_max_avg(numbers):
mn = min(numbers) # smallest number in the list
mx = max(numbers) # largest number in the list
avg = sum(numbers) / len(numbers) # arithmetic mean
return mn, mx, avg # Python packs these into a tuple automatically
scores = [85, 92, 78, 65, 88, 95]
low, high, mean = min_max_avg(scores) # unpack all three return values at once
print(f"Lowest : {low}") # 65
print(f"Highest: {high}") # 95
print(f"Average: {mean:.1f}") # 83.8 — .1f means 1 decimal place
Output: Lowest : 65 Highest: 95 Average: 83.8
