View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

How to Compute Square Roots in Python

By Pavan Vadapalli

Updated on Mar 08, 2025 | 4 min read | 13.0k views

Share:

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.

What is a Square Root?

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

Square Root Function in Python

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:

1. Using the Python math module:

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 

2. Using the Python cmath module

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

3. Using the exponent ** operator:

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

Placement Assistance

Executive PG Program13 Months
View Program
background

Liverpool John Moores University

Master of Science in Machine Learning & AI

Dual Credentials

Master's Degree19 Months
View Program

Advance Your Career with upGrad’s Advanced Certificate in Machine Learning and Deep Learning

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! 

Conclusion

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.

Frequently Asked Questions (FAQs)

1. How to do a square root in Python?

2. How to find the square root of a list in Python?

3. How to find square in Python code?

4. How do you type square root in Python keyboard?

5. How to find square root?

6. How to calculate square root in Python without sqrt?

7. What is the square root function in Python?

8. How to find square root in Python using NumPy?

9. How to find the square root of a negative number in Python?

10. What is the sqrt function in Python?

11. How to find the square root in Python using loops?

Pavan Vadapalli

899 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive Program in Generative AI for Leaders

76%

seats filled

View Program

Top Resources

Recommended Programs

LJMU

Liverpool John Moores University

Master of Science in Machine Learning & AI

Dual Credentials

Master's Degree

19 Months

View Program
IIITB
bestseller

IIIT Bangalore

Executive Diploma in Machine Learning and AI

Placement Assistance

Executive PG Program

13 Months

View Program
IIITB

IIIT Bangalore

Post Graduate Certificate in Machine Learning & NLP (Executive)

Career Essentials Soft Skills Program

Certification

8 Months

View Program