How to Find Square Root in Python?

Wajiha Urooj
Edureka
Published in
4 min readJul 23, 2019

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?
  • How to Calculate the Square root in Python:
  • Using sqrt() function
  • Using pow() function
  • A working example of Square root in Python

What is a Square root?

The square root is any number y such that . 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 , 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,

The syntax of this function is as follows:

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.

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

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.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Machine Learning Classifier in Python

2. Python Scikit-Learn Cheat Sheet

3. Machine Learning Tools

4. Python Libraries For Data Science And Machine Learning

5. Chatbot In Python

6. Python Collections

7. Python Modules

8. Python developer Skills

9. OOPs Interview Questions and Answers

10. Resume For A Python Developer

11. Exploratory Data Analysis In Python

12. Snake Game With Python’s Turtle Module

13. Python Developer Salary

14. Principal Component Analysis

15. Python vs C++

16. Scrapy Tutorial

17. Python SciPy

18. Least Squares Regression Method

19. Jupyter Notebook Cheat Sheet

20. Python Basics

21. Python Pattern Programs

22. Web Scraping With Python

23. Python Decorator

24. Python Spyder IDE

25. Mobile Applications Using Kivy In Python

26. Top 10 Best Books To Learn & Practice Python

27. Robot Framework With Python

28. Snake Game in Python using PyGame

29. Django Interview Questions and Answers

30. Top 10 Python Applications

31. Hash Tables and Hashmaps in Python

32. Python 3.8

33. Support Vector Machine

34. Python Tutorial

Originally published at https://www.edureka.co on July 23, 2019.

--

--