Python *args and **kwargs
In the given article, you will learn about the *args and **kwargs function arguments of the Python programming language.
In programming, we define a function to make reusable code that performs repeated activities. To play out that activity, we call a function with the particular value, this value is known as a function parameter in Python. You have definitely seen many functions have one or more specified parameters. We can pass a variable number of parameters to a function using special symbols. These are the two symbols-
- *args (Non-Keyword Arguments)
- **kwargs (Keyword Arguments)
Usage of Python *args
In Python, the single-asterisk mark type of parameter *args can be utilized to send a non-keyworded variable-length argument list to the function. It allows us to pass an unspecified number of arguments to a function. Sometimes, at the time of the function definition, you do not need to know how many arguments will be passed to your function.
In the function, we will have to utilize an indicator * before the argument name to pass variable-length arguments. The parameters are passed as a tuple, and these passed parameters make a tuple inside the function with the same name as the parameter without the asterisk mark *.
In the given example, we have not passed any list as an argument to the function, because the *args allows us to pass a varying number of possible arguments to the function.
def sum(*args):
output = 0
# Iterating over the Python args
# tuple and get output
for x in args:
output += x
return output
Now, we can pass any number of arguments to the function. Here is the output -
>>> sum(10, 33, 12)
55
>>> sum(2, 4, 5, 12)
23
>>> sum(22, 420, 433, 233)
1108
>>>
Usage of Python **kwargs
In Python, the double-asterisk mark type of the parameter **kwargs can be utilized to send a keyworded variable length of arguments to a function because we cannot use the *args to pass a keyword argument. This is useful if you want to handle named arguments in a function. It makes the function flexible.
In the given example, the number of arguments is unknown, so we have added a double (**) before the parameter.
def stud_name(**kwargs):
print("\nData type of argument:",type(kwargs))
for key, value in kwargs.items():
print("The name of {} is {}".format(key, value))
When we run the above program and provide the name of the student with an ID, the output will be-
>>> stud_name(ID101='Smith', ID201="Priska", ID301="Abhi", ID312="Alex")
Data type of argument: <class 'dict'>
The name of ID101 is Smith
The name of ID201 is Priska
The name of ID301 is Abhi
The name of ID312 is Alex
Related Articles
Python split strings by comma
Python program to multiply two numbers
Find the stop words in nltk Python
Prettytable in Python
Convert list to numpy array
How to shuffle a list in Python
Hollow diamond pattern in python
sentimentintensityanalyzer Python
isalpha Python
Python YouTube Downloader Script
Python projects for beginners
Convert string to datetime python pandas
Pandas fillna example
Lemmatize python
How to generate QR Code in Python using PyQRCode
Python tesseract
Fibonacci series in python
Python File Handler - Create, Read, Write, Access, Lock File
Convert xml to json python
xmltodict example
Python convert dict to xml