# Python square root | Python sqrt function example

Python square root can be done by using a math module sqrt function. Python sqrt function is inbuilt in a math module, you have to import the math package (module). The sqrt function in a python programming language returns the square root of any number (number > 0). In this tutorial, you will learn how to get the square root of any number in python with various ways and examples.

### Syntax

Following is the syntax for Python sqrt() function.

```import math

math.sqrt(x)```

### Parameters

X –  is a given value, which you want to square root.

### Return Value

Will return value floating number of the square root of x for x > 0.

## Python square root function Example

Here is an example to the square root of 9, using a math module and python sqrt function.

```import math

# print the square root of 9
print(math.sqrt(9))```

Output: 3.0

### Python square root without math module

You can do square toot in python without importing a math module. It can be done by creating a python square function with a return value in the float.

Like we are using a simple calculation statement x ** 0.5 to get the square root of a number. This approach is slightly more flexible and it also works for complex inputs:

```x = float(input("Please Enter number "))

fx = x ** 0.5
print(fx)```

Output:

### QA: What if you pass the negative or zero value in sqrt function?

If you pass the negative value in sqrt function, then python throws an error – `ValueError: math domain error`. Here is an example and see error output.

```import math

# negative value
print(math.sqrt(-1))
```

Output:

But if you pass the value Zero, Then it returns the 0.0.

It can your interview question – How to find square root a number in python 3?
Must Read math module for better understanding – Python math module | Python import math

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Square root and python sqrt function examples are in Python 3, so it may change its different from python 2 or upgraded versions.

This site uses Akismet to reduce spam. Learn how your comment data is processed.