DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

How to calculate Square Root in Python?

In this short tutorial, we look at how we can calculate the square root in Python. We look at four different methods and break them down for you.

Table of contents

What is a square root?

The square root of a number is a value when multiplied by itself returns that same number.

For example, 6 x 6 = 36, so a square root of 36 is 6. However -6 x -6 is 36 too, so -6 is also a square root of 36.

In Python or any other Programming Language we have various methods to calculate the square root of a number. And in this tutorial, we will cover four different methods to calculate the square root of a number.

Using the pow() function to calculate the square root

The pow() function is a quick method of finding the square root in Python.

Let us first understand how the pow() function works in Python.
The pow() function takes 2 parameters, the first parameter is the numerical value, and the second parameter is the power of the numerical value.

Syntax:

pow(x,y) # where y is the power of x
Enter fullscreen mode Exit fullscreen mode

Input:

# Using the pow() function 
import math
num = float(input(" Enter a number: "))
sqRoot = math.pow(num, 0.5)
print("The square root of a given number {0} = {1}".format(num, sqRoot)) 
Enter fullscreen mode Exit fullscreen mode

Output:

Enter a number: 25
The square root of a given number 25.0 = 5.0
Enter fullscreen mode Exit fullscreen mode

Calculating the square root in using the exponent operator

The exponential operator, denoted with ** performs the square root operation as same as the pow() function.

To make things more interesting, let’s find the square root of a number by defining a function of our own.

Input:

# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
    if n < 0:
        return
    else:
        return n**0.5
        print(sqRoot(36))
Enter fullscreen mode Exit fullscreen mode

Output:

6.0
Enter fullscreen mode Exit fullscreen mode

We have begun by defining a function named sqRoot(n). We have then added the equation, n**0.5, which is to calculate the square root and store the result in the variable x. When we call the function we have defined, we input the number we want to find the square root of in the place of the argument or parameter n. The function is then called to implement the action and print the result.

Using the sqrt() function to calculate square root

The sqrt() function is a predefined method used to find the square root in Python. Firstly, we import the math module to use the sqrt() function.

Input:

# Using the sqrt() function to calculate the square root in Python
import math
num = int(input("Enter a number:"))
sqRoot = math.sqrt(num)
print (f"The square root of {num} is " ,sqRoot)
Enter fullscreen mode Exit fullscreen mode

Output:

Enter a number:16
The square root of 16 is 4.0
Enter fullscreen mode Exit fullscreen mode

In the first line, we begin with importing the math module, then in the next line we take the input from the user. After that we find the square root of the number using the sqrt() method and the result will be stored in the variable, sqRoot. The final line of code makes sure that the result is printed out.

Calculating the square root in Python using the cmath module

The cmath module is used to calculate the square root of a Real or Complex number in Python. The various methods we have used so far will work fine for all positive Real numbers. But for negative or complex numbers, the cmath module proves to be useful.

Input:

# Using the cmath module to calculate the square root of real or complex numbers in Python
import math
num = eval(input(Enter a number: )
num_sqRoot = cmath.sqrt(num)
print(The square root of {0} is {1:0.3f}+{2:0.3f}j.format(num, num_sqRoot.real, num_sqRoot.imag))
Enter fullscreen mode Exit fullscreen mode

Output:

Enter a number: 4+4j
The square root of (4+4j) is 2.197+0.910j
Enter fullscreen mode Exit fullscreen mode

In this program, we used the sqrt() function in the cmath module. Note that we have used the eval() function to convert the inputs to complex numbers as well.
The cmath.sqrt() also can be used to return the square-root of a negative number. For example-

Input:

import cmath
a = -25
print(cmath.sqrt(a)) 
Enter fullscreen mode Exit fullscreen mode

Output:

5j
Enter fullscreen mode Exit fullscreen mode

Closing thoughts

In this tutorial, we have learnt how to calculate the square root of a number in Python by using the sqrt() function, the exponent operator, the pow() function and the cmath module.

If you need to work with whole numbers instead of floating point numbers; math.isqrt() outputs the square as an integer and rounds down to the nearest whole number. The sqrt() function can also be used with libraries other than the “math” library such as numPy, a Python library used for working with arrays.

Top comments (1)

Collapse
 
elcharitas profile image
Jonathan Irhodia

Your concise arrangements and menrions of the various possible ways of getting this simple task done is truly note worthy!!

This would have been helpful some 3years back when I started python. Im sure it will be to many other developers here.