In this article, we are going to learn all about Python args and kwargs along with examples. These are the most important things if you want to pass parameters in a function or class.
If you don’t know about Python args and kwargs, Don’t worry at the end of this article, you are able to work with args and kwargs in Python.
In programming, we define a function to make reusable code that performs some specific task.
Headings of Contents
Python args and kwargs
To perform the operation, we call the function with a specific value. that value is called arguments or parameters. To add three numbers we define function Addition.
Example:
def Addition(x, y, z):
print("Sum is :- ", x + y + z)
To execute the above function we have to call the function along with three arguments.
Addition(12, 12, 12)
When you run the above code you will get output like:
The sum is:- 36
But here one important point is, sometimes you need to pass more than three-parameter or arguments in a function, On that time *args comes into the picture.
*args in Python
In Python, we can pass a variable number of arguments to a function with a special symbol is an asterisk (* ).* args is also called Non-Keyword Arguments.
Suppose in the above example we are not sure, how many arguments passed into the function. At that time we can use *args.
Example 1:
Use args with Python function.
def Addition(*args):
sum = 0
for i in args:
sum = sum + i
print("The Sum is :-", sum)
Addition(12)
Addition(12, 12, 12, 15)
Addition(12, 67, 33)
When you will run the above code you will get output like:-
The sum is:- 12
The sum is:- 51
The sum is:- 112
Example 2:
Use args with class.
class StudentAge:
def __init__(self, *args):
self.studentList = args
def studentAge(self):
print(f"Student age list is:- {self.studentList}")
s1 = StudentAge(12,23,45,12,32,15)
s1.studentAge()
Output:
Student age list is:- (12, 23, 45, 12, 32, 15)
Note:- You can change the name of args as your choice but an asterisk (*) is important.
Example 3:
def Addition(*num):
sum = 0
for i in num:
sum = sum + i
print("The Sum is :-", sum)
Addition(12)
Addition(12, 12, 12)
Addition(12, 67)
When you will run the above code you will get output like:
The sum is:- 12
The sum is:- 36
The sum is:- 79
**kwargs in Python:
**kwargs means keyword variable-length argument. In *args we can not pass keyword variable but here we pass keyword variable length arguments.
Example 1:
def Details(**kwargs):
for key,value in kwargs.items():
print("{} = {}".format(key, value))
Details(name = 'Vishvajit', Occupation = 'Programmer', country = 'India', city = 'Noida')
When you will run the above code you will get output like:
name = Vishvajit
Occupation = Programmer
country = India
city = Noida
Example 2:
Use Python kwargs with Python class.
class Engineer:
def __init__(self, **kwargs):
self.eng_name = kwargs['eng_name']
self.eng_type = kwargs['eng_type']
def Details(self):
print(f"My name is {self.eng_name} and I am {self.eng_type}.")
s1 = Engineer(eng_name = "Vishvajit Rao", eng_type = "Software Engineer")
s1.Details()
print("-----------")
s2 = Engineer(eng_name = "Pankaj Kumar", eng_type = "Civil Engineer")
s2.Details()
Output:
My name is Vishvajit Rao and I am a Software Engineer.
-----------
My name is Pankaj Kumar and I am a Civil Engineer.
Python *args and **kwargs together
You can use both python args and kwargs inside a function.
Example:
def myFunction(*args, **kwargs):
sum = 0
for i in args:
sum = sum + i
print("Sum is :- ",sum)
for key, value in kwargs.items():
print("{} = {}".format(key, value))
myFunction(12, 34, 54, name = 'Vishvajit', Occupation = 'Programmer', country = 'India', city = 'Noida')
When you run the above code you will output like:
Sum is :- 100
name = Vishvajit
Occupation = Programmer
country = India
city = Noida
Conclusion
In this article, you have learned all about Python args and kwargs along with various examples. args and kwargs in Python play the most important when you are working with variable argument and keyword variable-length argument.
I hope you don’t have any confusion regarding python args and kwargs,If you have any query, please ask me throught comment.
If you like this article, please share and keep visiting for further Python interesting tutorials.
FAQs about Python args and kwargs
What is *args and **kwargs in Python?
Ans:- *args and *kwargs are the special keywords in Python. args allow us to pass a variable number of arguments in function whereas **kwargs allow us to pass keyword variable-length argument in the function.
How do you pass Kwargs to a function in Python?
Ans:- Kwargs in Python allows to pass keyword variable-length argument in the function. This issued when we are not sure how many keyword arguments passed to function.
What type is *args in Python?
Ans:- In Python, *args allows to pass a variable number of arguments in a function. This is used when we don’t want to know, how many numbers of a variable number of arguments passed into the function.
Happy Coding
~ Vishvajit Rao
For more information:- Click Here