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


Quiz Level 1

Quiz Level 2


Request Me




Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python

How to solve the Liner Equation using Python