Python return Statement with Examples

How to use the Python return Statement

In this tutorial, you'll learn how to use the Python return statement with examples.

What is the return statement in Python?

A return statement is a statement that occurs in a function definition used to return a value to a function call.

The return Statement Syntax

The following illustrates the syntax of the return statement:

def function_name:

return [expression_list]

Remarks

  • The express_list can be a variable or a function call.
  • If the expression_list omitted, the function call returns None, otherwise, returns a value.

Examples

Example 1:

Sums two numbers and returns a total:

#sums 2 numbers
def sum(a, b):
    return a + b

total = sum(100200)
print(total)

Output:

300

Example 2:

Returns a value of an expression of comparing two numbers:

#compares 2 numbers
def compare(a, b):
    return a > b
print(compare(1020))

Output:

False

Example 3:

Returns None:

#returns None
def get_name(name):
    return
print (get_name("James"))

The function get_name returns None since the express_list omitted.

Example 4:

Calls one function inside another function:

#call one function inside another function
def sum1(a, b):
    return a + b

def sum2(a, b, c):
    return sum1(a, b) + c

print(sum2(102030))

Output:

60

Example 5

#returns OK or NG
def example_if(a):
    if a > 10:
        return "OK"
    print("Print this line if a <= 10")
    return "NG"
print(example_if(10))

Output:

Print this line if a <= 10
NG

If you change the value of parameter a to something else that is larger than 10, the output will look like this:

OK

Read more:

In this tutorial, you've learned how to use the Python return statement.


See also:
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python
Python max() Function – Find the Largest Value in Python

Leave a Comment