Programs

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! 

 

 

Want to share this article?

Plan your Software Development Career Now!

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks