Table of Contents
show
Introduction
- Tuples can be returned by the function as return values
- Generally, the function returns only one value but by returning tuple, a function can return more than one value
Example: to return quotient and reminder when computing division with two integers
def division(a,b):
quotient = a / b
remainder = a % b
return quotient, remainder
n1 = int(input("Enter number 1"))
n2 = int(input("Enter number 2"))
tuple1=division(n1,n2)
print("tuple1:",tuple1)
print("Type of tuple1:", type(tuple1))
Output
Enter number 1 10
Enter number 2 3
tuple1: (3.3333333333333335, 1)
Type of tuple1: <class 'tuple'>
Example: to return maximum and minimum element in a tuple
def max_min(t):
return max(t),min(t)
a=(1,-1,0,10,50,20,-20)
tup = max_min(a)
(max,min) = tup
print("Maximum = ", max)
print("Minimum = ", min)
Output
Maximum = 50
Minimum = -20
Views: 0