Python is a powerful language that many developers find irresistible. One of the great things about Python is that it’s easy to learn, making it a great choice for beginners.
There are many times when you’re writing a program you’ll often need to pass along arguments and keywords to your program. This document will teach you how to do that in Python.
Python uses special symbols to pass a variable number of arguments to the function. The two special symbols are *args and **kwargs. This article will discuss how to use Python *args and **kwargs with examples.
Table of Contents
Python *args and **kwargs
While coding, we often define a function for increasing the reusability of code for the same operations. A function is called back to perform the desired operation with specified values. These values are called function arguments in Python.
In Python, we use special symbols in order to pass a variable number of arguments to a function.
The two special symbols are:
1. *args ( Non-Keyword Argument)
2. **kwargs (Keyword Argument)
These special symbols are used when there is no specific number of arguments, and we are unsure about them.
Let’s study both special symbols with examples.
1. Python *args
Python special symbol *args is used for passing a variable number of arguments to the function. When we wish to give a non-keyword argument, python *args has opted for a variable-length list of arguments.
- We use a single asterisk operator (*) before the variable name as a parameter in the argument.
- The arguments are passed in the form of a tuple.
- In the function, the passed arguments form a tuple inside with the same name as the specified parameter (excluding the asterisk).
Let’s study few examples for better understanding.
def Print(*fruit):
for x in fruit:
print("Arguments:", x)
Print('Strawberry', 'Apple', 'Melon', 'Orange')
Output

2. Python **kwargs
Python **kwargs is a special symbol used to pass a variable number of arguments to the function. With the help of Python *args, we cannot pass keyword arguments. Instead, **kwargs is opted for passing keyword arguments in a variable length argument lists.
- For **kwargs, we use a double asterisk operators (**) before the variable name that is a parameter.
- The arguments in the function are passed in the form of a dictionary, i.e. key-value pairs.
- The passed kwargs arguments make a dictionary of arguments inside a function with the same specified name of the parameter passed. (exclude asterisk).
Here are the few examples to learn how to use Python **kwargs.
def Print(**word):
for key, value in word.items():
print ("%s == %s" %(key, value))
Print(start ='Learn', mid ='with', end='CodeLeaks')
Output

How to use *args and **kwargs in Python program?
Here is an example using *args and *kwargs to call a function in Python program.
def Print(Num1, Num2, Num3):
print("Num1:", Num1)
print("Num2:", Num2)
print("Num3:", Num3)
my_args = ("John", "Hamlet", "Chris")
Print(*my_args)
my_kwargs = {"Num1" : "John", "Num2" : "Hamlet", "Num3" : "Chris"}
Print(**my_kwargs)
Output

FAQs
Is there any other way to pass data to a function besides using arguments or kwargs?
There may be other ways to pass data to a function besides using arguments or kwargs, but they are not commonly used. For example, you can use keyword arguments, named arguments, or positional arguments.
Which is better: calling a function with arguments or calling it with kwargs?
Calling a function with arguments is typically better because it is more explicit. This means that you can see what arguments the function expects, and you do not have to guess. Additionally, calling a function with arguments is more efficient because the compiler can optimize the code accordingly.
Calling a function with kwargs can be helpful if you want to pass in some default values or if you want to pass in arguments using an array. However, calling a function with kwargs can be slower because the compiler has to generate code for every parameter.
What is the difference between positional and keyword arguments?
Positional arguments are typically used when you want to pass information to a function as it is being called. Keyword arguments are typically used when you want to pass information to a function that takes multiple arguments.
Learn about Python Filter Function and Python timedelta function.
CONCLUSION
In this blog post, we have discussed the basics of *args and **kwargs in Python. These special attributes pass the information along to a function as the first argument. This is an important topic, as it can help you write more efficient code and make better use of your functions. I hope the examples helped you in learning the concept easily.