Exponents are superscript numbers that describe how many times you want to multiply a number by itself. Calculating a value raised to the power of another value is a fundamental operation in applied mathematics such as finance, machine learning, statistics, and data science. This tutorial shows you how to do it in Python!
Definition
For pow(x, y)
, the pow()
function returns the value of x
raised to the power y
. It performs the same function as the power operator **
, i.e. x**y
, but differs in that it comes with an optional argument called mod.
Examples without mod
>>> pow(5, 2) 25 >>> pow(-3, 3) -27 >>> pow(2, -2) 0.25
Parameters and Syntax
pow(base, exp, mod=None) |
The pow()
function includes two compulsory arguments, base
and exp
, and one optional argument, mod
, whose default value is None
. All arguments must be of numeric data type.
Parameter | Description |
exp | A number that represents the base of the function, whose power is to be calculated. |
base | A number that represents the exponent of the function, to which the base will be raised. |
mod | A number with which the modulo will be computed. |
Return value: The output of base
raised to the power exp
and will be a numeric data type, int, float or complex, depending on what you input.
Using the pow() function without the mod argument
When using the pow(x, y)
function without the optional mod argument, it will perform the same operation as the power operator x**y
, raising x
to the power y
.
Comparison of the two methods
>>> pow(6, 4) 1296 >>> 6 ** 4 1296
The pow()
function accepts all numeric data types, i.e. int, float and even complex numbers. In general the return value will depend on what data types you input. The example above shows that both arguments are type int, therefore, an int type is returned. However, if you were to instead use a float number as one or both of the arguments, the function will automatically return a float type.
Examples using float types
>>> pow(3.0, 4) 81.0 >>> pow(4.5, 2.3) 31.7971929089206
As with float type inputs leading to float outputs, the same reasoning applies to complex numbers. If you enter a complex number as one or both of the arguments, a complex number will be returned.
Example using complex numbers
>>> pow(4+2j, 3) (16+88j)
The return type will also depend on whether your arguments are non-negative or negative, as is shown in the below table.
base | exp | Return type |
Non-negative | Non-negative | int |
Non-negative | Negative | foat |
Negative | Non-negative | int |
Negative | Negative | float |
Examples of return values with different input types
>>> pow(7, 2) 49 >>> pow(4, -5) 0.0009765625 >>> pow(-6, 3) -216 >>> pow(-9, -4) 0.00015241579027587258
Using the pow() function with a mod argument
What sets the pow()
function apart from the **
operator is its third optional argument, mod
, which gives you the ability to do a modulo operation within the function.
The process of operations when using the mod argument is as follows:
If we have pow(x, y, z)
, the function first performs the task of raising x
to the power y
and then that result is used to perform the modulo task with respect to z
. It would be the equivalent of (x**y) % z
.
Examples using mod
>>> pow(14, 7, 5) 4 >>> pow(-8, 3, 5) 3 >>> pow(2, 4, -3) -2
The general rule for using the mod argument is that all values must be of integer type, the exp
argument must be non-negative and the mod argument must be non-zero. However, Python 3.8 now comes with the functionality of computing modular inverses. In this case, the exp
argument may be negative, on the condition that base is relatively prime to mod, i.e, the only common integer divisor of base and mod is 1.
So, when using the pow()
function with negative exp
, the function will perform as follows:
pow(inv_base, -exp, mod)
In other words, the function will compute the modular inverse of base and mod first and then that result will be used in the pow()
function as base to be computed as normal with the exp argument being converted to its non-negative counterpart.
Example of modular inverse
>>> pow(87, -1, 25) 23
In this example, the straight modular inverse is calculated because inv_base
will be raised to the power 1.
Example of modular inverse when exp is not -1
>>> pow(34, -5, 19) 10 # The modular inverse of 34 mod 19 is 14, therefore, we end up with the function pow(14, 5, 19) >>> pow(14, 5, 19) 10
Calculating the nth root of a number using pow()
Unfortunately, Python does not have a built-in function to calculate the nth root of a number. The math module only has a function to calculate square roots, math.sqrt()
, therefore, we have to get creative in order to calculate nth roots.
We know that nx
is equivalent to x1n
. Thus, using this knowledge we can calculate the nth root in Python by using either pow(x, (1/n))
or x**(1/n)
.
Examples of calculating nth roots
>>> pow(625, (1/4)) 4.0 >>> 729**(1/3) 8.999999999999998
Note that performing an nth root calculation will always return a float when not using complex numbers. Since Python’s float type works on approximations, it will often return the approximation rather than the exact number, even when an exact answer is possible. This is demonstrated in the second example above.
When calculating the nth root of a negative number, the return value will be a complex number whether an integer number is possible or not.
Examples of calculating nth roots of negative bases
>>> pow(-16, (1/2)) (2.4492935982947064e-16+4j) >>> pow(-27, (1/3)) (1.5000000000000004+2.598076211353316j)
We would expect the second example above, the cubed root of -27, to result in -3, but instead we get a complex number. This is because Python returns the principal root rather than the real root. For an explanation of these different types of roots, you can look up the Fundamental theorem of algebra.
math.pow() Function
In the math module of Python, there is a similar function called math.pow()
. To use this we first need to import the math function, thus, the built-in pow()
function will be very slightly faster. The main differences between the two functions is that math.pow()
does not allow for the optional mod argument and it will always return a float. So if you want to ensure that you get a float result, math.pow()
is a better option.
Example of using math.pow()
>>> import math >>> math.pow(9, 5) 59049.0
When to use the pow() function vs when to use the ** operator
When deciding between using the pow()
function or the **
operator, the most important factor to consider would be the efficiency of your code. We can use the timeit.timeit()
function from the timeit
module to find out how fast Python executes our code.
Examples of using timeit with simple numbers
>>> import timeit >>> timeit.timeit('pow(5, 2)') 0.25059129999863217 >>> timeit.timeit('5**2') 0.008814800001346157
When performing a simple power computation, the **
operator appears to be much faster.
Examples using modulo
>>> timeit.timeit('pow(52, 2, 4)') 0.7482693000001746 >>> timeit.timeit('52**2 % 4') 0.012026999998852261
The same is true even when we include a modulo operation.
However, when we want to perform power operations with very large numbers, the pow()
function is much quicker, showing that the power of the pow()
function lies in executing longer computations.
Examples using large numbers
>>> timeit.timeit('pow(5234, 2341, 124)') 0.9020593000004737 >>> timeit.timeit('5234**2341 % 124') 152.56075580000106
Here the pow()
function is extremely fast compared to the **
operator. Therefore, we can generalize these findings by saying that when you want to perform short, simple calculations, the ** operator is the better option, however, if your operations involve very large numbers, the pow()
function is much more efficient.