What does the keyword ‘return’ do in Python?

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.

How to use return statement in Python?

To use a return statement in Python, use the syntax return [expression]. The return statement is important because it allows you to return values from functions and methods.

Syntax

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

Example

def club():
  return 11 + 19


print(club())

Output

30

We have defined a function that returns the sum of two values in this example. This example returns one value.

How to return multiple values in Python

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 multiple values separated by commas. The returned values are tuples 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.

How to return 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. The list is like the array of elements created using 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 a list instead of a tuple.

How to return a dictionary in Python

To return a Dictionary in Python, use the return keyword and write the dictionary you want to return inside the function. The 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}

We initialized an empty dictionary first and then appended the key-value pairs to the Dictionary. And then return the Dictionary.

Conclusion

The return statement makes your functions send Python objects back to the caller code.

Leave a Comment

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