1# if args is not passed it return message
2# "Hey you didn't pass the arguements"
3
4def ech(nums,*args):
5 if args:
6 return [i ** nums for i in args] # list comprehension
7 else:
8 return "Hey you didn't pass args"
9
10print(ech(3,4,3,2,1))
1def func(*args):
2 x = [] # emplty list
3 for i in args:
4 i = i * 2
5 x.append(i)
6 y = tuple(x) # converting back list into tuple
7 return y
8
9tpl = (2,3,4,5)
10print(func(*tpl))
1# if args is not passed it return message
2# "Hey you didn't pass the arguements"
3
4def ech(num,*args):
5 if args:
6 a = []
7 for i in args:
8 a.append(i**num)
9 return a # return should be outside loop
10 else:
11 return "Hey you didn't pass the arguements" # return should be outside loop
12
13print(ech(3))
1# normal parameters with *args
2
3def mul(a,b,*args): # a,b are normal paremeters
4 multiply = 1
5 for i in args:
6 multiply *= i
7
8 return multiply
9
10print(mul(3,5,6,7,8)) # 3 and 5 are being passed as a argument but 6,7,8 are args
1def display(a,b,c):
2 print("arg1:", a)
3 print("arg2:", b)
4 print("arg3:", c)
5
6lst = [2,3]
7display(1,*lst)
1def wrap(*args):
2 lis = list(args) # it is important to convert args into list before looping, args take tuple as a argument
3 for i in range(len(lis)):
4 lis[i] = lis[i] * 2
5 args = tuple(lis)
6 return args
7print(wrap(2,3,4,5,6))
1def mul(*args): # args as argument
2 multiply = 1
3 for i in args:
4 multiply *= i
5
6 return multiply
7
8lst = [4,4,4,4]
9tpl = (4,4,4,4)
10
11print(mul(*lst)) # list unpacking
12print(mul(*tpl)) # tuple unpacking
1# if option is not passed it still returns 20
2# if option = True or option = 1 is passed it returns 20 which is sum of numbers
3# if option = False or option = 0 is passed it returns 0
4
5def addition(a, b, *args, option=True):
6 result = 0
7 if option:
8 for i in args:
9 result += i
10 return a + b + result
11 else:
12 return result
1def add(*args):
2 total = 0
3 for i in args:
4 total += i
5
6 return total
7
8print(add(5,5,10))