12-22-2020, 11:24 AM
A Guide to Python’s pow() Function
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, the function returns the value of raised to the power . It performs the same function as the power operator , i.e. , but differs in that it comes with an optional argument called mod.
Examples without mod
Parameters and Syntax
The function includes two compulsory arguments, and , and one optional argument, , whose default value is . 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 modulowill be computed.
Return value: The output of raised to the power 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 function without the optional mod argument, it will perform the same operation as the power operator , raising to the power .
Comparison of the two methods
The 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 floatnumber as one or both of the arguments, the function will automatically return a float type.
Examples using float types
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
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
Using the pow() function with a mod argument
What sets the function apart from the operator is its third optional argument, , 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, the function first performs the task of raising to the power and then that result is used to perform the modulo task with respect to . It would be the equivalent of .
Examples using mod
The general rule for using the mod argument is that all values must be of integer type, the 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 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 function with negative , the function will perform as follows:
In other words, the function will compute the modular inverse of base and mod first and then that result will be used in the function as base to be computed as normal with the exp argument being converted to its non-negative counterpart.
Example of modular inverse
In this example, the straight modular inverse is calculated because will be raised to the power 1.
Example of modular inverse when exp is not -1
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,, therefore, we have to get creative in order to calculate nth roots.
We know that is equivalent to . Thus, using this knowledge we can calculate the nth root in Python by using either or .
Examples of calculating nth roots
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
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 moduleof Python, there is a similar function called. To use this we first need to import the math function, thus, the built-in function will be very slightly faster. The main differences between the two functions is that 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, is a better option.
Example of using math.pow()
When to use the pow() function vs when to use the ** operator
When deciding between using the function or the operator, the most important factor to consider would be the efficiency of your code. We can use the function from the module to find out how fast Python executes our code.
Examples of using timeit with simple numbers
When performing a simple power computation, the operator appears to be much faster.
Examples using modulo
The same is true even when we include a modulo operation.
However, when we want to perform power operations with very large numbers, the function is much quicker, showing that the power of the function lies in executing longer computations.
Examples using large numbers
Here the 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 function is much more efficient.
The post A Guide to Python’s pow() Function first appeared on Finxter.
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
Code:
pow(x, y)Code:
pow()Code:
xCode:
yCode:
**Code:
x**yExamples without mod
Code:
>>> pow(5, 2)
25
>>> pow(-3, 3)
-27
>>> pow(2, -2)
0.25Code:
pow(base, exp, mod=None)The
Code:
pow()Code:
baseCode:
expCode:
modCode:
NoneParameter |
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 modulowill be computed.
Return value: The output of
Code:
baseCode:
expUsing the pow() function without the mod argument
When using the
Code:
pow(x, y)Code:
x**yCode:
xCode:
yComparison of the two methods
Code:
>>> pow(6, 4)
1296
>>> 6 ** 4
1296The
Code:
pow()Examples using float types
Code:
>>> pow(3.0, 4)
81.0
>>> pow(4.5, 2.3)
31.7971929089206As 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
Code:
>>> 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
Code:
>>> pow(7, 2)
49
>>> pow(4, -5)
0.0009765625
>>> pow(-6, 3)
-216
>>> pow(-9, -4)
0.00015241579027587258What sets the
Code:
pow()Code:
**Code:
modThe process of operations when using the mod argument is as follows:
If we have
Code:
pow(x, y, z)Code:
xCode:
yCode:
zCode:
(x**y) % zExamples using mod
Code:
>>> pow(14, 7, 5)
4
>>> pow(-8, 3, 5)
3
>>> pow(2, 4, -3)
-2The general rule for using the mod argument is that all values must be of integer type, the
Code:
expCode:
expSo, when using the
Code:
pow()Code:
expCode:
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
Code:
pow()Example of modular inverse
Code:
>>> pow(87, -1, 25)
23In this example, the straight modular inverse is calculated because
Code:
inv_baseExample of modular inverse when exp is not -1
Code:
>>> 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)
10Unfortunately, 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,
Code:
math.sqrt()We know that
Code:
nxCode:
x1nCode:
pow(x, (1/n))Code:
x**(1/n)Examples of calculating nth roots
Code:
>>> pow(625, (1/4))
4.0
>>> 729**(1/3)
8.999999999999998Note 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
Code:
>>> 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 moduleof Python, there is a similar function called
Code:
math.pow()Code:
pow()Code:
math.pow()Code:
math.pow()Example of using math.pow()
Code:
>>> import math
>>> math.pow(9, 5)
59049.0When deciding between using the
Code:
pow()Code:
**Code:
timeit.timeit()Code:
timeitExamples of using timeit with simple numbers
Code:
>>> import timeit
>>> timeit.timeit('pow(5, 2)')
0.25059129999863217
>>> timeit.timeit('5**2')
0.008814800001346157When performing a simple power computation, the
Code:
**Examples using modulo
Code:
>>> timeit.timeit('pow(52, 2, 4)')
0.7482693000001746
>>> timeit.timeit('52**2 % 4')
0.012026999998852261The same is true even when we include a modulo operation.
However, when we want to perform power operations with very large numbers, the
Code:
pow()Code:
pow()Examples using large numbers
Code:
>>> timeit.timeit('pow(5234, 2341, 124)')
0.9020593000004737
>>> timeit.timeit('5234**2341 % 124')
152.56075580000106Here the
Code:
pow()Code:
**Code:
pow()The post A Guide to Python’s pow() Function first appeared on Finxter.

