In this tutorial, we’re going to break down the process of exponentiation in Python. In mathematics, exponentiation is an operation that multiplies a number by itself several times. Python provides built-in operators and functions for performing exponentiation.
The ** operator for exponentiation
Many developers think of the caret symbol
(^) as the operator for powers, because that is what it stands for in mathematics. In most programming languages, however, this symbol acts as a bitwise
xor. In Python, the powers up operator is denoted by two asterisk characters
** between the base and the number of the power. The functionality of this operator complements that of the multiplication operator
*: the only difference is that the second operator specifies how many times the first operand will be multiplied by itself.
To multiply the number 5 by itself 6 times, we use
** between the base 5 and the operand of degree 6. Conclusion:
Let’s check the operator with other values. Initialize an integer, a negative integer, zero, and two
float numbers, one greater than zero and one less than zero. We assign random values to the degrees.
num1 = 2 num2 = -5 num3 = 0 num4 = 1.025 num5 = 0.5 print(num1, '^12 =', num1**12) print(num2, '^4 =', num2**4) print(num3, '^9999 =', num3**9999) print(num4, '^-3 =', num4**-3) print(num5, '^8 =', num5**8)
2 ^12 = 4096 -5 ^4 = 625 0 ^9999 = 0 1.025 ^-3 = 0.928599410919749 0.5 ^8 = 0.00390625
pow() or math.power() for powers
You can also power up in Python using
pow() or the math module, which has its own implementation of the same module. In both functions, we need to pass two arguments: the base and the power itself. Let’s try calling both functions and see the result.
import math print(pow(-8, 7)) print(math.pow(-8, 7)) print(pow(2, 1.5)) print(math.pow(2, 1.5)) print(pow(4, 3)) print(math.pow(4,3)) print(pow(2.0, 5)) print(math.pow(2.0, 5))
-2097152 -2097152.0 2.8284271247461903 2.8284271247461903 64 64.0 32.0 32.0
The only difference is that
math.pow() always returns a floating-point number, even if you pass integers. But
pow() will return a floating-point number if at least one of the arguments was the same.
numpy.np() for exponentiation
The numpy module has its own
power() function for multiplication. It takes the same arguments as
pow(), where the first is the base and the second is the value of the degree. Let’s display the same results.
print(np.power(-8, 7)) print(np.power(2, 1.5)) print(np.power(4, 3)) print(np.power(2.0, 5))
-2097152 2.8284271247461903 64 32.0
How do I get the square of a number in Python? To square a number, you need to specify
2 as a power. There is no built-in function for getting the square in Python. For example, the square of a number is 6 –
6**2 -> 36.
Comparing the running times of different solutions
Now let’s compare how long it takes for each of the three functions and the
** operator to execute. To do this, we use the
timeit module. The base is 2 and the value of the power is 9999999.
import numpy as np import math import time start = time.process_time() val = 2**9999999 print('** for', time.process_time() - start, 'ms') start = time.process_time() val = pow(2, 9999999) print('pow() per', time.process_time() - start, 'ms') start = time.process_time() val = np.power(2, 9999999) print('np.power() per', time.process_time() - start, 'ms') start = time.process_time() val = math.pow(2, 9999999) print('math.pow() per', time.process_time() - start, 'ms')
** for 0.078125 ms pow() for 0.0625 ms np.power() in 0.0 ms Traceback (most recent call last): File "C:\Programs\Python\Python38\test.py", line 18, in val = math.pow(2, 9999999) OverflowError: math range error
The first thing to note is that
math.pow() returned an
OverflowError. This means that the function does not support large degree values. The differences between the others are quite simple, but you can see that
np.power() is the fastest.