Home » Variable Length Arguments in Python

Variable Length Arguments in Python

by Online Tutorials Library

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-

  1. Non – Keyworded Arguments (*args)
  2. Keyworded Arguments (**kwargs)

Non – Keyworded Arguments (*args)

First let us understand the properties of *args,

  1. The * in *args indicates that we can pass variable number of arguments in a function in Python.
  2. 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,

  1. In the first step, we have created a function that will take variable arguments as its parameters.
  2. After this, we have used a for loop that will take each element or parameter in this case and print it.
  3. Finally, we have passed six strings as the parameters in our function.
  4. 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.

  1. In the first step, we have created a function that will take two parameters and then rest as variable arguments as its parameters.
  2. In the next step, we have defined the function in which we have displayed the values of the first two parameters.
  3. After this, we have used a for loop that will take each element or parameter in this case and print it.
  4. Finally, we have passed six strings as the parameters in our function.
  5. 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-

  1. The double star indicates that any number of keyworded arguments can be passed in a function.
  2. 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.

  1. In the first step, we have created a function that takes keyworded arguments as its parameters.
  2. After this, in the function definition we have specified that a for loop will be used to fetch the key-value pair.
  3. Finally, we have passed six key value pairs inside the function.
  4. 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,

  1. In the first step, we have created a function that takes one parameter and rest as keyworded arguments as its parameters.
  2. After this, in the function definition, we have specified that a for loop will be used to fetch the key-value pair.
  3. 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.
  4. 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-

  1. In the first step, we have created a function that takes *args and **kwargs (non-keyworded and keyworded arguments) as its parameters.
  2. After this, we have the function definition where we have displayed the values of both parameters.
  3. Finally, we have passed six parameters in the function, from which three are strings and the other three are key-value pairs.
  4. 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.


You may also like