difference between args and kwargs python code example
Example 1: difference between args and kwargs in python
def adder(*num):
sum = 0
for n in num:
sum = sum + n
print("Sum:",sum)
adder(3,5)
adder(4,5,6,7)
adder(1,2,3,5,6)\
def intro(**data):
print("\nData type of argument:",type(data))
for key, value in data.items():
print("{} is {}".format(key,value))
intro(Firstname="Sita", Lastname="Sharma", Age=22, Phone=1234567890)
intro(Firstname="John", Lastname="Wood", Email="[email protected]", Country="Wakanda", Age=25, Phone=9876543210)
Example 2: **kwargs
When it iterating over a dictionary you are only able to iterate over
the keys not the values. The ** when placed before a variable will allow
you to iterate and unpack both key and value pairs. Because you are
unpacking both key and value this will return the result as a dictionary.