Python * Single And **Double Asterisk Explained! - GUVI Blogs (2024)

What do * (single asterisk) and **(double asterisk)doforparameters 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.

Python * Single And **Double Asterisk Explained! - GUVI Blogs (1)

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
  • What does Double Asterisk (**) mean if used with Arguments?
    • Double Asterisk Program - 1
    • Double Asterisk Program - 2
  • Final Notes
  • 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, tryGUVI’s Python self-paced certification course with IIT Certification.

    For instance,

    Single Asterisk Program – 1

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (2)

    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:

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (3)

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

    Single Asterisk Program – 2

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (4)

    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:

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (5)

    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,

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (6)

    Double Asterisk Program – 1

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (7)

    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:

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (8)

    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

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (9)

    Here is the output of the following code:

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (10)

    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, tryGUVI’s Python self-paced certification course with IIT Certification.

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (11)

    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

    Python * Single And **Double Asterisk Explained! - GUVI Blogs (2024)
    Top Articles
    How to Install Minecraft on a VPS Server - Knowledgebase
    Cycle Billing: What It is, How It Works, Pros and Cons
    Hotels
    Loves Employee Pay Stub
    Costco The Dalles Or
    Klustron 9
    Best Transmission Service Margate
    His Lost Lycan Luna Chapter 5
    Produzione mondiale di vino
    Visustella Battle Core
    Morgan Wallen Pnc Park Seating Chart
    Echo & the Bunnymen - Lips Like Sugar Lyrics
    charleston cars & trucks - by owner - craigslist
    Peraton Sso
    Craigslist Red Wing Mn
    1773X To
    Loves Employee Pay Stub
    91 East Freeway Accident Today 2022
    A Cup of Cozy – Podcast
    Airline Reception Meaning
    Current Students - Pace University Online
    Promatch Parts
    Evil Dead Rise - Everything You Need To Know
    Busted! 29 New Arrests in Portsmouth, Ohio – 03/27/22 Scioto County Mugshots
    Scat Ladyboy
    Brenda Song Wikifeet
    Flixtor Nu Not Working
    Chase Bank Cerca De Mí
    Morlan Chevrolet Sikeston
    Everything You Need to Know About NLE Choppa
    Blue Beetle Movie Tickets and Showtimes Near Me | Regal
    Today's Final Jeopardy Clue
    Autozone Locations Near Me
    Hisense Ht5021Kp Manual
    Srg Senior Living Yardi Elearning Login
    Invalleerkracht [Gratis] voorbeelden van sollicitatiebrieven & expert tips
    2700 Yen To Usd
    Emily Tosta Butt
    Www.craigslist.com Waco
    Silicone Spray Advance Auto
    4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
    Mother Cabrini, the First American Saint of the Catholic Church
    Costco The Dalles Or
    Leland Westerlund
    Adams-Buggs Funeral Services Obituaries
    Okta Login Nordstrom
    786 Area Code -Get a Local Phone Number For Miami, Florida
    Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
    Ret Paladin Phase 2 Bis Wotlk
    Southern Blotting: Principle, Steps, Applications | Microbe Online
    Latest Posts
    Article information

    Author: Virgilio Hermann JD

    Last Updated:

    Views: 6216

    Rating: 4 / 5 (61 voted)

    Reviews: 92% of readers found this page helpful

    Author information

    Name: Virgilio Hermann JD

    Birthday: 1997-12-21

    Address: 6946 Schoen Cove, Sipesshire, MO 55944

    Phone: +3763365785260

    Job: Accounting Engineer

    Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

    Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.