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)
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# 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
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))
1# Python program to illustrate
2# **kwargs for variable number of keyword arguments
3
4def info(**kwargs):
5 for key, value in kwargs.items():
6 print ("%s == %s" %(key, value))
7
8# Driver code
9info(first ='This', mid ='is', last='Me')