Open In App

What does the Double Star operator mean in Python?

Last Updated : 14 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Double Star or (**) is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python Language. It is also known as Power Operator.

What is the Precedence of Arithmetic Operators?

Arithmetic operators follow the same precedence rules as in mathematics, and they are: exponential is performed first, multiplication and division are performed next ,followed by addition and subtraction.

Arithmetic operators priorities order in Decreasing Mode:

()   >>   **   >>   *  >>  /  >>  //  >>  %   >>   +   >>   -

Uses of Double Star operator:

As Exponentiation Operator

For numeric data types, double-asterisk (**) is defined as an Exponentiation Operator:

Example:

Python3




# Python code to Demonstrate the Exponential Operactor
 
a = 2
b = 5
 
# using double asterisk operator
c = a**b
print(c)
 
 
# using double asterisk operator
z = 2 * (4 ** 2) + 3 * (4 ** 2 - 10)
print(z)


Output:

32
50

 

As arguments in functions and methods

In a function definition, the double asterisk is also known  **kwargs. They used to pass a keyword, variable-length argument dictionary to a function. The two asterisks (**) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.

First, let’s simply print out the **kwargs arguments that we pass to a function. We’ll create a short function to do this:

Example:

Python3




# Python Program to create a function to get a dictionary of names.
# Here, we will start with a dictionary of three names
 
 
def function(**kwargs):
    for key, value in kwargs.items():
        print("The value of {} is {}".format(key, value))
 
 
function(name_1="Shrey", name_2="Rohan", name_3="Ayush")


Output:

The value of name_1 is Shrey
The value of name_2 is Rohan
The value of name_3 is Ayush

Now here is another example where we will pass additional arguments to the function to show that **kwargs will accept any number of arguments:

Python3




# Python Program to create a function to get a dictionary of as many names
# you want to include in your Dictionary
 
 
def function(**kwargs):
    for key, value in kwargs.items():
        print("The value of {} is {}".format(key, value))
 
 
function(
    name_1="Ayush",
    name_2="Aman",
    name_3="Harman",
    name_4="Babber",
    name_5="Striver",
)


Output:

The value of name_1 is Ayush
The value of name_2 is Aman
The value of name_3 is Harman
The value of name_4 is Babber
The value of name_5 is Striver

The time complexity of the given Python program is O(n), where n is the number of key-value pairs in the input dictionary. 

The auxiliary space complexity of the program is also O(n), as the program stores the input dictionary in memory while iterating over it.

Conclusion:

Using **kwargs provides us with the flexibility to use keyword arguments in our program. When we use **kwargs as a parameter, we don’t need to know how many arguments we would eventually like to pass to a function. Creating functions that accept **kwargs are best used in situations where you expect that the number of inputs within the argument list will remain relatively small.



Previous Article
Next Article

Similar Reads

Benefits of Double Division Operator over Single Division Operator in Python
The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. C/C++ Code # A Python program to demonstrate use of # "//" for both integers and floating points print(5//2) print(-5//2) print(5.0//2) Output: 2 -3 2.0 The time complexity of the program is O(1) as it conta
2 min read
Python - Star or Asterisk operator ( * )
There are a many places you’ll see * and ** used in Python. Many Python Programmers even at the intermediate level are often puzzled when it comes to the asterisk ( * ) character in Python. After studying this article, you will have a solid understanding of the asterisk ( * ) operator in Python and become a better coder in the process! Below are th
3 min read
What does %s mean in a Python format string?
The % symbol is used in Python with a large variety of data types and configurations. %s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string. The %s operator is put w
3 min read
What Does $ Mean in Python?
Python programming language has several operators that are used to perform a specific operation on objects. These operators are special or special characters that perform a specific task, such as arithmetic operators, logical operators, assignment operators, etc. In this article, we will learn about another operator in Python which is used on Strin
2 min read
What does inplace mean in Pandas?
In this article, we will see Inplace in pandas. Inplace is an argument used in different functions. Some functions in which inplace is used as an attributes like, set_index(), dropna(), fillna(), reset_index(), drop(), replace() and many more. The default value of this attribute is False and it returns the copy of the object. Here we are using fill
2 min read
What does -1 mean in numpy reshape?
While working with arrays many times we come across situations where we need to change the shape of that array but it is a very time-consuming process because first, we copy the data and then arrange it into the desired shape, but in Python, we have a function called reshape() for this purpose. What is numpy.reshape() in Python The numpy.reshape()
3 min read
Why import star in Python is a bad idea
Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import. Also it becomes very difficult at some times to say from w
3 min read
Python - Draw Star Using Turtle Graphics
In this article, we will learn how to make a Star using Turtle Graphics in Python. For that let's first know what is Turtle Graphics. Turtle graphics Turtle is a Python feature like a drawing board, which let us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around. Turtle comes into the turtle libr
2 min read
Draw Colourful Star Pattern in Turtle - Python
In this article we will use Python's turtle library to draw a spiral of stars, filled with randomly generated colours. We can generate different patterns by varying some parameters. modules required: turtle: turtle library enables users to draw picture or shapes using commands, providing them with a virtual canvas. turtle comes with Python's Standa
2 min read
How to draw color filled star in Python-Turtle?
Prerequisite: Turtle Programming Basics, Draw Color Filled Shapes in Turtle Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move the turtle, there are some functions i.e forward(), backward(), etc. Approach: The following st
2 min read
three90RightbarBannerImg