Calculate exponent in Python

In Math, the exponent is referred to the number of times a number is multiplied by itself.

For example, 4^3

In this case, the exponent will be 4 * 4 * 4 = 64

In Python, you may use different ways for calculating the exponents. All these are explained below with example code.

First way: Using ** for calculating exponent in Python

The simplest way is using the exponentiation operator (**) double asterisk for calculating the exponent in Python.

The example below calculates the exponent for three different numbers:

#An example of Python exponent



exp1 = 4**3

exp2 = 2**4

exp3 = 5**5



print("The exponent of 4**3 = ",exp1)

print("The exponent of 2**4 = ",exp2)

print("The exponent of 5**5 = ",exp3)

The result:

Python exponent

How about using exponent operator for floating point numbers?

You may also use double asterisk operator (**) for calculating the exponents of floating point numbers. The following example calculates exponents for three floating numbers:

flt_exp1 = 4.2**2

flt_exp2 = 3.3**3

flt_exp3 = 6.2**2



print("The exponent of 4.2**2 = ",flt_exp1)

print("The exponent of 3.3**3 = ",flt_exp2)

print("The exponent of 6.2**2 = ",flt_exp3)

The output:

The exponent of 4.2**2 =  17.64

The exponent of 3.3**3 =  35.937

The exponent of 6.2**2 =  38.440000000000005

Similarly, you may use the floating number for power:

flt_exp1 = 3**2.5

flt_exp2 = 2**5.5

flt_exp3 = 5**2.1



print("The exponent of 3**2.5 = ",flt_exp1)

print("The exponent of 2**5.5 = ",flt_exp2)

print("The exponent of 5**2.1 = ",flt_exp3)

The result:

The exponent of 3**2.5 =  15.588457268119896

The exponent of 2**5.5 =  45.254833995939045

The exponent of 5**2.1 =  29.36547357720048

What about negative numbers?

Using negative exponent means how many times to divide 1 by the given number. For example:

5^-1 means:

1/5 =  0.2

You may also use negative power as using the exponent operator in Python:

neg_exp1 = 5**-1

neg_exp2 = 10**-3

neg_exp3 = 20**-4



print("The exponent of 5**-1 = ",neg_exp1)

print("The exponent of 10**-3 = ",neg_exp2)

print("The exponent of 20**-4 = ",neg_exp3)

The output:

The exponent of 5**-1 =  0.2

The exponent of 10**-3 =  0.001

The exponent of 20**-4 =  6.25e-06

Second way of getting exponent in Python: the pow() function

In Mathematics, 3^2 is also called “3 to the power 2” to refer exponentiation. So, in Python, a function pow() is also available that is built-in and does not require to include any module like math. You may use this directly.

The syntax for using the pow() function is:

pow(x, y[, z])

If you use pow() function with two arguments i.e. pow(x,y) then it is equivalent to x**y (as we used in the above section).

If the third argument (z) is given in the pow() function then it acts like this:

pow(x, y) % z)

That means, the pow() returns x to the power y, modulo z.

The example of pow() function

This example uses the first two arguments of the pow() function. So it acts just like the ** operator. I used various numbers; int, floating number, negative numbers. See the code and output:

pow1 = pow(2, 3)

pow2 = pow(4.5, 2)

pow3 = pow(-4, 2)

pow4 = pow(5, -2)



print("The result of pow(2, 3) = " ,pow1)

print("The result of pow(4.5, 2) = " ,pow2)

print("The result of pow(-4, 2) = " ,pow3)

print("The result of pow(5, -2) = " ,pow4)

The output:

Python power

Using third argument (modulo) example

The example below shows using the third argument in the pow() function:

pow1 = pow(2, 3,3)

pow2 = pow(4, 2, 4)

pow3 = pow(5, 3, 6)

print("The result of pow(2, 3,3) = " ,pow1)

print("The result of pow(4, 2, 4) = " ,pow2)

print("The result of pow(5, 3, 6) = " ,pow3)

The result:

The result of pow(2, 3,3) =  2

The result of pow(4, 2, 4) =  0

The result of pow(5, 3, 6) =  5

Third way – using math module’s pow() function

The math module also has a function called pow() for getting the exponent. The syntax for using the math’s pow() function is:

math.pow(x, y)

The difference is evident; the math’s pow() function allows only two arguments. It returns x raised to power y.

There is another difference between the two pow() functions. The math pow() function converts both its arguments to type float.

An example of its usage is shown below:

import math



m_pow1 = math.pow(2, 3)

m_pow2 = math.pow(4.5, 3)

m_pow3 = math.pow(3, 2.5)

print("The result ofpow(2, 3,3) = " ,m_pow1)

print("The result of pow(4, 2, 4) = " ,m_pow2)

print("The result of pow(5, 3, 6) = " ,m_pow3)

The output:

The result of math.pow(2, 3) =  8.0

The result of math.pow(4.5, 3) =  91.125

The result of math.pow(3, 2.5) =  15.588457268119896

There, you can see pow(2,3) returned 8.0 i.e. it converted both int arguments to float.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!