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 –
It would return with the result –
Similarly, to calculate 2-10 –
The outcome would be –
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 –
Or, 2**(-10) –
>>> pow(2, -10)
The output will be –
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 –
It would get us the result –
or, if we check it directly –
Similar result is achieved –
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
It would get us the result in form of an imaginary number –
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.