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 myFun(*args,**kwargs):
2 print("args: ", args)
3 print("kwargs: ", kwargs)
4
5myFun('my','name','is Maheep',firstname="Maheep",lastname="Chaudhary")
6
7# *args - take the any number of argument as values from the user
8# **kwargs - take any number of arguments as key as keywords with
9# value associated with them
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>>> def f(a, b, *args, **kwargs):
2... print(F'a = {a}')
3... print(F'b = {b}')
4... print(F'args = {args}')
5... print(F'kwargs = {kwargs}')
6...
7
8>>> f(1, 2, 'foo', 'bar', 'baz', 'qux', x=100, y=200, z=300)
9a = 1
10b = 2
11args = ('foo', 'bar', 'baz', 'qux')
12kwargs = {'x': 100, 'y': 200, 'z': 300}
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