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

๐ŸŒ
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 ยป
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ math.html
math โ€” Mathematical functions
1 week ago - For a two dimensional point (x, y), this is equivalent to computing the hypotenuse of a right triangle using the Pythagorean theorem, sqrt(x*x + y*y).
Discussions

math - How do I calculate square root in Python? - Stack Overflow
How can I do it in Python? The inputs will probably be all positive integers, and relatively small (say less than a billion), but just in case they're not, is there anything that might break? Note: This is an attempt at a canonical question after a discussion on Meta about an existing question with the same title. ... Comments are not for extended discussion; this conversation has been moved to chat. ... The math module from the standard library has a sqrt ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Issue with math.sqrt not working
I am having an issue with programming within IDEL. In this example I am trying to perform math.sqrt and it is giving me a name error. This is pretty much exactly like the textbook is showing me, so I am unsure what the issue is. I have done the following: import sqrt from math, etc. More on discuss.python.org
๐ŸŒ discuss.python.org
0
February 17, 2023
What's the better way of taking the square root of a number?
Test it! Run a loop of a 100 numbers, trying both methods and storing the results in a Dataframe. Test to see if the result is equal whilst you loop. You could even time each method to see if one is marginally more efficient than the other More on reddit.com
๐ŸŒ r/learnpython
10
15
December 25, 2022
Is there any down sides of using n**0.5 instead of math.squared(n) ? Besides readability?
In case anyone wonders, OP means math.sqrt() and not math.squared() My guess would be that math.sqrt is faster since it is in a math library, but I have not tested it. You could make a little piece of code that checks the time taken for a bunch of each calculation to see. More on reddit.com
๐ŸŒ r/learnpython
42
44
March 18, 2024
๐ŸŒ
Real Python
realpython.com โ€บ python-square-root-function
The Python Square Root Function โ€“ Real Python
November 3, 2024 - The Python square root function, sqrt(), is part of the math module and is used to calculate the square root of a given number. To use it, you import the math module and call math.sqrt() with a non-negative number as an argument.
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
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ python โ€บ python square root function
Python Square Root Function
February 21, 2009 - The Python math.sqrt() method is used retrieve the square root of the given value. The square root of a number is the factor of multiplying the number by itself to get that number.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:numpy โ€บ math methods โ€บ .sqrt()
Python:NumPy | Math Methods | .sqrt() | Codecademy
April 9, 2025 - The .sqrt() function computes the positive square root of all elements in the input array.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Issue with math.sqrt not working - Python Help - Discussions on Python.org
February 17, 2023 - Hello everyone, New to programming in Python, in my first class. I am having an issue with programming within IDEL. In this example I am trying to perform math.sqrt and it is giving me a name error. This is pretty muโ€ฆ
๐ŸŒ
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 ...
๐ŸŒ
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).
๐ŸŒ
GitHub
gist.github.com โ€บ 1543590 โ€บ 7132c793b15e589c81a8972872861404d93f7c9d
Computes square root in Python SQRT
Computes square root in Python SQRT. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ math module โ€บ math.sqrt()
Python | Math Module | math.sqrt() | Codecademy
September 12, 2025 - The Python .sqrt() function is used to calculate the square root of a given number and is a part of the math library.
๐ŸŒ
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.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ sqrtx
Sqrt(x) - LeetCode
Can you solve this real interview question? Sqrt(x) - Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ python
How to calculate square root in Python (with examples)
March 18, 2024 - One of the simplest ways to calculate the square root of a number in Python is by using the exponentiation operator (**). This operator allows you to raise a number to a specific power.
๐ŸŒ
University of Vermont
uvm.edu โ€บ ~cbcafier โ€บ cs1210 โ€บ book โ€บ 05_functions โ€บ intro_to_math_module.html
Introduction to the math module: pi and sqrt() โ€“ Clayton Cafiero
June 12, 2025 - Letโ€™s unpack this. Within the math module, thereโ€™s a function named sqrt(). Writing math.sqrt() is accessing the sqrt() function within the math module. This uses what is called dot notation in Python (and many other languages use this as well).
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ blog โ€บ artificial intelligence โ€บ how to find square root in python: techniques explained
How to Find Square Root in Python: A Beginner's Guide
October 13, 2025 - Finding the square root in Python is simple and can be done using several techniques. You can use built-in functions like math.sqrt(), operators like **, or libraries such as NumPy and SymPy for advanced use cases.