AppDividend
Latest Code Tutorials

Python return: How to Use return in Python

If you want functions to work as expected, every programming language must have a return statement. The return keyword is to exit a function and return a value.

The function is a series of statements that returns some value to a caller. It can also be passed by zero or more parameters that may be used in the execution of the body.

In short, a function “returns some value to the caller“. Now, let’s understand the return statement.

Python return

The return is a built-in Python statement or keyword used to end the execution of a function call and “returns” the result (value of the expression following the return keyword) to the caller.

The statement after the return statement is not executed. If the return statement is without any expression, then None is returned.

Please note that the return statement in Python can not be used outside the function.

Syntax

def method():
    statements
    .
    .
    return [expression]

Example

def club():
    return 11 + 19


print(club())

Output

30

In this example, we have defined a function that returns the sum of two values. This example returns one value.

Python return multiple values.

To return multiple values in Python from a function, we can use the following ways.

  1. Return multiple values by separated commas(tuple)
  2. Return list
  3. Return set
  4. Return dictionary

Return various values by separating commas

To return multiple values by separated commas, use the return statement. In Python, you can return the multiple values separated by commas. The returned values are Python tuple with comma-separated values.

def club():
    return 11, 19, 21, 46


print(club())

Output

(11, 19, 21, 46)

From the output, you can see that the function has returned a tuple containing command-separated values.

In Python, comma-separated values are considered tuples without parentheses, except where required by syntax.

We can also verify its data type.

def club():
    return 11, 19, 21, 46


print(type(club()))

Output

<class 'tuple'>

You can access the item by its index.

def club():
    return 11, 19, 21, 46

data = club()
print(data[2])

Output

21

If you try to access the index that does not exist, it will throw an exception.

def club():
    return 11, 19, 21, 46

data = club()
print(data[20])

Output

Traceback (most recent call last):
  File "app.py", line 5, in <module>
    print(data[20])
IndexError: tuple index out of range

We have got the IndexError: tuple index out of range.

Returning a list in Python.

To return a list in Python, use the return keyword and write the list you want to return inside the function. Python list is like the array of elements created using the square brackets.

Lists are different from arrays as they can contain elements of various types. In addition, lists in Python are other than tuples as they are mutable.

def club():
    str = "AppDividend"
    x = 20
    return [str, x]


data = club()
print(data)

Output

['AppDividend', 20]

In this code, we can see that we have a return list with the help of [ ].

Using [ ] returns list instead of a tuple.

Returning a Dictionary

To return a Dictionary in Python, use the return keyword and write the Dictionary you want to return inside the function. Python Dictionary is similar to a hash or map in other languages.

We can define a dictionary using a dict() method and then specify the key according to values, and we will compose the Dictionary and return the Dictionary.

def club():
    dct = dict()
    dct['str'] = "AppDividend"
    dct['age'] = 3
    return dct


data = club()
print(data)

Output

{'str': 'AppDividend', 'age': 3}

You can see that we have initialized an empty dictionary first and then appended the key-value pairs to the Dictionary. And then return the Dictionary.

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.