AppDividend
Latest Code Tutorials

# Python sqrt: How To Calculate Square Root in Python

In algebra, the square is the result of a number, n, multiplied by itself, which means n. So the equation becomes x = n². To calculate the square of a number in Python, check out the square tutorial.

The square root of the number is a number n, which, when multiplied by itself, yields the square, x. Let’s see how to calculate the square root of a number in the Python programming language.

## How to Calculate Square root in Python

To calculate the square root of the value, use the Python sqrt() method. The sqrt() is a built-in math library function in Python that returns the square root of x for x > 0. To use the sqrt() method, import math module. We can import the math module and then call the sqrt() function to get the square root of any given value.

### Syntax

The syntax for the Python sqrt() function is the following.

`math.sqrt(number)`

### Argument

The sqrt() function takes a number as an argument.

### Return Value

It returns the square root of a number argument.

### Implementation of sqrt() function

Import math module to work with mathematical operations in Python.

See the following example.

```# app.py

import math

print(math.sqrt(81))
```

See the output.

It is the best way to find the square root of any number.

Now, there is another way that will give us the same output, but the number must have positive, not negative, and not complex. See the below example.

```// app.py

num = 49

sq_root = num ** 0.5
print(sq_root)```

See the output.

## Python sqrt: Square root of a complex number

To calculate a square root of a complex number in Python, use the cmath module. To use the cmath module, you need to import it first and then use its sqrt() function.

See the following example.

```// app.py

import cmath

print(cmath.sqrt(1 + 2j))
```

See the output.

## Python sqrt: Square root of float value

To calculate the square root of the float value, use the math.sqrt() function in Python. Pass the float value to the math.sqrt() function and it will return the square root of that value.

See the following example.

```// app.py

import math

print(math.sqrt(math.pi))
```

See the output.

## Python sqrt: Calculate Square root of negative value

To calculate the square root of the negative value, use the math.sqrt() function in Python. Pass the negative value to the math.sqrt() function and it will return the square root of the negative value.

```import math

print(math.sqrt(-9))```

#### Output

`ValueError: math domain error`

We get the ValueError. The error says math domain error.

To solve the math domain error in Python, we need to prevent the user so that he cannot calculate the square root of a negative number before we execute the math.sqrt() function.

```import math

x = -81
if int(x) >= 0:
math.sqrt(x)
else:
print("You can not calculate square root of negative number")
```

#### Output

`You can not calculate square root of negative number`

And it resolved the issue by preventing it from happening.

That’s it for finding a square root in the Python tutorial.