Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. List, Tuples and Dictiona...
  5. Tuples
  6. Variable Length Argument Tuples

Variable Length Argument Tuples

Introduction

  • Variable number of arguments can also be passed to a function
  • Gather (Collect)
    • A variable name that is preceded by an asterisk (*) collects the arguments into a tuple

Program to print values in tuple

def dotraverse(*t):
	i=0
	while i < len(t):
		print(t[i])
		i = i + 1

dotraverse(10,-1,3,9)

Output

10
-1
3
9

Scatter

To pass a series of arguments to a function, use * before the arguments

Program to print quotient and remainder

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"))
num = (n1,n2)
tuple1=division(*num)
print("tuple1:",tuple1)

Output,

Enter number 1 10
Enter number 2 3
tuple1: (3.3333333333333335, 1)

When the asterisk (*) is used before the arguments at the time of function definition, it collects all the calling function arguments in a tuple and when it is used at the time of calling, it scatters the values of the tuple

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