1def add(*args): # *args takes multiple inputs
2 return sum(args)
3
4
5print(add(1,2,3,4,5)) # prints 15
6print(add(10, 20, 30)) # prints 60
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# We use *args and **kwargs as an argument when we are unsure
2# about the number of arguments to pass in the functions.
3
4#This is an example of *args
5def adder(*num):
6 sum = 0
7
8 for n in num:
9 sum = sum + n
10
11 print("Sum:",sum)
12
13adder(3,5)
14adder(4,5,6,7)
15adder(1,2,3,5,6)\
16
17
18#This is an example of **kwargs
19def intro(**data):
20 print("\nData type of argument:",type(data))
21
22 for key, value in data.items():
23 print("{} is {}".format(key,value))
24
25intro(Firstname="Sita", Lastname="Sharma", Age=22, Phone=1234567890)
26intro(Firstname="John", Lastname="Wood", Email="johnwood@nomail.com", Country="Wakanda", Age=25, Phone=9876543210)
1def display2(a,b,c):
2 print("kwarg1:", a)
3 print("kwarg2:", b)
4 print("kwarg3:", c)
5
6d = {"a": 1, "b": 2, "c": 3}
7display2(**d)
1def display(**kwargs):
2 d = {k.upper():v.upper() for k,v in kwargs.items() }
3 return d
4
5d = {"name":"neo","age":"33","city":"tokyo"}
6print(display(**d))