How to Compute Square Roots in Python

How to Compute Square Roots in Python

Last updated:
2nd Feb, 2023
Views
3 Mins
View All

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

 Master of Science in Machine Learning & AI from LJMU Executive Post Graduate Programme in Machine Learning & AI from IIITB Advanced Certificate Programme in Machine Learning & NLP from IIITB Advanced Certificate Programme in Machine Learning & Deep Learning from IIITB Executive Post Graduate Program in Data Science & Machine Learning from University of Maryland To Explore all our courses, visit our page below. Machine Learning Courses

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

 Artificial Intelligence Courses Tableau Courses NLP Courses Deep Learning Courses

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

 IoT: History, Present & Future Machine Learning Tutorial: Learn ML What is Algorithm? Simple & Easy Robotics Engineer Salary in India : All Roles A Day in the Life of a Machine Learning Engineer: What do they do? What is IoT (Internet of Things) Permutation vs Combination: Difference between Permutation and Combination Top 7 Trends in Artificial Intelligence & Machine Learning Machine Learning with R: Everything You Need to Know AI & ML Free Courses Introduction to NLP Fundamentals of Deep Learning of Neural Networks Linear Regression: Step by Step Guide Artificial Intelligence in the Real World Introduction to Tableau Case Study using Python, SQL and Tableau

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!

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
Select Area of interest
Select Work Experience
By clicking 'Submit' you Agree to
UpGrad's Terms & Conditions

Suggested Blogs

3568
BFS is a graph traversal technique to explore and analyse graphs. It systematically visits all the neighbouring vertices of a current vertex before mo

27 Jul 2023

5501
For those who are familiar with the technologies, the difference between CPU and GPU is relatively simple. However, to better understand the differenc

24 Feb 2023

5273
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

20 Feb 2023

5349
Is machine learning possible without maths? Absolutely Not. Machine learning is entirely about maths. It is an application of artificial intelligence