In tuple, more than one value can be returned from a function by ‘packing’ the values. The tuple can be then ‘unpacked’ into the variables by using the same number of variables on the left-hand side as there are elements in a tuple.
Example:
Program:
#Function to compute area and perimeter of the square.
def square(r):
area = r * r
perimeter = 4 * r
#Returns a tuple having two elements area and perimeter, i.e. two variables are packed in a tuple
return (area, perimeter)
#end of function
side = int(input("Enter the side of the square: "))
#The returned value from the function is unpacked into two variables area and perimeter
area, perimeter = square(side)
print("Area of the square is:",area)
print("The perimeter of the square is:",perimeter)
OUTPUT:
Enter the side of the square: 4
Area of the square is: 16
The perimeter of the square is: 16