Javatpoint Logo
Javatpoint Logo

The sqrt(): Math Function of Python

The sqrt() function is a built-in function in Python for performing operations related to math. The sqrt() function is used for returning the square root of any imported number.

In this tutorial, we will discuss how we can use the sqrt() function in Python.



'N' is any number which is greater or equal to 0 (N >= 0).


It will return the square root of the number "N", that is passed as the parameter.



The square root of 7:  2.6457513110645907
The square root of 49:  7.0
The square root of 115:  10.723805294763608


We get an error while executing the sqrt() function if the number passed as a parameter is less than "0" due to a runtime error.



The square root of 16:  4.0
ValueError                                Traceback (most recent call last)
<ipython-input-3-79d85b7d1d62> in <module>
      6 # printing the error when x less than 0
----> 7 print ("The square root of -16: ", m.sqrt(-16))

ValueError: math domain error


As at first, we passed "16" as a parameter, and we got the output as "4", but for "-16", which is less than "0", we got an error, saying "math domain error".


We can also use the sqrt() function for creating an application for performing math functions. Suppose we have a number "N", of which we have to check whether it is a prime number or not.


We will use the following approach:

  • Step1: we will import the math module.
  • Step 2: we will create a function "check_if" for checking if the given number is prime or not.
  • Step 3: we will check if the number is equal to 1. If "yes", it will return false.
  • Step 4: we will run a for loop from range "2" to "sqrt(N)".
  • Step 5: we will check if any number between the given range divides "N".




Enter the number you want to check:  12
The number is not prime


Enter the number you want to check:  11
The number is prime


Enter the number you want to check:  53
The number is prime


In this tutorial, we have discussed how to use Python's sqrt() function for calculating the square root of any number greater than 0.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA