Difference between *args and **kwargs DS301222

*args :
allows us to pass variable number of non keyword arguments to function
Ex :
def sum(*num):
total=0
for i in num:
total=total+i
print(total)

sum(3,5,2)
sum(2,5,4,6)

**kwargs :
it allows us to pass the variable length of keyword arguments to the function.
ex:
def arg(**data):
for key,value in data.items():
print(" {} is {} " .format(key,value))
arg(name =“sia”, age=12)
arg(name=“rio”, age=11,height=5)

Main difference is *args pass variable number non keyword arguments where tuple operation is performed where as **kwargs pass variable number keyword arguments where dictionary operation is performed

3 Likes

@kinnukeshiya
Very good explanation. Keep it up :fire:

1 Like