Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python return Statement

  • The python return statement is used in a function to return something to the caller program.
  • We can use the return statement inside a function only.
  • In Python, every function returns something. If there are no return statements, then it returns None.
  • If the return statement contains an expression, it’s evaluated first and then the value is returned.
  • The return statement terminates the function execution.
  • A function can have multiple return statements. When any of them is executed, the function terminates.
  • A function can return multiple types of values.
  • Python function can return multiple values in a single return statement.

Python return Statement Example

Let’s look at a simple example to add two numbers and return the total to the caller.

def add(x, y):
    total = x + y
    return total

We can optimize the function by having the expression in the return statement.

def add(x, y):
    return x + y

Every Function returns Something

Let’s see what is returned when a function doesn’t have a return statement.

>>> def foo():
...     pass
... 
>>> 
>>> print(foo())
None
>>>
Python Return None
Python Return None

What happens when the return statement has nothing?

When the return statement has no value, the function returns None.

>>> def return_none():
...     return
... 
>>> print(return_none())
None
>>> 

Python Functions can have multiple return statements

def type_of_int(i):
    if i % 2 == 0:
        return 'even'
    else:
        return 'odd'

Python Function can return multiple types of values

Unlike other programming languages, python functions are not restricted to return a single type of value. If you look at the function definition, it doesn’t have any information about what it can return.

Let’s look at an example where the function will return multiple types of values.

def get_demo_data(object_type):
    if 'str' == object_type:
        return 'test'
    elif 'tuple' == object_type:
        return (1, 2, 3)
    elif 'list' == object_type:
        return [1, 2, 3]
    elif 'dict' == object_type:
        return {"1": 1, "2": 2, "3": 3}
    else:
        return None

Returning Multiple Values in a single return Statement

We can return multiple values from a single return statement. These values are separated by a comma and returned as a tuple to the caller program.

def return_multiple_values():
    return 1, 2, 3


print(return_multiple_values())
print(type(return_multiple_values()))

Output:

(1, 2, 3)
<class 'tuple'>
Python Return Multiple Values
Python return Multiple Values

Python return statement with finally block

When return statement is executed inside a try-except block, the finally block code is executed first before returning the value to the caller.

def hello():
    try:
        return 'hello try'
    finally:
        print('finally block')


def hello_new():
    try:
        raise TypeError
    except TypeError as te:
        return 'hello except'
    finally:
        print('finally block')


print(hello())
print(hello_new())

Output:

finally block
hello try
finally block
hello except

If the finally block has a return statement, then the earlier return statement gets ignored and the value from the finally block is returned.

def hello():
    try:
        return 'hello try'
    finally:
        print('finally block')
        return 'hello from finally'


print(hello())

Output:

finally block
hello from finally

References: