fbpx

Python * Single And **Double Asterisk Can’t Be Explained Any Better!

What do * (single asterisk) and ** (double asterisk) do for parameters in Python? Understanding the difference between them is vital for a Python programmer.

To be clear, Python is a very straightforward language. That is to say, even a beginner can understand Python programs. However, there could be times when it can be tricky enough to block your mind. 

Today, in this guide, we will talk about one such condition. Say, a beginner or someone who just switched from some other programming language to Python could end up scratching their heads about it.

Single Asterisk (*) and Double Asterisk (**) used with function arguments is our topic for today. In this guide, we will talk about “What does * (single asterisk) and ** (double asterisk) do to parameters” with proper illustrations. 

single asterisk & double Asterisk

Quick Answer: Single Asterisk allows the developer to pass a variable number of Positional parameters and automatically converts the input values in the form of tuples. At the same time, Double Asterisks allows the users to pass a variable number of Keyword parameters in the form of a Dictionary. 

Scroll down for further details about *single asterisk and **double asterisk.

What does Single Asterisk (*) mean if used with parameters?

We use a single asterisk (*) with the function parameters to indicate two things-

  • No fixed number of input values/parameters.
  • Parameters are assigned to the function as tuples.

Apart from this, Single Asterisk (*) also represents the condition where only Positional parameters can be passed as positional arguments in the function. 

For instance,

Single Asterisk Program – 1 

single asterisk Program 1

Here, we have created a custom method, “func” with an argument using a single asterisk. Now, we have passed a variable “para” (list) which has a total of 5 values. But, if you look at the program’s output, you will find that the list is now converted into a tuple.

 Output:

That’s how the single asterisk is used before the argument converts a parameter of whatever type to a tuple. 

Single Asterisk Program – 2

single asterisk Program 2

Here, we have manually passed four different parameters in the 4th statement, whereas, In the 6th statement, we have tried to pass a Keyword parameter to check what type of error we get. 

Here is the output:

From the above output, We can conclude that single asterisk arguments accept only positional parameters. Passing the Keyword parameter will raise TypeError.

Now, let’s move to the next section to learn about double Asterisk (**) arguments in detail. 

What does Double Asterisk (**) mean if used with Arguments?

Using a double asterisk before the argument will allow you to pass a variable number of keyword parameters in the function. Apart from this, all the input parameters get automatically converted into dictionary values. 

For instance,

Double Asterisk Program – 1

double asterisk Program 1

The above program is an ideal illustration of function arguments with double asterisks. As mentioned above, the func method will only accept keyword parameters. If you try to pass the positional parameter, you’ll get the following error:

When we pass positional parameters in an argument with double asterisks, then we trigger the TypeError. 

A double asterisk ensures that the argument which we pass is stored as a dictionary in the function. You can also access the keys and values of the dictionary using basic dictionary statements. 

Double Asterisk Program – 2 

double asterisk Program 2

Here is the output of the following code:

As shown above, you can easily implement all the dictionary attributes on the parameters that you have passed. All those attributes are now converted into a dictionary by the double-asterisk attached before the function’s argument. 

Final Notes

Using a single asterisk and double asterisk allows you to input as many parameters as you want in your program. Therefore, if you create a user-based application where you aren’t aware of the user inputs, you may use either a single asterisk or double asterisk as a prefix to the function argument. It’s sometimes confusing, as both of these have their varied features.

If you have doubt, we recommend you to go through the above guide once more. If it still bothers you, drop it down in the comments; we will discuss it further. You may also visit the GUVI website. With the best curriculum and expert trainers, GUVI extends a great culture of technological skill enhancement courses.

Five MCQs

#1. Which asterisk would you prefer to store the variable number of records of different employees with multiple values? 

A. Double Asterisk

B. Single Asterisk

C. Difficult to determine

D. None of the above

Ans. Double Asterisk


#2. What will be the output of the following code?

def func(**int):

   for key, value in int.items():

       print(“The current key is {} with value {}”.format(key, value))

func(srNo = “1”,item = “Vegetables”)

A. The current key is srNo with a value of 1

That is to say, the current key is the item with value Vegetable.

B. Also, the current key is 1 with value srNo

That is to say, the current key is Vegetables with value item.

C. Again, the current key is 1 with value srNo

Which implies, the current key is the item with value Vegetables

D. None of the above

Ans. A

#3. What type of parameters are accepted by double asterisk arguments?

A. Positional Parameters

B. Keyword Parameters

C. Both A and B

D. None of the above

Ans. B

#4. The major difference between the single asterisk and a double asterisk is:

A. Single asterisk accepts positional parameters only whereas double asterisk accepts keyword parameters.

B. Also, single asterisk accepts keyword parameters only whereas double asterisk accepts positional parameters.

C. Again, a single asterisk accepts positional parameters only whereas a double asterisk accepts both types of parameters.

D. There is no such difference based on parameters

Ans. A

#5. Which of them is true

A. Double asterisk is used before arguments

B. Single asterisk accepts positional parameters

C. Single asterisk is used post arguments

D. A and B

Ans. D

Archana

5 thoughts on “Python * Single And **Double Asterisk Can’t Be Explained Any Better!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top