Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] A Guide to Python’s pow() Function
1
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
Code:
pow(x, y)
, the
Code:
pow()
function returns the value of
Code:
x
raised to the power
Code:
y
. It performs the same function as the power operator
Code:
**
, i.e.
Code:
x**y
, but differs in that it comes with an optional argument called mod.

Examples without mod

Code:
>>> pow(5, 2) 25 >>> pow(-3, 3) -27 >>> pow(2, -2) 0.25
Parameters and Syntax

Code:
pow(base, exp, mod=None)


The
Code:
pow()
function includes two compulsory arguments,
Code:
base
and
Code:
exp
, and one optional argument,
Code:
mod
, whose default value is
Code:
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 modulowill be computed.

Return value: The output of
Code:
base
raised to the power
Code:
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
Code:
pow(x, y)
function without the optional mod argument, it will perform the same operation as the power operator
Code:
x**y
, raising
Code:
x
to the power
Code:
y
.

Comparison of the two methods

Code:
>>> pow(6, 4) 1296 >>> 6 ** 4 1296

The
Code:
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 floatnumber as one or both of the arguments, the function will automatically return a float type.

Examples using float types

Code:
>>> 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

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.00015241579027587258
Using the pow() function with a mod argument

What sets the
Code:
pow()
function apart from the
Code:
**
operator is its third optional argument,
Code:
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
Code:
pow(x, y, z)
, the function first performs the task of raising
Code:
x
to the power
Code:
y
and then that result is used to perform the modulo task with respect to
Code:
z
. It would be the equivalent of
Code:
(x**y) % z
.

Examples using mod

Code:
>>> 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
Code:
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
Code:
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
Code:
pow()
function with negative
Code:
exp
, the function will perform as follows:

Code:
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()
function as base to be computed as normal with the  exp argument being converted to its non-negative counterpart.

Example of modular inverse

Code:
>>> pow(87, -1, 25) 23

In this example, the straight modular inverse is calculated because
Code:
inv_base
will be raised to the power 1.

Example 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) 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,
Code:
math.sqrt()
, therefore, we have to get creative in order to calculate nth roots.

We know that
Code:
nx
is equivalent to
Code:
x1n
. Thus, using this knowledge we can calculate the nth root in Python by using either
Code:
pow(x, (1/n))
or
Code:
x**(1/n)
. 

Examples of calculating nth roots

Code:
>>> 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

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()
. To use this we first need to import the math function, thus, the built-in
Code:
pow()
function will be very slightly faster. The main differences between the two functions is that
Code:
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,
Code:
math.pow()
is a better option.

Example of using math.pow()

Code:
>>> 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
Code:
pow()
function or the
Code:
**
operator, the most important factor to consider would be the efficiency of your code. We can use the
Code:
timeit.timeit()
function from the
Code:
timeit
module to find out how fast Python executes our code.

Examples of using timeit with simple numbers

Code:
>>> import timeit >>> timeit.timeit('pow(5, 2)') 0.25059129999863217 >>> timeit.timeit('5**2') 0.008814800001346157

When performing a simple power computation, the
Code:
**
operator appears to be much faster.

Examples using modulo

Code:
>>> 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
Code:
pow()
function is much quicker, showing that the power of the
Code:
pow()
function lies in executing longer computations.

Examples using large numbers

Code:
>>> timeit.timeit('pow(5234, 2341, 124)') 0.9020593000004737 >>> timeit.timeit('5234**2341 % 124') 152.56075580000106

Here the
Code:
pow()
function is extremely fast compared to the
Code:
**
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
Code:
pow()
function is much more efficient.

The post A Guide to Python’s pow() Function first appeared on Finxter.
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: