How to Find Square Root of a Number Using Python
The below article demonstrates different ways to find the Square Root of a Number using Python programming language.
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:
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:
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:
Output 2:
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:
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:
Output 2:
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.
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