03-21-2022, 11:16 AM
Return Keyword in Python – A Simple Illustrated Guide
![[Image: return-1-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2021/01/return-1-1024x576.jpg)
Python’s keyword commands the execution flow to exit a function immediately and return a value to the caller of the function. You can specify an optional return value—or even a return expression—after the keyword. If you don’t provide a return value, Python will return the default value to the caller.
Python Return Keyword Video
Return Keyword Followed by Return Value
Here’s an example of the return keyword in combination with a return value:
Within function, Python returns the result 4 to the caller. The function then prints the output to the shell.
Return Keyword Followed by Return Expression
Here’s an example of the return keyword in combination with a return expression:
Within function, Python evaluates the expression and returns the result 4 to the caller. The function then prints the output to the shell.
Return Keyword Followed by No Value
Here’s an example of the return keyword without defining a return value:
Within function, Python returns the default value to the caller. The function then prints the output to the shell.
Interactive Code Shell
Run the following code in your browser:
https://trinket.io/embed/python/e0d733e100
Exercise: Change the three return values to 42, 42, and ‘Alice’ in the interactive code shell!
The post Return Keyword in Python – A Simple Illustrated Guide first appeared on Finxter.
![[Image: return-1-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2021/01/return-1-1024x576.jpg)
Python’s
Code:
returnCode:
returnCode:
NonePython Return Keyword Video
Return Keyword Followed by Return Value
Here’s an example of the return keyword in combination with a return value:
Code:
def f(): return 4 print(f())
# OUTPUT: 4Within function
Code:
f()Code:
print()Return Keyword Followed by Return Expression
Here’s an example of the return keyword in combination with a return expression:
Code:
def f(): return 2+2 print(f())
# OUTPUT: 4Within function
Code:
f()Code:
2+2=4Code:
print()Return Keyword Followed by No Value
Here’s an example of the return keyword without defining a return value:
Code:
def f(): return print(f())
# OUTPUT: NoneWithin function
Code:
f()Code:
NoneCode:
print()Interactive Code Shell
Run the following code in your browser:
https://trinket.io/embed/python/e0d733e100
Exercise: Change the three return values to 42, 42, and ‘Alice’ in the interactive code shell!
The post Return Keyword in Python – A Simple Illustrated Guide first appeared on Finxter.

