pow() function in Python

In this article, we would discuss pow() function in Python. We can use either exponentiation operator (**) or pow() function to calculate powers. In other words, we can either use a operator or function to get the desired outcome.

First we discuss the exponentiation operator (**). We do base**exp to get the desired outcome. Hence, to calculate 210, we would issue the following –

>>> 2**10

It would return with the result –

1024

Similarly, to calculate 2-10

>>> 2**(-10)

The outcome would be –

0.0009765625

Similar results can also be achieved through pow() function. We need to call the pow() function and provide the relevant arguments. But, there is more to it. Either we can supply pow() with two arguments or three arguments. We will discuss each of these next –

I. pow(base, exp) function (with two arguments)

pow(base, exp) function is equivalent to using base**exp. Earlier, we used 2**10. But, it can also be done through pow() –

>>> pow(2, 10)

This would return with –

1024

Or, 2**(-10) –

>>> pow(2, -10)

The output will be –

0.0009765625

II. pow(base, exp[,mod]) function (with three arguments)

pow(base, exp[,mod]) function is equivalent to base**exp%mod. mod is here modulus. The rest of the calculation is self-explanatory. Modulus returns the remainder of a division. Here, when 1024 is divided by 10 then remainder comes out to be 4. Hence, the desired outcome from such a calculation is the remainder which is left. We will continue with the above mentioned examples –

>>> pow(2,10,10)

It would get us the result –

4

or, if we check it directly –

>>> 2**10%10

Similar result is achieved –

4

In conclusion, we have discussed pow() function in Python.

Additional Info –

We get a complex number if we switch to a negative base. Lets say we want to take the square root of -16

>>> pow(-16,0.5)

It would get us the result in form of an imaginary number –

(2.4492935982947064e-16+4j)

Although, we have discussed only couple of examples here. We would like you to try the pow() function with different combinations. It just helps us to have more clarity.