Simplify Your Calculations with Python Exponent Operator

If you wish to know the power of a number, the power operator will prove to be of great use to you. Also referred to as the exponent operator, it is one of Python’s most widely used operators due to its ease of use.

Syntax of exponent operator

If A refers to the base number which you want to multiply the number of times and X refers to how many times you wish to do that, you can write this expression as

#A multiplied X times 

Hence, through this arithmetic operator, you can find exponential values easily. For example,


Power operator with float

If you want the answer in float, opt for typecasting the expression. Use float data type here.


Exponent operator’s time complexity

The time complexity of the function with the exponent operator is O(log(d)) as the code with c**d will get executed O(log(d)) times only. Multiplications referring to O(log(d)) are included.

It will work faster than the pow function. The operator doesn’t involve any function call so it works at a quicker rate.

Precedence of the power operator

Unlike any other operator, the exponent operator is also executed from right to left. It holds the second place after the parenthesis in the operator precedence table. Hence, it has a high priority. If you want to execute it from left to right, you can use parentheses.

If you wish to learn more about the operator’s precedence in Python, check out this link: Mastering Python Order of Operations(Opens in a new browser tab)

Power operator in numpy

In numpy, you can find the square of a list of numbers stored as numpy array. Let’s look at an example to understand its usage with numpy :

import numpy as np
myarray = np.array([10,20,30,50,60])
#output: array([ 100,  400,  900, 2500, 3600], dtype=int32)

Complex values with Exponent operator

While dealing with numbers, we might require complex numbers too. To obtain a complex number with an exponent operator, you can opt for a negative number as a base(like -5) and a fractional number as the exponent (like 3/35 in decimal number format ). This way you will get a complex value as the code output.

For example, if you want to find the value of -5^1.5, ensure that you put the parentheses correctly else Python will first calculate 5 raised to 1.5 and later put the negative sign.

ans= (-5) ** 1.5

Division Error in **

We can get a Zero division error while using the exponent operator in Python. This can happen when we use 0.0 as a base and a negative number as an exponent. Logically this is wrong and hence we will get an error.


Negative exponent with power operator

If you use a negative exponent with a positive number, you will get the answer with e. For example:

ans= 26 ** -5

This works for negative numbers as a base with a negative exponent also. Consider this example:

ans= -25 ** -6.5

Here, Python interprets this as 25 raised to the power of -6.5 and then takes the negative sign of 25.

Difference between ** and *

** is an exponent operator in Python while * is used for multiplication. We can also use * operator to demonstrate exponents. For example, 4**3 matches the output of 4*4*4.

However, these operators refer to different Python functions. The first case refers to 4.__pow__(3) i.e. this function will suffice the output of 4**3. But the second statement will be referenced by 4.__mul__(4.__mul__(4))


What is the use of the exponent operator?

It helps to find the result of a number raised to some power.

What is 0**0?

In Python language, 0**0 gives one as output.


In this article, we learned the usage of the Exponent operator in Python and how it differs from the pow function.

Notify of
Inline Feedbacks
View all comments