Python has four ways to calculate the
n-th power (exponent) of
x so that
xⁿ=x*x*...*x that multiplies the base
x with itself, and repeating this
- Method 1: Use the double-asterisk operator such as in
- Method 2: Use the built-in
pow()function such as in
- Method 3: Import the math library and calculate
- Method 4: Import the NumPy library and calculate
Let’s dive into these four methods one by one!
Method 1: Double-Asterisk x**n
The double asterisk (**) symbol is used as an exponentiation operator. The left operand is the base and the right operand is the power. For example, the expression
x**n multiplies the value
x with itself,
Let’s have a look at a couple of simple examples:
>>> 2**2 4 >>> 2**3 8 >>> 2**4 16 >>> 2**5 32 >>> -3**3 -27
You can also raise to a negative power in which case, the whole expression is inverted such that
x**-n == 1/(x**n).
>>> 2**-3 0.125 >>> 2**-2 0.25
Method 2: Built-In pow(x, n)
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
|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.|
Here are a couple of examples without the
>>> pow(5, 2) 25 >>> pow(-3, 3) -27 >>> pow(2, -2) 0.25
If we have a
mod argument such as
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 .
Here are three examples with the mod argument:
>>> pow(14, 7, 5) 4 >>> pow(-8, 3, 5) 3 >>> pow(2, 4, -3) -2
Method 3: math.pow(x, n)
math.pow(x, n) function raises
x to the power of
n. It calculates the exponent function. The difference to the built-in
pow() function is that it doesn’t allow the optional mod argument and it always returns a float, even if the input arguments are integers.
Consider the following examples that show how to use it with integer arguments, float arguments, negative bases, and negative exponents:
>>> math.pow(2, 3) 8.0 >>> math.pow(2.3, 3.2) 14.372392707920499 >>> math.pow(-2, 3) -8.0 >>> math.pow(2, -3) 0.125
Method 4: numpy.power(x, n)
The NumPy library has a
np.power(x, n) function that raises
x to the power of
n. While the inputs can be arrays, when used on numerical values such as integers and floats, the function also works in the one-dimensional case.
>>> np.power(2, 2) 4 >>> np.power(2, 3) 8 >>> np.power(-2, 3) -8 >>> np.power(2.0, -3) 0.125
However, if you try to raise an integer to a negative power, NumPy raises an error:
>>> np.power(2, -3) Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> np.power(2, -3) ValueError: Integers to negative integer powers are not allowed.
To fix it, convert the first integer argument to a float value, for example using the
You’ve learned four ways to calculate the exponent function in Python.
Method 1: Use the double-asterisk operator such as in
Method 2: Use the built-in
pow() function such as in
Method 3: Import the math library and calculate
Method 4: Import the NumPy library and calculate
Thanks for studying with us—you can join our free email academy with cheat sheets and regular free Python lessons here:
Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers.
|+||Addition||Calculating the sum of the two operands|
|–||Subtraction||Subtracting the second operand from the first operand|
|*||Multiplication||Multiplying the first with the second operand|
|/||Division||Dividing the first by the second operand|
|%||Modulo||Calculating the remainder when dividing the first by the second operand|
|//||Integer Division, Floor Division||Dividing the first operand by the second operand and rounding the result down to the next integer|
|**||Exponent||Raising the first operand to the power of the second operand|
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.