Member-only story

9 Hidden Gems of Python’s return Statement Explained with Code

Python Coding
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

6. Return in init Method:

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

No responses yet