Returning multiple values in Python
In this tutorial, we will learn how to return multiple values in Python. Returning multiple values in Python is valid and very easy to understand.
Did you use Java or any other programming language before using Python? If yes, what happened when you tried to return multiple values in a function?
Yes! That gave you error. Java, C, C++ etc do not support multiple return values. You may come across various logic where you may incur the need to return multiple values for the proper functioning of the program. Thanks to Python!
Returning multiple values from a function can be done using various different approaches. Here we will see some of them using Python.
Method 1 – Returning a tuple
Given below is an illustration of the following:
#return values in a tuple def retMulValue(): retStr = 'CodeSpeedy' retNum = 14 return retStr, retNum #driver if __name__ == "__main__": strVal, numVal = retMulValue() #call the function print(f"The string value is: {strVal} and the number value is: {numVal}.")
Output
The string value is: CodeSpeedy and the number value is: 14.
Explanation
Inside the ‘retMulValue()’ function, we are initializing two variables with values and returning them as comma(,) separated values which is a tuple.
Method 2 – Returning a list with the Python function
Given below is an illustration of the following:
#return values in a list def retMulValue(): retStr = 'CodeSpeedy' retNum = 14 return [retStr, retNum] #driver if __name__ == "__main__": retVal = retMulValue() #call the function print(f"The string value is: {retVal[0]} and the number value is: {retVal[1]}.")
Output
The string value is: CodeSpeedy and the number value is: 14.
Explanation
We return the values using a list inside the function ‘retMulValue()’.
Method 3 – Returning a dictionary
Given below is an illustration of the following:
#return values in a dictionary def retMulValue(): dicValues = {'StringVal': 'CodeSpeedy', 'NumVal': 14} return dicValues #driver if __name__ == "__main__": retVal = retMulValue() #call the function print(f"The string value is: {retVal['StringVal']} and the number value is: {retVal['NumVal']}.")
Output
The string value is: CodeSpeedy and the number value is: 14.
Explanation
We return the values using a dictionary inside the function ‘retMulValue()’.
How to return multiple values in Python function
Python provides the support of returning multiple values. These are the various ways to perform this task:
- Using Tuple
def calc(a,b): c=a+b d=a*b return c,d ans1,ans2=calc(7,4) print(ans1,ans2)
OUTPUT:
11 28
- Using Object
class Calculate: def calc(self,a,b): self.c=c self.d=d c=a+b d=a*b return c,d ans1,ans2=calc(7,4) print(ans1,ans2)
OUTPUT:
11 28
- Using List
def calc(a,b): c=a+b d=a*b return [c,d] ans1,ans2=calc(7,4) list=calc() print(list)
OUTPUT:
11 28
- Using Dictionary
def calc(a,b): dy=dict() dy['c']=a+b dy['d']=a*b return dy ans1,ans2=calc(7,4) dy=calc() print(dy)
OUTPUT:
11 28
S0, these are the following ways with the help of which you can return different and multiple values in Python. The values can differ in their datatype nature. For example, you can return a string and a number together like this:
def func(): str="Rachna" n=11 return str,n str, n = func() print(str) print(n)
This code will give no error and produce the following output:
Rachna 11
You may also read,
Leave a Reply