Return Multiple Values from Function
Return Multiple Values from Function
We can return multiple values from a function in Python using return statement. The multiple values must be mentioned after return keyword, and the values must be separated by comma.
The syntax to return multiple values, say a
, b
, and c
from a function is
def someFunction():
#some code
return a, b, c
In this tutorial, we will learn how to return multiple values from a function in Python, with the help of examples.
Example
In the following program, we write swap()
function that can take two arguments, swap the values in the variables, and return both of them.
Python Program
#function that returns multiple values
def swap(x, y):
temp = x
x = y
y = temp
return x, y
# Take two values
a = 10
b = 20
print(f'\nBefore Swap - (a, b) : ({a}, {b})')
#swap a, b
a, b = swap(a, b)
print(f'\nAfter Swap - (a, b) : ({a}, {b})')
Output
Enter a : 5
Enter b : 9
Before Swap - (a, b) : (5, 9)
After Swap - (a, b) : (9, 5)
Summary
In this tutorial of Python Examples, we learned how to return multiple values from a function, with the help of well detailed example programs.