The Wayback Machine - https://web.archive.org/web/20241002131451/https://www.geeksforgeeks.org/python-math-function-sqrt/
Open In App

Python math.sqrt() function | Find Square Root in Python

Last Updated : 20 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

sqrt() function returns square root of any number. It is an inbuilt function in Python programming language.

In this article, we will learn more about the Python Program to Find the Square Root.

sqrt() Function

We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root of different numbers by using sqrt() function.

Python3




# Python3 program to demonstrate the
# sqrt() method
 
# import the math module
import math
 
# print the square root of  0
print(math.sqrt(0))
 
# print the square root of 4
print(math.sqrt(4))
 
# print the square root of 3.5
print(math.sqrt(3.5))


Output

0.0
2.0
1.8708286933869707

Definition of math.sqrt() Function

sqrt() function in Python is an in-built function, and it is present in the math library.

You can use the sqrt function after importing the math library.

import math

sqrt() function only takes a value greater than or equal to 0.

math.sqrt() Method Syntax

math.sqrt(x)

Parameter

x: is any number such that x>=0

Returns: It returns the square root of the number passed in the parameter.

sqrt() Function Examples

Let’s look at some different uses of math.sqrt() function.

Example 1: Check if Prime or Not

In this example, we are given a number and we are checking if a number is prime or not. Here,run a loop from 2 to sqrt(n) and check if any number in range (2-sqrt(n)) divides n.

Python3




# Python program for practical application of sqrt() function
# import math module
import math
 
# function to check if prime or not
def check(n):
    if n == 1:
        return False
         
        # from 1 to sqrt(n)
    for x in range(2, (int)(math.sqrt(n))+1):
        if n % x == 0:
            return False
    return True
 
# driver code
n = 23
if check(n):
    print("prime")
else:
    print("not prime")


Output

prime

Example 2: Finding Hypotenuse Of a Triangle

In this example, we are using sqrt() function to find the hypotenuse of a triangle.

Python3




a = 10
b = 23
 
import math
 
# importing the math module
c = math.sqrt(a ** 2 + b ** 2)
 
print( "The value for the hypotenuse would be ", c)


Output

The value for the hypotenuse would be  25.079872407968907

sqrt() Function Error

When x<0 it does not execute due to a runtime error. In this example, we can see that we cannot calculate the Python square root if the number is less than zero.

Python3




# Python3 program to demonstrate the error in
# sqrt() method
 
# import the math module
import math
 
# print the error when x<0
print(math.sqrt(-1))


Output

Traceback (most recent call last):
  File "/home/67438f8df14f0e41df1b55c6c21499ef.py", line 8, in 
    print(math.sqrt(-1)) 
ValueError: math domain error

This was all about the sqrt() function that is used to find square root in Python. Finding square root in Python is very easy with this in-built function.

For more Math Library Functions : Python Math Module



Similar Reads

Python | sympy.sqrt() method
With the help of sympy.sqrt() method, we can find the square root of any number by using sympy.sqrt() method. Syntax : sympy.sqrt(number) Return : Return square root of any number. Example #1 : In this example we can see that by using sympy.sqrt() method, we can get the square root of any number. # import sympy import sympy # Use sympy.sqrt() metho
1 min read
Python | sympy.sqrt() method
With the help of sympy.sqrt() method, we can find the square root of any value in terms of values and also in terms of symbolically simplified mathematical expression. Syntax: sqrt(val) Parameters: val – It is the value on which square root operation is to be performed. Returns: Returns square root in terms of values and symbolically simplified mat
1 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 values of square root by passing value to it. # importi
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 # Python Program explaining # sqrt() method # loading decimal libra
2 min read
numpy.sqrt() in Python
numpy.sqrt(array[, out]) function is used to determine the positive square root of an array, element-wise. Syntax: numpy.sqrt() Parameters: array : [array_like] Input values whose square roots have to be determined. out : [ndarray, optional] Alternate array object in which to put the result; if provided, it must have the same shape as arr. Returns
3 min read
Compute the square root of negative input with emath in Python
In this article, we will cover how to compute the square root of negative inputs with emath in Python using NumPy. Example:Input: [-3,-4] Output: [0.+1.73205081j 0.+2.j ] Explanation: Square root of a negative input.NumPy.emath.sqrt method: The np.emath.sqrt() method from the NumPy library calculates the square root of complex inputs. A complex val
2 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 square root of complex inputs. A complex value is
3 min read
Python math function | modf()
modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Syntax : modf(number) Time Complexity: O(1) Auxiliary Space: O(1) Parameter : There is only one mandatory parameter which is the numbe
3 min read
Python math function | hypot()
hypot() function is an inbuilt math function in Python that return the Euclidean norm, [Tex]\sqrt{(x*x + y*y)} [/Tex]. Syntax : hypot(x, y) Parameters : x and y are numerical values Returns : Returns a float value having Euclidean norm, sqrt(x*x + y*y). Error : When more than two arguments are passed, it returns a TypeError. Note : One has to impor
2 min read
Python | math.fabs() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.fabs() function returns the absolute value of the number. Syntax: math.fabs(x) Parameter: x: This is a numeric expression. Returns: the absolute value of the number. Time Complexity: O(1) Auxiliary Space: O(1) Code #1: C/C++
1 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 expression. Returns: largest integer not greater th
1 min read
Python math function | copysign()
math.copysign() is a function exists in Standard math Library of Python. This function returns a float value consisting of magnitude from parameter x and the sign (+ve or -ve) from parameter y. Syntax : math.copysign(x, y) Parameters : x : Integer value to be converted y : Integer whose sign is required Returns : float value consisting of magnitude
1 min read
Python | math.ceil() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.ceil() function returns the smallest integral value greater than the number. If number is already integer, same number is returned. Syntax: math.ceil(x) Parameter: x: This is a numeric expression. Returns: Smallest integer no
1 min read
Python | math.factorial() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.factorial() function returns the factorial of desired number. Syntax: math.factorial(x) Parameter: x: This is a numeric expression. Returns: factorial of desired number. Time Complexity: O(n) where n is the input number. Auxi
2 min read
Python | math.copysign() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.copysign(a, b) function return a float with the magnitude (absolute value) of a but the sign of b. Syntax: math.copysign(a, b) Parameter: a, b: numeric values. Returns: Return absolute value of a but the sign of b. Code #1: #
1 min read
Python | math.gcd() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments. Syntax: math.gcd(x, y) Parameter: x : Non-negative integer whose gcd has to be computed. y : Non-negative integer whose gcd has to be
2 min read
Python | math.cos() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.cos() function returns the cosine of value passed as argument. The value passed in this function should be in radians. Syntax: math.cos(x) Parameter: x : value to be passed to cos() Returns: Returns the cosine of value passed
2 min read
Python | math.tan() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.tan() function returns the tangent of value passed as argument. The value passed in this function should be in radians. Syntax: math.tan(x) Parameter: x : value to be passed to tan() Returns: Returns the tangent of value pass
1 min read
Python | math.sin() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.sin() function returns the sine of value passed as argument. The value passed in this function should be in radians. Syntax: math.sin(x) Parameter: x : value to be passed to sin() Returns: Returns the sine of value passed as
1 min read
Python - math.acos() function
Math module contains a number of functions which is used for mathematical operations. The math.acos() function returns the arc cosine value of a number. The value passed in this function should be in between -1 to 1. Syntax: math.acos(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be passed to acos() Retu
2 min read
Python - math.acosh() function
Math module contains a number of functions which is used for mathematical operations. The math.acosh() function returns the hyperbolic arc cosine value of a number. The value passed in this function should be greater than or equal to 1. Syntax: math.acosh(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be
2 min read
Python - math.asin() function
Math module contains a number of functions which is used for mathematical operations. The math.asin() function returns the arc sine value of a number. The value passed in this function should be between -1 to 1. Syntax: math.asin(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be passed to asin() Returns:T
2 min read
Python - math.asinh() function
Math module contains a number of functions which is used for mathematical operations. The math.asinh() function returns the hyperbolic arc sine value of a number. Syntax: math.asinh(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be passed to asinh() Returns:This function returns the hyperbolic arc sine va
1 min read
Python - math.atan() function
Math module contains a number of functions which is used for mathematical operations. The math.atan() function returns the arctangent of a number as a value. The value passed in this function should be between -PI/2 and PI/2 radians. Syntax: math.atan(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be pass
2 min read
Python - math.atanh() function
Math module contains a number of functions which is used for mathematical operations. The math.atanh() function returns the hyperbolic arctangent of a number as a value. The value passed in this function should be between -0.99 to 0.99. Syntax: math.atanh(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be
1 min read
Python - math.sinh() function
Math module contains a number of functions which is used for mathematical operations. The math.sinh() function returns the hyperbolic sine value of a number. Syntax: math.sinh(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be passed to sinh() Returns:This function returns the hyperbolic sine value of a nu
2 min read
Python - math.cosh() function
Math module contains a number of functions which is used for mathematical operations. The math.cosh() function returns the hyperbolic cosine value of a number. Syntax: math.cosh(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be passed to cosh() Returns:This function returns the hyperbolic cosine value of
2 min read
Python - math.tanh() function
Math module contains a number of functions which is used for mathematical operations. The math.tanh() function returns the hyperbolic tangent value of a number. Syntax: math.tanh(x) Parameter:This method accepts only single parameters. x :This parameter is the value to be passed to tanh() Returns:This function returns the hyperbolic tangent value o
2 min read
Python math function - nextafter()
math.nextafter() is a function introduced in python 3.9.0. nextafter(x,y) returns the next float value after x towards y, if x is equal to y then y is returned. Syntax: math.nextafter(x, y) Parameter: x and y are two integers/floating point values. Returns: Return the next floating-point value after x towards y. If x is equal to y, return y. Exampl
1 min read
Python - math.ulp(x) function
ULP stands for “Unit in the Last Place”. math.ulp() is introduced in python 3.9.0 version, it returns the value of the least significant bit of the float x. In numerical analysis and computer science, unit of least precision (ULP) or unit in the last place is the spacing between floating-point numbers. Note: If the argument is a NaN (not a number),
2 min read
Practice Tags :