The Python power function pow() from the math module allows us to perform exponentiation and find roots of numbers easily.
import math
square_of_4 = math.pow(4,2)
sqrt_of_4 = math.pow(4,1/2)
The Python math module has many powerful functions which make performing certain calculations in Python very easy.
One such calculation which is very easy to perform in Python is finding the value of numbers to the certain power.
We can use the Python power function pow() from the math module to do exponentiation and find roots of numbers easily.
The pow() function takes two numbers as input, the first number is the base and the second number is the exponent.
For example, for squaring in Python, we pass “2” to the second parameter in the pow() function.
Below are some examples of how to use the pow() function to find the squares of various numbers.
import math
print(math.pow(4, 2))
print(math.pow(9, 2))
print(math.pow(13, 2))
print(math.pow(90, 2))
print(math.pow(2182, 2))
#Output:
16.0
81.0
169.0
8100.0
4761124.0
Exponentiating Numbers in Python with math.pow()
We can use the math pow() function in Python to perform exponentiation for any exponent.
For example, if we want to find the value of a number to the 3rd power, we pass “3” as the second argument.
import math
print(math.pow(4, 3))
print(math.pow(9, 3))
#Output:
64.0
729.0
To find the value of a number to the 5th power, we pass “5” as the second argument.
import math
print(math.pow(4, 5))
print(math.pow(9, 5))
#Output:
1024.0
59049.0
Finding the Square Root of a Number in Python with math.pow()
The pow() function from the Python math module also lets us compute square roots.
For a square root, we pass “1/2” to the second parameter in the pow() function. For roots, we must pass positive numbers to pow() for the first argument.
Below are some examples of how to use the pow() function to find square roots.
import math
print(math.pow(4, 1/2))
print(math.pow(9, 1/2))
print(math.pow(13, 1/2))
print(math.pow(90, 1/2))
print(math.pow(2182, 1/2))
#Output:
2.0
3.0
3.605551275463989
9.486832980505138
46.71188285650665
Finding the nth Root of a Number in Python with math.pow()
The pow() function from the Python math module also lets us compute nth roots.
For a nth root, given any n, we pass “1/n” to the second parameter in the pow() function. Again, for roots, we must pass positive numbers to pow() for the first argument.
Below are some examples of how to use the pow() function to find nth roots.
import math
#n=2
print(math.pow(4,1/2))
#n=3
print(math.pow(9,1/3))
#n=5
print(math.pow(13,1/5))
#n=6
print(math.pow(90,1/6))
#n=9
print(math.pow(2182,1/9))
#Output:
2.0
2.080083823051904
1.6702776523348104
2.1169328630254585
2.3495455051249885
Finding the Roots of Negative Numbers in Python
If you try to pass a negative number to the math pow() function, you will get a ValueError.
There are two ways you can find the roots of negative numbers in Python.
If the root is odd (3rd, 5th, 7th, etc.), you can use the built in exponentiation operator **.
import math
#n=9
print(math.pow(-2182,1/9))
#Output:
-2.3495455051249885
If the root is 2, you can use the square root function sqrt() from Python cmath module.
import cmath
print(cmath.sqrt(-2182))
#Output:
46.71188285650665j
Hopefully this article has been beneficial for you to learn how to use the Python power function pow() from the math module to exponentiate and find roots of numbers.