1>>> def argsKwargs(*args, **kwargs):
2... print(args)
3... print(kwargs)
4...
5>>> argsKwargs('1', 1, 'slgotting.com', upvote='yes', is_true=True, test=1, sufficient_example=True)
6('1', 1, 'slgotting.com')
7{'upvote': 'yes', 'is_true': True, 'test': 1, 'sufficient_example': True}
1def multiply(*args):
2 z = 1
3 for num in args:
4 z *= num
5 print(z)
6
7multiply(4, 5)
8multiply(10, 9)
9multiply(2, 3, 4)
10multiply(3, 5, 10, 6)
1# concatenate_keys.py
2def concatenate(**kwargs):
3 result = ""
4 # Iterating over the keys of the Python kwargs dictionary
5 for arg in kwargs:
6 result += arg
7 return result
8
9print(concatenate(a="Real", b="Python", c="Is", d="Great", e="!"))
10
1//spread operator
2a = [1, 2, 3]
3
4function printArgs(args) {
5 console.log(...args)
6}
7
8printArgs(a)
9//=> 1 2 3
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))