How to Compute Square Roots in Python
Updated on Mar 08, 2025 | 4 min read | 13.0k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 08, 2025 | 4 min read | 13.0k views
Share:
Table of Contents
A high-level, multi-paradigm programming language with an object-oriented approach, Python is designed to be highly extensible, with multiple compact modules, including a multi-functional math module.
Here, we explore the different ways in which Square Roots in Python can calculate a very specific mathematical functionality – square roots – with or without the use of its math and cmath modules.
Enroll for the Machine Learning Course from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.
While most of us are familiar with this mathematical concept, it’s worth refreshing our memory with a simple definition: The value ‘y’ is x’s square root because ‘y’, when multiplied by itself, yields the original number x.
In mathematical terms this may be expressed as follows:
If x = y x y
or x = y2
then √x = y
A number’s square root may be extricated using Python in a variety of different ways. The most commonly used square root function in Python is math.sqrt(), which provides an efficient way to compute the square root of a given number.
However, Python also allows alternative approaches, such as:
A. With the inbuilt math.sqrt( ) function:
Step 1: Import the math module
Step 2: Use the sqrt( ) function
Input code:
Import math
Print(“54’s square root is” ,math.sqrt(49))
Output:
54’s square root is 7.348469228349534
B. With the inbuilt math.pow( ) function:
Step 1: Import the math module
Step 2: Use the pow( ) function
This operates on the simple mathematical principle: √x = x1/2 or √x = x0.5.
The function requires the input of two parameters – the base number and its exponent.
Input code:
Import math
number = float(input(” Please Enter any numeric Value : “))
squareRoot = math.pow(number, 0.5)
print(“The given number {0}’s square root = {1}”.format(number, squareRoot))
Output:
Please Enter any numeric Value: 54
The Given Number 54.0’s square root = 7.348469228349534
Check out upGrad’s Advanced Certification in DevOps
Step 1: Import the complex math (cmath) module
Step 2: Use the cmath.sqrt( ) function
The cmath module helps compute the square root of real or complex numbers.
Input code:
import cmath
num = 1+2j
sqrt = cmath.sqrt(num)
print(‘{0}’s square root is {1:0.2f} + {2:0.2f}’ .format(num,sqrt.real,sqrt.imag))
Output:
(1+2j)’s square root is 1.27+0.79
Operates on the same principle as the pow( ) function, i.e. √x = x1/2 or √x = x0.5
but does not require users to import the math module
Input code:
def sqrt(n):
if n < 0:
Return
Else:
return n**0.5
print(sqrt(54))
Output:
7.348469228349534
By using the sqrt in Python, you can efficiently compute square roots with built-in functions and mathematical principles
At upGrad, our Advanced Certificate in Machine Learning and Deep Learning, offered in collaboration with IIIT-B, is an 8-month course taught by industry experts to give you a real-world idea of how deep learning and machine learning work. In this course, you’ll get a chance to learn important concepts around machine learning, deep learning, computer vision, cloud, neural networks, and more.
Check out the course page and get yourself enrolled soon!
Computing the square root in Python is simple and can be done using various methods. The most common approach is using the math.sqrt() function, while the exponentiation operator (**0.5) provides an alternative without importing modules.
For working with arrays, numpy.sqrt() is efficient, and if dealing with negative numbers, cmath.sqrt() handles complex roots. Additionally, custom algorithms like the Newton-Raphson method can be used for iterative approximations.
By understanding these different techniques, you can efficiently compute the square root in Python based on your specific use case.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Top Resources