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 n
-times.
- Method 1: Use the double-asterisk operator such as in
x**n
. - Method 2: Use the built-in
pow()
function such as inpow(x, n)
. - Method 3: Import the math library and calculate
math.pow(x, n)
. - Method 4: Import the NumPy library and calculate
np.power(x, n)
.
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, n
times.
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)
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
.
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. |
Here are a couple of examples without the mod
argument:
>>> pow(5, 2) 25 >>> pow(-3, 3) -27 >>> pow(2, -2) 0.25
If we have a mod
argument such as z
in 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)
The 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 float()
function.
Summary
You’ve learned four ways to calculate the exponent function in Python.
Method 1: Use the double-asterisk operator such as in x**n
.
Method 2: Use the built-in pow()
function such as in pow(x, n)
.
Method 3: Import the math library and calculate math.pow(x, n)
.
Method 4: Import the NumPy library and calculate np.power(x, n)
.
Thanks for studying with us—you can join our free email academy with cheat sheets and regular free Python lessons here:
Arithmetic Operators
Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Calculating the sum of the two operands | 3 + 4 == 7 |
– | Subtraction | Subtracting the second operand from the first operand | 4 - 3 == 1 |
* | Multiplication | Multiplying the first with the second operand | 3 * 4 == 12 |
/ | Division | Dividing the first by the second operand | 3 / 4 == 0.75 |
% | Modulo | Calculating the remainder when dividing the first by the second operand | 7 % 4 == 3 |
// | Integer Division, Floor Division | Dividing the first operand by the second operand and rounding the result down to the next integer | 8 // 3 == 2 |
** | Exponent | Raising the first operand to the power of the second operand | 2 ** 3 == 8 |