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 python programming language that returns the square root of any number (number > 0). This tutorial you will learn how to get the square root of any number in python with various ways and examples.

Python square root, Python sqrt function example math


Following is the syntax for Python sqrt() function.



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.

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 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:


Python square root without math module 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.


pass the negative or zero value in sqrt function 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.

Leave a Reply

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