Variable Length Arguments in Python
We have tried and learned the different ways of defining and calling a function in our program.
In this article, we will discuss what are the variable-length arguments in Python.
Here we will cover two types-
- Non – Keyworded Arguments (*args)
- Keyworded Arguments (**kwargs)
Non – Keyworded Arguments (*args)
First let us understand the properties of *args,
- The * in *args indicates that we can pass variable number of arguments in a function in Python.
- We can pass any number of arguments during the function call.
The program given below illustrates the same,
Output:
Let us study Data Science and Blockchain
Explanation-
Let’s understand what we have done in the above program,
- In the first step, we have created a function that will take variable arguments as its parameters.
- After this, we have used a for loop that will take each element or parameter in this case and print it.
- Finally, we have passed six strings as the parameters in our function.
- On executing the program, the desired output is displayed.
Let’s have a look at one more program based on *args.
Output:
The first parameter is: Let The second parameter is: us study Data Science and Blockchain
Explanation-
It’s time to have a glance at the explanation of this program.
- In the first step, we have created a function that will take two parameters and then rest as variable arguments as its parameters.
- In the next step, we have defined the function in which we have displayed the values of the first two parameters.
- After this, we have used a for loop that will take each element or parameter in this case and print it.
- Finally, we have passed six strings as the parameters in our function.
- On executing the program, the desired output is displayed.
Now let us understand what is a keyworded argument or **kwargs in Python.
Keyworded Arguments (**kwargs)
Here we can pass a keyworded variable-length argument.
Some of its characteristics are-
- The double star indicates that any number of keyworded arguments can be passed in a function.
- It is analogous to a dictionary where each key is mapped to a value.
Consider the program given below,
Output:
a_key=Let b_key=us c_key=study d_key=Data Science e_key=and f_key=Blockchain
Explanation-
Let us see what we have done in the above program.
- In the first step, we have created a function that takes keyworded arguments as its parameters.
- After this, in the function definition we have specified that a for loop will be used to fetch the key-value pair.
- Finally, we have passed six key value pairs inside the function.
- On executing this program, the desired output is displayed.
Consider one more program given below based on **kwargs.
Output:
b_key=us c_key=study d_key=Data Science e_key=and f_key=Blockchain
Explanation-
Check out the explanation of this program,
- In the first step, we have created a function that takes one parameter and rest as keyworded arguments as its parameters.
- After this, in the function definition, we have specified that a for loop will be used to fetch the key-value pair.
- Finally, we have passed five key-value pairs inside the function and so it displays only those pairs in the output and doesn’t consider the first string.
- On executing this program, the desired output is displayed.
We will conclude this article with a program that illustrates how *args and **kwargs can be used in a single program.
Output:
The value of args is: ('Let', 'us', 'study') The value of kwargs is: {'a_key': 'Data Science', 'b_key': 'and', 'c_key': 'Blockchain'}
Explanation-
- In the first step, we have created a function that takes *args and **kwargs (non-keyworded and keyworded arguments) as its parameters.
- After this, we have the function definition where we have displayed the values of both parameters.
- Finally, we have passed six parameters in the function, from which three are strings and the other three are key-value pairs.
- On executing this program, the desired output is displayed.
Conclusion
In this article, we learned what are variable-length arguments in Python and how they can be used in our function.