🔄 Python Return Values — Sending Results Back from Functions
Introduction 🌟
The return statement is used to send a value back to the caller of a function. It is one of the most important concepts in Python functions, allowing your code to become modular and reusable.
Note
💡 A function without a return statement automatically returns None.
1. Basic Return Statement 🧱
basic_return.py
def add(a, b):
return a + b
result = add(10, 20)
print(result)✔️ return sends the value back to where the function was called.
2. Functions Without Return Value ➡️ None
none_return.py
def greet():
print("Hello!")
result = greet()
print(result) # Output: NoneNote
💡 If no return is specified, the function returns None.
3. Returning Multiple Values 🎁🎁
Python allows returning multiple values, which are packed into a tuple.
multiple_values.py
def calc(a, b):
return a + b, a - b, a * b
x, y, z = calc(10, 5)
print(x, y, z)Note
✔️ Multiple return values improve flexibility and reduce repeated function calls.
4. Returning Lists, Tuples, Dictionaries 📦
Returning a List
return_list.py
def squares(n):
return [x*x for x in range(1, n+1)]
print(squares(5))Returning a Dictionary
return_dict.py
def student_record(name, age):
return {"name": name, "age": age}
print(student_record("Sathish", 25))Returning a Tuple
return_tuple.py
def stats(numbers):
return min(numbers), max(numbers)
print(stats([10, 20, 30, 40]))5. Using Return to Exit a Function Early ⏹️
return_exit.py
def check_age(age):
if age < 18:
return "Not allowed"
return "Access granted"
print(check_age(16))Note
✔️ return stops the function immediately.
6. Return with Conditional Logic 🔀
conditional_return.py
def grade(score):
if score >= 90:
return "A"
elif score >= 75:
return "B"
elif score >= 50:
return "C"
return "Fail"
print(grade(82))7. Return Inside Loops 🔁
loop_return.py
def find_first_even(nums):
for n in nums:
if n % 2 == 0:
return n
return "No even number found"
print(find_first_even([1, 3, 7, 8, 9]))8. Returning Functions (Higher-Order Functions) 🧠
return_function.py
def outer():
def inner():
return "Hello from inner"
return inner # returning function itself
fn = outer()
print(fn())Note
✔️ Useful in decorators and functional programming.
9. Returning Lambda Expressions ⚡
return_lambda.py
def multiplier(n):
return lambda x: x * n
double = multiplier(2)
print(double(5))10. Common Mistakes ⚠️
- Returning inside a loop too early (accidentally stopping execution).
- Forgetting to return a value (result becomes None).
- Writing code after return — it will never run.
unreachable_code.py
def test():
return "Done"
print("This will never execute")11. Real-World Examples 🌍
Login validation
login_example.py
def login(username, password):
if username == "admin" and password == "1234":
return True
return False
print(login("admin", "1234"))Billing System
billing_example.py
def bill(items):
return sum(items)
print(bill([100, 200, 50]))Prime Number Checker
prime_checker.py
def is_prime(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
print(is_prime(17))Conclusion 🎉
>>“Return values give functions purpose — they send results back and make code truly reusable.” ✨
You now fully understand return values in Python! Want the next topic? Try Lambda Functions, Recursion, Modules, or OOP (Classes & Objects). Just tell me! 😊