Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconMachine Learningbreadcumb forward arrow iconHow to Compute Square Roots in Python

How to Compute Square Roots in Python

Last updated:
2nd Feb, 2023
Views
Read Time
3 Mins
share image icon
In this article
Chevron in toc
View All
How to Compute Square Roots in Python

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 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 Functionality in Python

A number’s square root may be extricated using Python in a variety of different ways:

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 

Best Machine Learning and AI Courses Online

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

In-demand Machine Learning Skills

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

Popular AI and ML Blogs & Free Courses

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! 

 

 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Explore Free Courses

Suggested Blogs

What is BFS Algorithm? Breath First Search Algorithm Explained
3363
BFS is a graph traversal technique to explore and analyse graphs. It systematically visits all the neighbouring vertices of a current vertex before mo
Read More

by Pavan Vadapalli

27 Jul 2023

CPU vs GPU in Machine Learning? Which is Important
5445
For those who are familiar with the technologies, the difference between CPU and GPU is relatively simple. However, to better understand the differenc
Read More

by Rohit Sharma

24 Feb 2023

Machine Learning vs Data Analytics: A Brief Comparison
5233
Data is also called the new ‘oil’ of this century. Meaning data is as precious for the functioning of a business in the 21st century as cr
Read More

by Pavan Vadapalli

20 Feb 2023

Maths for Machine Learning Specialisation
5300
Is machine learning possible without maths? Absolutely Not. Machine learning is entirely about maths. It is an application of artificial intelligence
Read More

by Pavan Vadapalli

20 Feb 2023

Machine Learning for Java Developers
5905
Machine Learning in Java: Machine learning has taken over the industry and is rising at a rapid rate. Machine learning gives algorithms a chance to l
Read More

by Pavan Vadapalli

19 Feb 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon