How To Use *args and **kwargs In Python
Code Example
# *args In Python
def sum(x, y):
sum = x + y
print(sum)
sum(1, 2)
Output: 3
sum(1, 2, 3)
Output: Here you will get "TypeError"
To solve this problem *args can help us, let's check it out :
def sum(*args):
sum = 0
for num in args:
sum += num
print("Sum is :", sum)
sum(1, 2)
Output: 3
sum(1, 2, 3)
Output: 6
sum(1, 2, 3, 4)
Output: 10
Comments
Post a Comment