Python math.sqrt() function | Find Square Root in Python

Last Updated : 20 Dec, 2023

sqrt() function returns square root of any number. It is an inbuilt function in Python programming language.

sqrt() Function

We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root of different numbers by using sqrt() function.

Python3

 `# Python3 program to demonstrate the ` `# sqrt() method `   `# import the math module ` `import` `math `   `# print the square root of  0 ` `print``(math.sqrt(``0``)) `   `# print the square root of 4` `print``(math.sqrt(``4``)) `   `# print the square root of 3.5` `print``(math.sqrt(``3.5``)) `

Output

```0.0
2.0
1.8708286933869707```

Definition of math.sqrt() Function

sqrt() function in Python is an in-built function, and it is present in the math library.

You can use the sqrt function after importing the math library.

`import math`

sqrt() function only takes a value greater than or equal to 0.

math.sqrt() Method Syntax

math.sqrt(x)

Parameter

x: is any number such that x>=0

Returns: It returns the square root of the number passed in the parameter.

sqrt() Function Examples

Let’s look at some different uses of math.sqrt() function.

Example 1: Check if Prime or Not

In this example, we are given a number and we are checking if a number is prime or not. Here,run a loop from 2 to sqrt(n) and check if any number in range (2-sqrt(n)) divides n.

Python3

 `# Python program for practical application of sqrt() function ` `# import math module ` `import` `math `   `# function to check if prime or not ` `def` `check(n): ` `    ``if` `n ``=``=` `1``: ` `        ``return` `False` `        `  `        ``# from 1 to sqrt(n) ` `    ``for` `x ``in` `range``(``2``, (``int``)(math.sqrt(n))``+``1``): ` `        ``if` `n ``%` `x ``=``=` `0``: ` `            ``return` `False` `    ``return` `True`   `# driver code ` `n ``=` `23` `if` `check(n): ` `    ``print``(``"prime"``) ` `else``: ` `    ``print``(``"not prime"``)`

Output

`prime`

Example 2: Finding Hypotenuse Of a Triangle

In this example, we are using sqrt() function to find the hypotenuse of a triangle.

Python3

 `a ``=` `10` `b ``=` `23`   `import` `math`   `# importing the math module` `c ``=` `math.sqrt(a ``*``*` `2` `+` `b ``*``*` `2``)`   `print``( ``"The value for the hypotenuse would be "``, c)`

Output

`The value for the hypotenuse would be  25.079872407968907`

sqrt() Function Error

When x<0 it does not execute due to a runtime error. In this example, we can see that we cannot calculate the Python square root if the number is less than zero.

Python3

 `# Python3 program to demonstrate the error in` `# sqrt() method`   `# import the math module` `import` `math`   `# print the error when x<0` `print``(math.sqrt(``-``1``))`

Output

```Traceback (most recent call last):
File "/home/67438f8df14f0e41df1b55c6c21499ef.py", line 8, in
print(math.sqrt(-1))
ValueError: math domain error```

This was all about the sqrt() function that is used to find square root in Python. Finding square root in Python is very easy with this in-built function.

For more Math Library Functions : Python Math Module