In this Python tutorial, we’ll learn about the role of Python return statement in Python programs. We’ll understand it using practical Python code examples.
- Introduction: Python Return Statement
- Syntax of Return Statement In Python
- Implementing Python Return Statement (Easy Examples)
- Example 1: Not Using Python Return Statement
- Example 2: Return Without Value
- Example 3: Return With Value
- Example 4: Code After Return Statement
- Example 5: Multiple Return Statements
- Example 6: Return List After Addition Of Values
Introduction: Python Return Statement
As the name suggests, it’s used to return something. In Python, we’ve functions that are used to perform some specific actions. Click here if you want to learn about them in detail. Sometimes we want those functions to perform a specific actions and return the result to where it’s been called. That’s where Python return statement plays its role.
By default, None is returned but we can return a specific value from function using this return statement. Let’s now understand it using practical Python code examples.
Syntax of Return Statement In Python
def fun(): return someValue
- The keyword ‘return‘ is necessary.
- Using only the keyword will return None.
- Not using this keyword will also return None.
- We can pass any type of value using this return statement.
Implementing Python Return Statement (Easy Examples)
Below examples will demonstrate how to properly use return statement in Python.
Example 1: Not Using Python Return Statement
def func(): print('Simple fun') print(func())
Simple fun None
We’ve created a simple function having a print function which will print a simple statement. After that, we’ve called this function and have used it inside a print function to see what it’ll return. As a result, function is executed first which displayed the message and after that, it returned None.
We can also store the returned value inside a variable. See below:
returnedVal = func() print( returnedVal )
Simple fun None
Example 2: Return Without Value
def fun(): return print(fun())
We can see that using only a return keyword without value will also result in None returned by it.
Example 3: Return With Value
def add(): return 5+6 print( add() )
We can see that the sum of two integers has been returned using the return statement. We can also return it using the below method. See below:
def add(): a=5 b=6 sum = a + b return sum print( add() )
We can return a list, tuple, dictionary, string etc. using Python return statement. See below examples.
def fun(): return 'String returned' print(fun())
Let’s now return a list. See below code:
def fun(): return [1,2,5,6] print(fun())
[1, 2, 5, 6]
You can try it with other values as well.
Example 4: Code After Return Statement
def sum(): a=45 b=3 sum=a + b return sum sum=87 print(sum())
We’ve assigned a value to variable ‘sum’ after the return statement but still the value above return statement is returned. So that means we should always use the return statement in the end of our Python function.
Example 5: Multiple Return Statements
Using multiple return statement is of no use as the value of only the first return statement will be returned even if its None. See below:
def funct(): return 'Simple value' return 'Return it' return [1,2,3,4] print(funct())
Example 6: Return List After Addition Of Values
def fun(a,b): lst= for val in a: for vall in b: lst.append(val+vall) return lst returnVal=fun( [1,2,3,4] , [22,5,8.6,98.45] ) print( returnVal )
[23, 6, 9.6, 99.45, 24, 7, 10.6, 100.45, 25, 8, 11.6, 101.45, 26, 9, 12.6, 102.45]
We’ve created a function that have two parameters. We’ve passed it two Python lists. Inside this function, we’ve created an empty list which will store the values after addition. We’ve created a logic using nested Python for loop which will add each item of first list to every item of second list and store it in a new list. After that, we’ve returned the list using return statement, stored the value in a variable and displayed it.
So this is how we can use Python return statement. Don’t hesitate to share it with your squad of Python programmers.
Also, don’t hold back your queries regarding Python return statement, if you still have any. We’ll be very glad to solve all your queries.
In conclusion, now you have an in-depth practical knowledge of how to use Python return statement. We’d love to receive your valuable feedback on this post.
Do visit our other posts on Python programming. Thank you for reading this one.