How to Calculate a Square Root in Python
The square root of a number is a number that gives the original number when squared. The square root of 9 is 3 because 3x3 = 9.
In Python we can calculate the square root of a number using the power operator, which is **
(two asterisks.)
To use it, place the original number to the left of the power operator, raise the value to the power of 0.5 and store the result in a variable. Here is an example of getting the square root of 16:
original = 16
squ_root = original ** 0.5
print(squ_root)
4.0
Get Square Root with the Python math Package
For a cleaner syntax, you can use the .sqrt()
function from the Python math
package.
import math
original = 23
squ_root = math.sqrt(original)
print(squ_root)
4.795831523312719
math