4 Methods to Calculate Square Root in Python

Square Root In Python

In this tutorial, we are going to discuss the different ways to calculate square root in Python.


What is a square root?

In Mathematics, a square root of a number ‘p‘ is a number ‘q‘ which follows the condition p = q2. In Python, we have so many methods to calculate the square root of numbers. Let’s discuss some well-known methods in Python to calculate the square root of numbers.

1. Calculate square root using the exponent operator

In this method, we will define our own function to find the square root of a number. And to calculate the square root of a number we will be using the exponent operator (**) in Python.

The defined function will take a number as an argument and return the square root of the number if it’s positive else it will print a warning. Let’s implement this in Python code.

# Define the user defined sqrt() function
# to calculate the square root of a number
def sqrt(N):
    if N < 0:
        print('Square root of negative number does not exist!')
        return
    else:
        print(f'Square root of number {N}: {N**0.5}')
        return

# Call the above defined sqrt() function
# to calculate the square root of a number
sqrt(441)
sqrt(0.81)
sqrt(6.25)
sqrt(634)
sqrt(-121)

Output:

Square root of number 441: 21.0 
Square root of number 0.81: 0.9 
Square root of number 6.25: 2.5 
Square root of number 634: 25.179356624028344
Square root of negative number does not exist!

2. Using the sqrt() function

In Python, the sqrt() function is a predefined function that is defined in the math module. The sqrt() function returns the square root of the number passed as an argument. Let’s see how we can use the built-in sqrt() function in a Python program.

# Import Python math module
import math as m

# Call the predefined sqrt() function
# to calculate the square root of a number
print(f'Square root of number 121: {m.sqrt(121)}')
print(f'Square root of number 0.49: {m.sqrt(0.49)}')
print(f'Square root of number 4.41: {m.sqrt(4.41)}')
print(f'Square root of number 265: {m.sqrt(265)}')

Output:

Square root of number 121: 11.0 
Square root of number 0.49: 0.7 
Square root of number 4.41: 2.1
Square root of number 265: 16.278820596099706

NOTE: If a negative number is passed as an argument to the built-in sqrt() function then it will throw a math domain error. Let’s see an example.

# Import Python math module
import math as m

# Call the predefined sqrt() function
# to calculate the square root of a negative number
m.sqrt(-125)

Output:

Math Domain Error Sqrt
math domain error: sqrt()

3. Using the pow() function

In this method to calculate square root, we will be using the built-in pow() function. In Python, the pow() function is a predefined function that is defined in the math module. The pow() function takes two arguments one is base and the other is the exponent/power and returns the square root of the number (base) passed as the first argument. To calculate the square root the exponent/power argument is fixed to 0.5. Let’s see how we can use the built-in pow() function in a Python program.

# Import Python math module
import math as m

# Call the predefined pow() function
# to calculate the square root of a number
print(f'Square root of number 625: {m.pow(625, 0.5)}')
print(f'Square root of number 0.64: {m.pow(0.64, 0.5)}')
print(f'Square root of number 1.21: {m.pow(1.21, 0.5)}')
print(f'Square root of number 7: {m.pow(7, 0.5)}')

Output:

Square root of number 625: 25.0 
Square root of number 0.64: 0.8 
Square root of number 1.21: 1.1 
Square root of number 7: 2.6457513110645907

NOTE: Here also if a negative number is passed as an argument to the built-in pow() function then it will throw a math domain error. Let’s see an example.

# Import Python math module
import math as m

# Call the predefined pow() function
# to calculate the square root of a negative number
m.pow(-121, 0.5)

Output:

Math Domain Error Pow
math domain error: pow()

4. Using built-in np.sqrt() function

In this method of finding the square root, we will be using the built-in np.sqrt() function. In Python, the np.sqrt() function is a predefined function that is defined in the numpy module. The np.sqrt() function returns a numpy array where each element is the square root of the corresponding element in the numpy array passed as an argument. Let’s see how we can use the built-in np.sqrt() function in a Python program.

# Import Python numpy module
import numpy as np

# Define a numpy array
arr = np.array([0, 225, 0.36, 6.25, 10, -15])
print('NumPy array:')
print(arr)

# Call the predefined np.sqrt() function
# to calculate the square root of each element
# in the numpy array
print('Returned NumPy array with Square roots:')
print(np.sqrt(arr))

Output:

NumPy array: 
[  0.   225.     6.25  10.   -15.  ]
Returned NumPy array with Square roots: 
[ 0.         15.          2.5         3.16227766         nan] 
<ipython-input-29-541b85f9361a>:13: RuntimeWarning: invalid value encountered in sqrt   print(np.sqrt(arr))

NOTE: If there is a negative number in the numpy array and it is passed to the built-in np.sqrt() function then it will throw a RuntimeWarning saying that an invalid value is encountered in sqrt. And set a nan value at the place of the square root of the negative element in the returned numpy array.

Conclusion

In this tutorial, we have learned the different ways to calculate the square root of numbers in Python. We have also learned how to use Python functions like math.sqrt(), math.pow(), and numpy.sqrt(). Hope you have understood the things well and are excited to explore and learn more.