In Python, the easiest way we can find the square root of a positive number is to use the sqrt() function from the Python math module.

import math

sqrt_of_10 = math.sqrt(10)

If you want to find the square root of a negative number, you should use the Python cmath module. This will return an imaginary number.

import cmath

sqrt_of_m10 = cmath.sqrt(-10)

You can also use the pow() function from the Python math module to compute square roots.

import math

sqrt_of_10 = math.pow(10,1/2)

Finally, you can use the built in ** operator to find the square root of a number.

sqrt_of_10 = 10**(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 square root of a number.

We can use the sqrt() function from the math module to find the square root of a number. The sqrt() function input has to be a positive number.

Below are some examples of using the sqrt() function to find the square root of a number in Python.

import math

print(math.sqrt(4))
print(math.sqrt(9))
print(math.sqrt(13))
print(math.sqrt(90))
print(math.sqrt(2182))

#Output:
2.0
3.0
3.605551275463989
9.486832980505138
46.71188285650665

If you try to input a negative number to the sqrt() function, you will get a ValueError. To get the square root of a negative number in Python, you can use the cmath module.

Finding the Square Root of Negative Numbers in Python

To find the square root of a negative number in Python, you can use the cmath module. The sqrt() function from the Python cmath module returns an imaginary number.

Below are some examples of how to find the square root of a negative number in Python.

import cmath

print(cmath.sqrt(-4))
print(cmath.sqrt(-9))
print(cmath.sqrt(-13))
print(cmath.sqrt(-90))
print(cmath.sqrt(-2182))

#Output:
2.0j
3.0j
3.605551275463989j
9.486832980505138j
46.71188285650665j

Finding the Square Root of a Number with pow() Function in Python

There are some other ways we can find the square root of a number in Python. The pow() function from the Python math module also lets us compute square roots.

The pow() function takes two numbers as input, the first number is the base and the second number is the exponent. The first number must be positive, but the second number can be negative.

For a square root, we pass “1/2” to the second parameter in the pow() function.

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

The Python pow() function can be useful if you want to find the cube root of a number or the nth root of a number in Python.

Finding the Square Root of a Number with the ** Operator in Python

We can also use the built in ** to find exponents in Python. To find a square root with the ** operator, we just put “(1/2)” after **.

Below are some examples of how to use the Python built in ** operator to find square roots.

import math

print(4**(1/2))
print(9**(1/2))
print(13**(1/2))
print(90**(1/2))
print(2182**(1/2))

#Output:
2.0
3.0
3.605551275463989
9.486832980505138
46.71188285650665

Finding the Square Root of a Number Without the Python math Module

Finally, we can estimate the square root of a number without the Python math module. To compute the square root in Python without the Python math module, we can employ the help of Newton’s Method.

Newton’s method is a root finding algorithm which can help us find an approximation of a function.

We can use Newton’s method to find the square root of a number in Python.

Below is a function which you can use to utilize Newton’s method to find an approximation for the square root of a number to precision level “a”. For a comparison, we will also use the sqrt() function from the Python math module.

import math

def newton_sqrt(n,a):
    x = n
    while(True):
        root = 0.5*(x+(n/x))
        if (abs(root-x) < a):
            break
        x = root
    return root

print(math.sqrt(13))
print(newton_sqrt(13,0.1))

print(math.sqrt(50))
print(newton_sqrt(50,0.0001))

print(math.sqrt(100))
print(newton_sqrt(100,0.000001))

print(math.sqrt(313))
print(newton_sqrt(313,0.00000001))

#Output:
3.605551275463989
3.6063454894655185

7.071067811865477
7.0710678118654755

10.0
10.0

17.69180601295413
17.69180601295413

As shown above, Newton's method allows us to get a pretty good approximation of the square root of a number without using the math module.

Hopefully this article has been beneficial for you to learn how to find the square root of a number in Python.

Categorized in:

Python,

Last Update: March 20, 2024