🌐
W3Schools
w3schools.com › python › ref_math_sqrt.asp
Python math.sqrt() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... # Import math Library import math # Return the square root of different numbers print (math.sqrt(9)) print (math.sqrt(25)) print (math.sqrt(16)) Try it Yourself »
🌐
Tutorialspoint
tutorialspoint.com › home › python › python square root function
Python Square Root Function
February 21, 2009 - # importing the module import math num = -1 # taking a number less than zero res = math.sqrt(num) # printing the result print('The square root of negative number is:',res)
🌐
Real Python
realpython.com › python-square-root-function
The Python Square Root Function – Real Python
November 3, 2024 - Can you find the square root in ... by using the exponent operator ** or the built-in pow() function. For example, 4**0.5 will return 2.0, giving you the same result as math.sqrt()....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-math-function-sqrt
Python math.sqrt() function | Find Square Root in Python - GeeksforGeeks
February 14, 2025 - The formula used is c = sqrt(a^2 + b^2). It calculates the hypotenuse c using the values of a and b. math.sqrt() does not work for negative numbers.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
Return the Euclidean norm, sqrt(sum(x**2 for x in coordinates)).
🌐
Programiz
programiz.com › python-programming › examples › square-root
Python Program to Find the Square Root
# Find square root of real or complex numbers # Importing the complex math module import cmath num = 1+2j # To take input from the user #num = eval(input('Enter a number: ')) num_sqrt = cmath.sqrt(num) print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
🌐
Codecademy
codecademy.com › docs › python › math module › math.sqrt()
Python | Math Module | math.sqrt() | Codecademy
September 12, 2025 - The Python .sqrt() function is ... of the math library. If the number is negative, the function raises a ValueError. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today. ... Returns a floating-point number representing the square root of num. This example uses Python .sqrt() to calculate ...
🌐
Scaler
scaler.com › home › topics › python math.sqrt() method
Python math.sqrt() Method - Scaler Topics
February 23, 2024 - It's a precise and efficient way to compute square roots, crucial for various mathematical computations. For instance, math.sqrt(25) returns 5.0, showcasing its utility in finding the square root of 25.
🌐
Enki
enki.com › post › how-to-square-numbers-in-python-sqrt
Enki | Blog - How to Square Numbers in Python - sqrt
In this case, math.sqrt(number) returns the square root of the given number. Knowing how to find square roots is crucial for tasks that involve retracing squared values, which often occur in complex computations. By learning these methods to square numbers in Python, you unlock the flexibility and ease of use that Python offers for mathematical operations.
Find elsewhere
🌐
TradingCode
tradingcode.net › python › math › square-root
How to calculate the square root in Python? - Math
For that simply set the list’s value to the outcome of the list comprehension. For example: # Take the square root of each value in the 'values' # list, overwriting the original list values = [math.sqrt(value) for value in values] The second option that gets the square roots from a list is with Python’s for loop.
🌐
Python Examples
pythonexamples.org › python-math-sqrt
Python math.sqrt() - Square Root
Square root of a negative number. sqrt() raises ValueError. import math x = -3 result = math.sqrt(x) print('sqrt(x) :', result)
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .sqrt()
Python:NumPy | Math Methods | .sqrt() | Codecademy
April 9, 2025 - ... The function returns an array ... array. This example demonstrates how to use NumPy’s .sqrt() function to calculate the square root of each element in a 1D array:...
Top answer
1 of 11
107

Option 1: math.sqrt()

The math module from the standard library has a sqrt function to calculate the square root of a number. It takes any type that can be converted to float (which includes int) and returns a float.

>>> import math
>>> math.sqrt(9)
3.0

Option 2: Fractional exponent

The power operator (**) or the built-in pow() function can also be used to calculate a square root. Mathematically speaking, the square root of a equals a to the power of 1/2.

The power operator requires numeric types and matches the conversion rules for binary arithmetic operators, so in this case it will return either a float or a complex number.

>>> 9 ** (1/2)
3.0
>>> 9 ** .5  # Same thing
3.0
>>> 2 ** .5
1.4142135623730951

(Note: in Python 2, 1/2 is truncated to 0, so you have to force floating point arithmetic with 1.0/2 or similar. See Why does Python give the "wrong" answer for square root?)

This method can be generalized to nth root, though fractions that can't be exactly represented as a float (like 1/3 or any denominator that's not a power of 2) may cause some inaccuracy:

>>> 8 ** (1/3)
2.0
>>> 125 ** (1/3)
4.999999999999999

Edge cases

Negative and complex

Exponentiation works with negative numbers and complex numbers, though the results have some slight inaccuracy:

>>> (-25) ** .5  # Should be 5j
(3.061616997868383e-16+5j)
>>> 8j ** .5  # Should be 2+2j
(2.0000000000000004+2j)

(Note: the parentheses are required on -25, otherwise it's parsed as -(25**.5) because exponentiation is more tightly binding than negation.)

Meanwhile, math is only built for floats, so for x<0, math.sqrt(x) will raise ValueError: math domain error and for complex x, it'll raise TypeError: can't convert complex to float. Instead, you can use cmath.sqrt(x), which is more accurate than exponentiation (and will likely be faster too):

>>> import cmath
>>> cmath.sqrt(-25)
5j
>>> cmath.sqrt(8j)
(2+2j)

Precision

Both options involve an implicit conversion to float, so floating point precision is a factor. For example let's try a big number:

>>> n = 10**30
>>> x = n**2
>>> root = x**.5
>>> root == n
False
>>> root - n  # how far off are they?
0.0
>>> int(root) - n  # how far off is the float from the int?
19884624838656

Very large numbers might not even fit in a float and you'll get OverflowError: int too large to convert to float. See Python sqrt limit for very large numbers?

Other types

Let's look at Decimal for example:

Exponentiation fails unless the exponent is also Decimal:

>>> decimal.Decimal('9') ** .5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'decimal.Decimal' and 'float'
>>> decimal.Decimal('9') ** decimal.Decimal('.5')
Decimal('3.000000000000000000000000000')

Meanwhile, math and cmath will silently convert their arguments to float and complex respectively, which could mean loss of precision.

decimal also has its own .sqrt(). See also calculating n-th roots using Python 3's decimal module

2 of 11
25

SymPy

Depending on your goal, it might be a good idea to delay the calculation of square roots for as long as possible. SymPy might help.

SymPy is a Python library for symbolic mathematics.

import sympy
sympy.sqrt(2)
# => sqrt(2)

This doesn't seem very useful at first.

But sympy can give more information than floats or Decimals:

sympy.sqrt(8) / sympy.sqrt(27)
# => 2*sqrt(6)/9

Also, no precision is lost. (√2)² is still an integer:

s = sympy.sqrt(2)
s**2
# => 2
type(s**2)
#=> <class 'sympy.core.numbers.Integer'>

In comparison, floats and Decimals would return a number which is very close to 2 but not equal to 2:

(2**0.5)**2
# => 2.0000000000000004

from decimal import Decimal
(Decimal('2')**Decimal('0.5'))**Decimal('2')
# => Decimal('1.999999999999999999999999999')

Sympy also understands more complex examples like the Gaussian integral:

from sympy import Symbol, integrate, pi, sqrt, exp, oo
x = Symbol('x')
integrate(exp(-x**2), (x, -oo, oo))
# => sqrt(pi)
integrate(exp(-x**2), (x, -oo, oo)) == sqrt(pi)
# => True

Finally, if a decimal representation is desired, it's possible to ask for more digits than will ever be needed:

sympy.N(sympy.sqrt(2), 1_000_000)
# => 1.4142135623730950488016...........2044193016904841204
🌐
LearnDataSci
learndatasci.com › solutions › python-square-root
Python Square Root: Real and Complex – LearnDataSci
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-5c74ec90b955> in <module> ----> 1 math.sqrt(-4) ValueError: math domain error ... Depending on how you want to handle square roots of negatives, handling a ValueError may be preferable. Alternatively, we can avoid this by using cmath.sqrt(), as we'll see in the next section. You can also calculate the square root of negative and complex numbers using the cmath library. See below for an example of this:
🌐
Hyperskill
hyperskill.org › university › python › math-in-python
Math in Python
August 2, 2024 - In this example, the math module's sqrt function is applied to the number 9, and the result is then printed. This code would output 3.0. The square root function plays a role in mathematics helping to determine the number that when multiplied by itself results in a specified value.
🌐
Codedamn
codedamn.com › news › python
How to calculate square root in Python (with examples)
March 18, 2024 - The square root of a number is equivalent to raising that number to the power of 0.5. Here’s how you can use this method: number = 9 sqrt = number ** 0.5 print(f"The square root of {number} is {sqrt}")
🌐
Analytics Vidhya
analyticsvidhya.com › home › python square root
Python Square Root
October 10, 2024 - ... It is used when working with complex numbers or when negative square roots need to be calculated. In Python, the exponentiation operator (**) can be used to calculate square roots by raising a number to the power of 1/2 (0.5).
🌐
W3Schools
w3schools.com › python › ref_math_isqrt.asp
Python math.isqrt() Method
# Import math Library import math # Print the square root of different numbers print (math.sqrt(10)) print (math.sqrt (12)) print (math.sqrt (68)) print (math.sqrt (100)) # Round square root downward to the nearest integer print (math.isqrt(10)) ...
🌐
Flexiple
flexiple.com › python › square-root-in-python
How to calculate Square Root in Python? - Flexiple
In the first line, we begin with importing the math module, then in the next line we take the input from the user. After that we find the square root of the number using the sqrt() method and the result will be stored in the variable, sqRoot. The final line of code makes sure that the result is printed out. The cmath module is used to calculate the square root of a Real or Complex number in Python...
🌐
Career Karma
careerkarma.com › blog › python › python sqrt(): a how-to guide
Python sqrt(): A How-To Guide
July 20, 2022 - Now you can see why it makes sense that the output will always be a double, even if the square root of a number is as simple as “9”. You can also pass in a variable that represents a number: yourValue= 90 math.sqrt(yourValue) # 9.486832980505138