Table of Contents
show
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