Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. List, Tuples and Dictiona...
  5. Tuples
  6. Tuple as return value

Tuple as return value

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments