Hit enter after type your search item

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:

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:

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:

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:

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:

The output:

Python power

Using third argument (modulo) example

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

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:

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.

This div height required for enabling the sticky sidebar