Python math.sqrt() function | Find Square Root in Python Last Updated : 14 Feb, 2025 Comments Improve Suggest changes Like Article Like Report math.sqrt() returns the square root of a number. It is an inbuilt function in the Python programming language, provided by the math module. In this article, we will learn about how to find the square root using this function.Example: Python import math # square root of 0 print(math.sqrt(0)) # square root of 4 print(math.sqrt(4)) # square root of 3.5 print(math.sqrt(3.5)) Output0.0 2.0 1.8708286933869707 We need to import math before using this function.import mathmath.sqrt() syntaxmath.sqrt(x)Parameter:x: A number greater than or equal to 0.Returns:The square root of the number x.math.sqrt() examplesLet's look at some different uses of math.sqrt() .Example 1: Check if number is primemath.sqrt() can be used to optimize prime number checking. We only need to check divisibility up to the square root of the number. Python import math n = 23 # Check if 23 is prime # Check if n is equal to 1 if n == 1: print("not prime") else: # Loop from 2 to the square root of n for x in range(2, int(math.sqrt(n)) + 1): if n % x == 0: print("not prime") break # Exit the loop else: print("prime") Outputprime ExplanationIf n > 1, we check for factors from 2 to sqrt(n).If any divisor is found, it prints "Not prime".If no divisors are found, it prints "Prime".Example 2: Finding hypotenuse of a triangleWe can use math.sqrt() to find the hypotenuse of a right-angled triangle using the Pythagorean theorem. Python a = 10 b = 23 import math c = math.sqrt(a ** 2 + b ** 2) print(c) OutputThe value for the hypotenuse would be 25.079872407968907ExplanationThe formula used is c = sqrt(a^2 + b^2).It calculates the hypotenuse c using the values of a and b.Error handlingmath.sqrt() does not work for negative numbers. It raises a ValueError if we pass a number less than 0. Python import math # error when x<0 print(math.sqrt(-1)) OutputTraceback (most recent call last): File "/home/67438f8df14f0e41df1b55c6c21499ef.py", line 8, in print(math.sqrt(-1)) ValueError: math domain errorExplanationSquare roots of negative numbers are not real numbers.math.sqrt() only works with non-negative values. Comment More infoAdvertise with us Next Article Python math.sqrt() function | Find Square Root in Python S Striver Follow Improve Article Tags : Misc Python Python-Built-in-functions Practice Tags : Miscpython Similar Reads Python Value Error :Math Domain Error in Python Errors are the problems in a program due to which the program will stop the execution. One of the errors is 'ValueError: math domain error' in Python. In this article, you will learn why this error occurs and how to fix it with examples. What is 'ValueError: math domain error' in Python?In mathemati 4 min read Compute the square root of complex inputs with scimath in Python In this article, we will cover how to compute the square root of complex inputs with scimath in Python using NumPy. ExampleInput: [-1 -2] Output: [0.+1.j 0.+1.41421356j] Explanation: Square root of complex input.NumPy.emath.sqrt method The np.emath.sqrt() method from the NumPy library calculates the 2 min read numpy.square() in Python numpy.square(arr, out = None, ufunc 'square') : This mathematical function helps user to calculate square value of each element in the array. Parameters : arr : [array_like] Input array or object whose elements, we need to square. Return : An array with square value of each array. Code #1 : Working 3 min read Python | cmath.sqrt() method With the help of cmath.sqrt() method, we can find the value of square root any number by passing it to cmath.sqrt() method. Syntax : cmath.sqrt(value) Return : Return the square root of any value. Example #1 : In this example we can see that by using cmath.sqrt() method, we are able to get the value 1 min read Python | Decimal sqrt() method Decimal#sqrt() : sqrt() is a Decimal class method which returns the Square root of a non-negative number to context precision. Syntax: Decimal.sqrt() Parameter: Decimal values Return: the Square root of a non-negative number to context precision. Code #1 : Example for sqrt() method Python3 # Python 2 min read Like