# *args is a Variable Length Argument of type Tuple,
# which takes any number of parameters, including 0 number of arguments as well.
def variableLengthArguments(*args):
print (sum(args))
variableLengthArguments() # Output: 0
variableLengthArguments(10) # Output: 10
variableLengthArguments(10, 20) # Output: 30
variableLengthArguments(10, 20, 30, 50, 90) # Output: 200
def func(*args):
x = [] # emplty list
for i in args:
i = i * 2
x.append(i)
y = tuple(x) # converting back list into tuple
return y
tpl = (2,3,4,5)
print(func(*tpl))
# if args is not passed it return message
# "Hey you didn't pass the arguements"
def ech(nums,*args):
if args:
return [i ** nums for i in args] # list comprehension
else:
return "Hey you didn't pass args"
print(ech(3,4,3,2,1))
# normal parameters with *args
def mul(a,b,*args): # a,b are normal paremeters
multiply = 1
for i in args:
multiply *= i
return multiply
print(mul(3,5,6,7,8)) # 3 and 5 are being passed as a argument but 6,7,8 are args
def wrap(*args):
lis = list(args) # it is important to convert args into list before looping, args take tuple as a argument
for i in range(len(lis)):
lis[i] = lis[i] * 2
args = tuple(lis)
return args
print(wrap(2,3,4,5,6))
# if args is not passed it return message
# "Hey you didn't pass the arguements"
def ech(num,*args):
if args:
a = []
for i in args:
a.append(i**num)
return a # return should be outside loop
else:
return "Hey you didn't pass the arguements" # return should be outside loop
print(ech(3))