Mastering Python (89 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (83 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How to find Square Root in Python?

Last updated on Jul 15,2021 19.1K Views

23 / 62 Blog from Python Fundamentals

We all have come across square roots in mathematics. It is undeniably one of the most important fundamentals and hence needs to be embedded in various applications. Python comes in handy to serve this purpose by making it really simple to integrate Square Roots in our programs. In this article, you will be learning how to find Square roots in Python.

Before going forward, let us take a look at the topics covered over here:

What is a Square root?

The square root is any number y such that x= y. Mathematically it is represented as x = √y. Python provides built-in methods to calculate square roots.

Now that we have a basic idea on what is a square root of a number and how to represent it, let’s move ahead and check how we can get the square root of a number in Python.

How to calculate the Square root in Python?

To calculate Square roots in Python, you will need to import the math module. This module consists of built-in methods namely sqrt() and pow() using which you can calculate the square roots. You can import it by simply using the import keyword as follows:

import math

Once this module is imported, you can make use of any function present within it.

Using sqrt() function

The sqrt() function basically take one parameter and returns the square root it. The syntax of this function is:

SYNTAX:

sqrt(x)   # x is the number whose square root needs to be calculated.

Now, let us take a look at an example of this function:

EXAMPLE:

from math import sqrt   #absolute importing
print(sqrt(25))

OUTPUT:    5.0

As you can see, the square root of 25 i.e 5 has been returned.

NOTE: In the above example, the sqrt() function has been imported using the absolute method. However, if you import the complete math module, you can execute the same as follows:

EXAMPLE:

import math
print(math.sqrt(25))

OUTPUT:    5.0

Using pow() function

Another method to compute the square root of any number is by using the pow() function. This function basically takes two parameters and multiplies them to compute the results. This is done in order to the mathematical equation where,

x= y or y=x**.5

The syntax of this function is as follows:

SYNTAX:

pow(x,y)  # where y is the power of x or x**y 

Now let us take a look at an example of this function:

EXAMPLE:

from math import pow
print(pow(25,.5))

OUTPUT:    5.0

These functions can be used to solve many of the mathematical problems. Let us now take a look at the working example of one such application of these functions.

A working example of Square root in Python

Let us try to implement the very famous Pythagoras Theorem using these functions.

Problem statement:

Accept values of 2 sides of a triangle and calculate the value of its hypotenuse. 

Solution:

Pythagoras theorem states that in a right-angled triangle, the side opposite to the right angle called the hypotenuse is measured as the square root of the sum of squares of measures of the other two sides, which means

c=√(a2+b2)   # where c is the hypotenuse 

Here is the solution in Python:

from math import sqrt  #Imported the square root function from math module
from math import pow     #Imported the power function from math module

a=int(input("Enter the measure of one side of a right angled triangle:"))    
b=int(input("Enter the measure of another side of a right angled triangle:"))    
#input function is used to take input from user and is stored as string
# which is then typecasted into an integer using the int() function.
c=sqrt(pow(a,2)+pow(b,2))       #we have implemented the formula c=√(a2+b2)
print(f"The measure of the hypotenuse is: {c} based on the measures of the other two sides {a} & {b}")      

OUTPUT:

Enter the measure of one side of a right-angled triangle :3
Enter the measure of another side of a right-angled triangle: 4

The measure of the hypotenuse is: 5.0 based on the measures of the other two sides 3 & 4

This brings us to the end of this article on Square Root in Python. I hope you have understood everything clearly.

Make sure you practice as much as possible and revert your experience.  

To get in-depth knowledge on Python along with its various applications, you can enroll for live Python certification course online with 24/7 support and lifetime access. 

Got a question for us? Please mention it in the comments section of this “Square Root in Python” blog and we will get back to you as soon as possible.

Upcoming Batches For Data Science with Python Certification Course
Course NameDateDetails
Data Science with Python Certification Course

Class Starts on 30th March,2024

30th March

SAT&SUN (Weekend Batch)
View Details
Data Science with Python Certification Course

Class Starts on 22nd June,2024

22nd June

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to find Square Root in Python?

edureka.co