Python return statement With Example

The python return statement is used to return values from the function. We can use the return statement in a function only. It can’t be used outside of a Python function.

Python Function without return statement

Every function in Python returns something. If the function doesn’t have any return statement, then it returns None.

Output:

Python Function Without Return Statement

 

Python Function Without Return Statement

Python Return Statement Example

We can perform some operation in a function and return the result to the caller using the return statement.

Output:

python return example

 

Python Return Example

Python return statement with expression

We can have expressions also in the return statement. In that case, the expression is evaluated and the result is returned.

Output:

Python Return Statement With Expression

 

Python Return Statement With Expression

Python return boolean

Let’s look at an example where we will return the boolean value of the argument of a function. We will use bool() function to get the boolean value of the object.

Output:

Python Return Boolean

 

Python Return Boolean

Python return string

Let’s look at an example where our function will return the string representation of the argument. We can use the str() function to get the string representation of an object.

Output:

Python Return String

 

Python Return String

Python return tuple

Sometimes we want to convert a number of variables into a tuple. Let’s see how to write a function to return a tuple from a variable number of arguments.

Output:

Python Function Return Tuple

 

Python Function Return Tuple

Further Reading: Python *args and **kwargs

Python function returning another function

We can return a function also from the return statement. This is similar to Currying, which is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.

Output:

Python Return Function

 

Python Return Function

Python function returning outer function

We can also return a function that is defined outside of the function with return statement.

Output:

Python Return Function

 

Python Function Return Outer Function

Python return multiple values

If you want to return multiple values from a function, you can return tuple, list, or dictionary object as per your requirement.

However, if you have to return a huge number of values then using sequence is too much resource hogging operation. We can use yield, in this case, to return multiple values one by one.

Output:

Python Function Return Tuple

 

Python Return vs Yield

Summary

The python return statement is used to return the output from a function. We learned that we can also return a function from another function. Also, expressions are evaluated and then the result is returned from the function.

You can checkout complete python script and more Python examples from our GitHub Repository.

By admin

Leave a Reply