Post thumbnail
PROGRAMMING LANGUAGES

Python * Single And **Double Asterisk Explained!

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 are our topics 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.

Table of contents


  1. What does Single Asterisk (*) mean if used with parameters?
    • Single Asterisk Program - 1
    • Single Asterisk Program - 2
  2. What does Double Asterisk (**) mean if used with Arguments?
    • Double Asterisk Program - 1
    • Double Asterisk Program - 2
  3. Final Notes
  4. These Five MCQs will help you take a quick test of the single asterisk and double asterisk concept.

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. 

If you are aspiring to explore Python through a self-paced course, try GUVI’s Python self-paced certification course with IIT Certification.

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. 

MDN

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 varied features.

If you have doubts, we recommend you go through the above guide once more. If it still bothers you, drop it down in the comments; we will discuss it further. 

If you are aspiring to explore Python through a self-paced course, try GUVI’s Python self-paced certification course with IIT Certification.

MDN

These Five MCQs will help you take a quick test of the single asterisk and double asterisk concept.

#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 items.

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

This 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, a single asterisk accepts keyword parameters only whereas a 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

Career transition

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Share logo Whatsapp logo X logo LinkedIn logo Facebook logo Copy link
Free Webinar
Free Webinar Icon
Free Webinar
Get the latest notifications! 🔔
close
Table of contents Table of contents
Table of contents Articles
Close button

  1. What does Single Asterisk (*) mean if used with parameters?
    • Single Asterisk Program - 1
    • Single Asterisk Program - 2
  2. What does Double Asterisk (**) mean if used with Arguments?
    • Double Asterisk Program - 1
    • Double Asterisk Program - 2
  3. Final Notes
  4. These Five MCQs will help you take a quick test of the single asterisk and double asterisk concept.