# A Python program returning multiple values from a method using tuple
# Function is defined that returns a tuple
def fun():
str = "Demo"
x = 20
return str, x; # Returning a tuple
# Driver code to test above method
str, x = fun() # Assign returned tuple
print(str)
print(x)
Output:
Demo
20
Explanation of above program:
A tuple is comma seperated values enclosed within () or without (). The values are of different datatypes but tuple is immutable that is values within tuple cannot be changed via indexes. The value can be accessed via key or index. In above program, a string and integer variable is defined which are then returned from fun(). When fun() is called tuple is received into two variables and thus printing their values. The other method of printing values could be receiving tuple in a variable and printing values via indexes i.e
tuple= fun() # Assign returned tuple
print(tuple[0])
print(tuple[1])
4 Comment(s)