Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Return Keyword in Python – A Simple Illustrated Guide

#1
Return Keyword in Python – A Simple Illustrated Guide

Python return keyword - visual example

Python’s return 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 return keyword. If you don’t provide a return value, Python will return the default value None 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:

def f(): return 4 print(f())
# OUTPUT: 4

Within function f(), Python returns the result 4 to the caller. The print() 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:

def f(): return 2+2 print(f())
# OUTPUT: 4

Within function f(), Python evaluates the expression 2+2=4 and returns the result 4 to the caller. The print() 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:

def f(): return print(f())
# OUTPUT: None

Within function f(), Python returns the default value None to the caller. The print() function then prints the output to the shell.

Interactive Code Shell


Run the following code in your browser:

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.



https://www.sickgaming.net/blog/2021/01/...ted-guide/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Tuple Concatenation: A Simple Illustrated Guide xSicKxBot 0 1,941 08-21-2023, 10:25 AM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,607 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] pvlib Python: A Comprehensive Guide to Solar Energy Simulation xSicKxBot 0 1,289 04-30-2023, 07:11 AM
Last Post: xSicKxBot
  [Tut] Python Container Types: A Quick Guide xSicKxBot 0 1,399 04-29-2023, 12:20 PM
Last Post: xSicKxBot
  [Tut] Python ? Put Legend Outside Plot ? – Easy Guide xSicKxBot 0 1,465 04-22-2023, 11:08 PM
Last Post: xSicKxBot
  [Tut] Python f-Strings — The Ultimate Guide xSicKxBot 0 1,425 04-10-2023, 07:38 AM
Last Post: xSicKxBot
  [Tut] Python Regex Capturing Groups – A Helpful Guide (+Video) xSicKxBot 0 1,416 04-07-2023, 10:07 AM
Last Post: xSicKxBot
  [Tut] PIP Install Django – A Helpful Illustrated Guide xSicKxBot 0 1,262 03-12-2023, 05:27 AM
Last Post: xSicKxBot
  [Tut] 10 Simple Ideas – Coding Projects to Create Real Value xSicKxBot 0 1,250 01-02-2023, 12:57 PM
Last Post: xSicKxBot
  [Tut] A Comprehensive Guide to maxsplit in Python xSicKxBot 0 1,246 12-10-2022, 07:55 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016