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 numpy.roots() function - Python numpy.roots() function return the roots of a polynomial with coefficients given in p. The values in the rank-1 array p are coefficients of a polynomial. If the length of p is n+1 then the polynomial is described by: p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n] Syntax : numpy.roots(p) Parame 1 min read How to bind arguments to given values in Python functions? In Python, binding arguments to specific values can be a powerful tool, allowing you to set default values for function parameters, create specialized versions of functions, or partially apply a function to a set of arguments. This technique is commonly known as "partial function application" and ca 3 min read 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 Mathematical Functions in Python | Set 1 (Numeric Functions) In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number 3 min read Use return value in another function - python In Python, one functionâs return value can be used in another, making code cleaner and more modular. This approach simplifies tasks, improves code reuse, and enhances readability. By breaking down logic into smaller functions that share data, you create flexible and maintainable programs. Letâs expl 2 min read round() function in Python Python round() function is a built-in function available with Python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer. In this 6 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 Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This functi 3 min read Python | math.floor() function In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.floor() function returns the largest integer not greater than x. If number is already integer, same number is returned. Syntax: math.floor(x) Parameter: x: This is a numeric e 1 min read Like