Open In App

What does the Double Star operator mean in Python?

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example:

2 ** 3 returns 8 (since 2³ = 8)

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

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.

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

Examples of ** (Double Star) Operator:

Example 1: As Exponentiation Operator

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

Python
a = 2
b = 5

c = a**b
print(c)

z = 2 * (4 ** 2) + 3 * (4 ** 2 - 10)
print(z)

Output
32
50

Explanation:

1. 2 ** 5 means 2 raised to the power 5 = 32

2. In the second expression:

  • 4 ** 2 = 16
  • So: 2 * 16 + 3 * (16 - 10) = 32 + 18 = 50

Example 2: As arguments in functions and methods

In a function definition, the double asterisk ** is used to define **kwargs, which allows you to pass a variable number of keyword arguments as a dictionary. While kwargs is just a naming convention, the ** is what actually enables this functionality. Example:

Python
def fun(**kwargs):
    for key, val in kwargs.items():
        print("{} : {}".format(key, val))


fun(n_1="Prajjwal", n_2="Kareena", n_3="Brijkant")

Output
n_1 : Prajjwal
n_2 : Kareena
n_3 : Brijkant

Explanation:

  • **kwargs collects keyword arguments into a dictionary.
  • Inside the function, we iterate through the dictionary and print each key-value pair.

Example 3: Unpacking Dictionary into Function Parameters

Apart from defining functions, ** is also used to pass a dictionary as keyword arguments when calling a function. It's very useful when wewant to pass dynamic data into a function.

Python
def intro(name, age, city):
    print(f"My name is {name}, I'm {age} years old and I live in {city}.")

p = {
    "name": "Prajjwa'",
    "age": 23,
    "city": "Prayagraj"
}

intro(**p)

Output
My name is Prajjwa', I'm 23 years old and I live in Prayagraj.

Explanation: The **p syntax unpacks the dictionary and passes its key-value pairs as arguments to the intro function.


Next Article

Similar Reads