How to Calculate Exponent in Python

To calculate an exponent in Python, use the exponentiation operator which is ** (two asterisks.) Pass the number to evaluate on the left and to the power of on the right.

 

Let's try this out by getting the value of 4^³ (four to the power of three) the expression of which is 4*4*4 = 64.

 

exp = 4**3

print(exp)
64

 

Floating Point Exponent in Python

It is also possible to use floating-point numbers for both the exponent and power of using the Python ** operator.

 

Exponent as a float:

 

exp = 4.5**3

print(exp)
91.125

 

Power as a float:

 

exp = 4**3.5

print(exp)
128.0

 

Both values as floats:

 

exp = 4.5**3.5

print(exp)
193.30531630687244

 

Negative Power

You can also use a negative power with the Python exponentiation operator like this:

 

exp = 4**-3

print(exp)
0.015625

 

The Python pow() Function

Another way to calculate an exponent in Python is with the pow() function. Pass the exponent as the first argument and the power as the second.

 

exp = pow(4,3)

print(exp)
64

 

Like, the Python ** operator, pow() also accepts any combination of floats and integers and negative power.

 

exp = pow(4.1,-3.1)

print(exp)
0.012599985466196825

 

The math.pow() Function

If you are using the math package, there is pow() function available in it, which might be a preferable option for you – its default behaviour is to return floats.

 

import math

exp = math.pow(4,3)

print(exp)
64.0