How to Find Square Root of a Number Using Python

How to Find Square Root of a Number Using Python

3 mins read5.9K Views Comment
Updated on Aug 23, 2023 17:15 IST

The below article demonstrates different ways to find the Square Root of a Number using Python programming language.

2022_06_how-to-find-square-root-of-a-number-in-python.jpg

In Python programming, there are various predefined methods to calculate the square root of a number. And in this article, we will discuss 4 different ways to find the square root of positive, negative, real as well as complex numbers. So, without further ado, let’s begin!

Also Read: Introduction to Python – Features, Use Cases, Resources, and Applications

We will be covering the following sections today:

What is a Square Root?

The square root is a factor of a number that, when multiplied by itself, returns the number itself. For example, the square root of 49 is 7, because 7 x 7 = 49. Similarly, 3 is a square root of 9, since 3 x 3 = 9. Also, it is interesting to note that -3 x -3 = 9 as well, so -3 is also a square root of 9!

Methods to Find Square Root of a Number in Python 

Using math module – sqrt()

We have an in-built square root function in the Python math library, called sqrt(), that directly returns the square root of any number when applied to it:

#Import the math library 
import math 
 
#Print the square root of 36
print("Square root of 36 is: ", math.sqrt(36)) 
 
#Print the square root of 6
print("Square root of 6 is: ",math.sqrt(6)) 
 
#Print the square root of 1.5
print("Square root of 1.5 is: ",math.sqrt(1.5)) 

Output:

output1

We can also take input from the user and display its square root. The number can be an integer as well as a float value: 

#Import the math library 
import math 
 
#Enter input
num = float(input("Enter numeric value : "))
 
squareRoot = math.sqrt(num)
print("The Square Root of {0} is: {1}".format(num, squareRoot))

Output:

output
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
3 months
– / –
2 weeks
– / –
16 weeks
β‚Ή1.7 K
3 months
– / –
– / –
β‚Ή4.24 K
2 weeks
β‚Ή3 K
3 weeks
– / –
4 months

Using math module – pow()

We have another in-built function in the Python math library that can be used to find the square root of a number – the pow() method.

It takes 2 arguments as input – the number and the power of that number. We can use it to find the square root of a number by applying a power of 0.5 to it, as shown:

#Import the math library 
import math 
 
#Enter input
num = float(input("Enter numeric value : "))
 
squareRoot = math.pow(num, 0.5)
print("The Square Root of {0} is: {1}".format(num, squareRoot))

Output 1:

output2

Output 2:

output3

Using arithmetic operator – exponent

On the same lines as the previous method, we can find the square root of a number by simply exponentiating it to the power of 0.5 using the exponent (**) operator, as shown:

#Import the math library 
import math 
 
#Enter input
num = float(input("Enter numeric value : "))
 
squareRoot = num ** 0.5
print("The Square Root of {0} is: {1}".format(num, squareRoot))

Output:

output4

Using cmath module – sqrt()

In this method, we use the sqrt() function of the cmath module to calculate the square root of a real or complex number in Python. this method is specifically used for negative or complex numbers:

#Import the cmath library 
import cmath 
 
#Enter input
num = eval(input("Enter numeric value : "))
 
squareRoot = cmath.sqrt(num)
print("The Square Root of {0} is: {1}".format(num, squareRoot))
 

Output 1:

output5

Output 2:

output6

Endnotes

With this, we have come to the end of our article. Hope this was helpful for you to understand how to find the square root of a number using different methods in Python. Want to learn more about Python and practice Python programming? Explore related articles here.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio