args and kwargs in Python Programming | Python Programming

python-video-tutorials

args and kwargs in Python: As we have already discussed functions. There are two types of functions: functions without parameters and functions with parameters. Here the concept of args and kwargs only related to parameterized functions. So, we will take parameterized function to understand the concept of args* and kwargs** deeply.

args and kwargs in python

*args

Suppose we want to find the sum of 2 number using parameterized function. Let’s see an example:

#sum of two number using parameterized function 

def add(x,y):
    z = x+y
    print("Sum is: "+str(z))

add(7,5)

NoteHere we already know that, there are only two arguments are required to call the function. What if i want to find the sum of three numbers later? So, now you are thinking we’ll use three parameters and pass three arguments instead of two. “Yes” that’s the correct answer, but it’s not the good practice. *args function is very useful to overcome this problem.

Using *args function we can pass multiple arguments to our function without any limitations:

Example 1: Program to elaborate the use of args* function.

#Example of args* function

def fncn(*args):
    #print(type(args))   #just to print the type of args
    print(args[0])

x = ["pythonlobby","python","coding"]   
fncn(*x)  #three arguments passed

Output:

# pythonlobby

Note: We have created list x, and same list is passed as a argument to our function. But *args keyword always take tuple as a input. Hence list passed as an argument has been converted to tuple and then passed to function.

Example 2: Program to elaborate the use of *args with normal arguments. Here i am going to use only one normal parameter but we can use multiple normal arguments as well.

#Example of args* function with normal parameter

def fncn(y,*args):
    print(y)
    print(args[0])

x = ["pythonlobby","python","coding"]  
y = "I am Normal Argument" 
fncn(y,*x)  

Output:

# I am Normal Argument                                                                                                          
# pythonlobby

*args takes tuple as input

args and kwargs in python

**kwargs

**kwargsThe functionality of **kwargs is also same as that of *args. The only difference is that *args takes input in the form of tuple but on the other side **kwargs takes input in the form of dictionary.
Let’s make it more clear with the help of an example.

Example 1

#Example of **kwargs with normal parameter

def fncn(**kwargs):
    #print(type(**kwargs))
    print(kwargs)

x = {"first_name":"www.","middle_name":"pythonlobby","extension":".com"}  
fncn(**x)  

Output:

# {'middle_name': 'pythonlobby', 'extension': '.com', 'first_name': 'www.'}                                                     

We can also use *args and **kwargs together:

Example 2:

#Example of **kwargs with normal parameter

def fncn(*args,**kwargs):
    #print(type(**kwargs))
    #print(type(*args))
    print(args)
    print(kwargs)

x = ("Python","Lab","Tutorials")
y = {"first_name":"www.","middle_name":"pythonlobby","extension":".com"}  
fncn(*x,**y)  

Output:

# ('Python', 'Lab', 'Tutorials')                                                                                                
# {'first_name': 'www.', 'extension': '.com', 'middle_name': 'pythonlobby'}                                                      

Example 3Python program to elaborate the use of *args,**kwargs along with normal parameter.

#Example of **kwargs with normal parameter

def fncn(z,*args,**kwargs):
    #print(type(**kwargs))
    #print(type(*args))
    print(z)
    print(args)
    print(kwargs)

z = "This is Normal Argument"
x = ("Python","Lab","Tutorials")
y = {"first_name":"www.","middle_name":"pythonlobby","extension":".com"}  
fncn(z,*x,**y)  

Output:

# This is Normal Arguments
# ('Python', 'Lab', 'Tutorials')                                                                                                
# {'first_name': 'www.', 'extension': '.com', 'middle_name': 'pythonlobby'}                                                      

Where we use *args, **kwargs?
Following are some programs along with solutions that will make you clear about the use of *args & **kwargs.