Member-only story
9 Hidden Gems of Python’s return Statement Explained with Code
Sep 19, 2024
1. Ends Function Execution:
def example():
return "This ends here"
print("This will never print")
2. Multiple Returns in a Function:
def check_value(x):
if x > 0:
return "Positive"
return "Non-positive"
3. Implicit None Return:
def no_return():
pass
result = no_return()
4. Returning Multiple Values:
def multiple_values():
return 1, 2, 3
a, b, c = multiple_values()
5. Returning Functions (Higher-order functions):
def outer_function():
def inner_function():
return "Hello"
return inner_function
func = outer_function()
print(func())
Hello