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.

Use with AI>>> 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.

Use with AI>>> 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:

Use with AI>>> 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:

Use with AI>>> (-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 (which are real numbers), 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):

Use with AI>>> 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:

Use with AI>>> 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:

Use with AI>>> 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

🌐
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 Python without importing the math module?Show/Hide · Yes, you can calculate the square root without importing the math module by using the exponent operator ** or the built-in pow() function.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions — Python 3.15.2 documentation
Changed in version 3.11: The special cases pow(0.0, -inf) and pow(-0.0, -inf) were changed to return inf instead of raising ValueError, for consistency with IEEE 754. math.sqrt(x)¶ · Return the square root of x.
Discussions

sqrt(x) VS x**0.5

As has already been shown by other posters, sqrt is noticeably faster than **, and many would also argue that it is more readable. That kind of leads to the question of why one would want to avoid sqrt. Why is this a goal for you? Is it because you know sqrt is slow? Is it because the math module said something mean?

By the way, if you're writing e.g. a game and want stuff to to be reasonably fast, it's quite often possible to avoid the use of sqrt by simply doing calculations on distance squared. Say you're going to trigger an explosion that deals damage to everybody within a five meter radius. The obvious way to check if somebody is within radius would be:

sqrt(dx*dx + dy*dy) < 5

but the fast way would be

(dx*dx + dy*dy) < 5*5

Hope this helps.

More on reddit.com
🌐 r/Python
28
34
August 30, 2011
How to test if a number is a perfect square in python
Instead of checking every number less than h by taking the square root you could do it the other way around: starting at num = 1, increase num until num ** 2 >= h. The number of perfect squares is then num-1, so something like num = 1 while num**2 < h: num += 1 q = num - 1 This will reduce the number of iterations from h to about sqrt(h). More on reddit.com
🌐 r/learnpython
10
4
March 19, 2019
I created an algorithm to calculate square root of large numbers in python. What is this worth?

Is this impressive?

Not really, no.

Check out GMP and its Python binding, GMPY2.

Its isqrt() function takes about 170 μs on my computer with your benchmark input, which is more than 500 times faster than what you implemented, assuming our computers are at least somewhat comparable. That's what a good algorithm and hand-coded assembly can do for basic multi-precision calculations.

from gmpy2 import mpz, isqrt, log10
from time import time

v = mpz(127**12600+7**5000+3459984321)

t0 = time()
x = isqrt(v)
t1 = time()

print(x ** 2 <= v, (x+1) ** 2 <= v, t1 - t0, log10(v))
More on reddit.com
🌐 r/math
31
5
February 1, 2018
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
People also ask

What is the fastest way to find the square root in Python?
For a single number, math.sqrt() is highly optimized and very fast. For calculations on a large array or list of numbers, numpy.sqrt() is significantly faster because it performs the operation in compiled C code, avoiding the overhead of a Python loop.
🌐
upgrad.com
upgrad.com › home › blog › artificial intelligence › how to find square root in python: techniques explained
How to Find Square Root in Python: Techniques Explained
How precise is the function Square Root in Python to result in floating points?
Python's usage of floating-point representation affects how accurate the output is. For the majority of practical reasons, Python normally employs the IEEE 754 double-precision format, which offers a high level of accuracy.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › square root in python
Exploring Square Root Calculation in Python: A Comprehensive Guide
What is a "domain error" in math.sqrt()?
A "math domain error" occurs when you provide a function with an input value that is outside its domain of definition. For math.sqrt(x), the domain is all non-negative real numbers (x &gt;= 0). Providing a negative number violates this condition, hence the error.
🌐
upgrad.com
upgrad.com › home › blog › artificial intelligence › how to find square root in python: techniques explained
How to Find Square Root in Python: Techniques Explained
Top answer
1 of 11
108

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.

Use with AI>>> 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.

Use with AI>>> 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:

Use with AI>>> 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:

Use with AI>>> (-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 (which are real numbers), 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):

Use with AI>>> 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:

Use with AI>>> 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:

Use with AI>>> 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.

Use with AIimport sympy
sympy.sqrt(2)
# => sqrt(2)

This doesn't seem very useful at first.

But sympy can give more information than floats or Decimals:

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

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

Use with AIs = 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:

Use with AI(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:

Use with AIfrom 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:

Use with AIsympy.N(sympy.sqrt(2), 1_000_000)
# => 1.4142135623730950488016...........2044193016904841204
🌐
LearnDataSci
learndatasci.com › solutions › python-square-root
Python Square Root: Real and Complex – LearnDataSci
It's simple to calculate the square root of a value in Python using the exponentiation operator ** or math.sqrt(). It's worth mentioning that math.sqrt() is usually the faster of the two and that by using cmath.sqrt() you can get the square root of a complex number.
🌐
Python Guides
pythonguides.com › square-root-in-python
Ways to Calculate Square Root in Python
January 27, 2026 - In Python, you can use the double asterisk ** for exponentiation. Since taking a square root is the same as raising a number to the power of 0.5, this works perfectly. Let’s say you are calculating the distance for a delivery drone traveling ...
🌐
TradingCode
tradingcode.net › python › math › square-root
How to calculate the square root in Python? - Math
The math.isqrt() function returns ... to the power of 0.5 we also get the square root. We can use Python’s exponent (**) operator or pow() function for this....
Find elsewhere
🌐
PYnative
pynative.com › home › python › programs and examples › python find square root of a number
Python Find Square Root of Number – PYnative
March 27, 2025 - import math print('square root of 0: ', math.sqrt(0)) print('square root of integer number 64: ', math.sqrt(64)) print('square root of float number 5.6: ', math.sqrt(5.6))Code language: Python (python) Run
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › square root in python
Exploring Square Root Calculation in Python: A Comprehensive Guide
August 25, 2026 - This approach works because raising a number to the power of 0.5 is equivalent to finding its square root. ... In this code, the ** operator is used with the base number and the exponent 0.5 to calculate the square root.
🌐
AskPython
askpython.com › python › examples › calculate-square-root
4 Methods to Calculate Square Root in Python - AskPython
July 6, 2021 - Square root of number 121: 11.0 Square root of number 0.49: 0.7 Square root of number 4.41: 2.1 Square root of number 265: 16.278820596099706 · NOTE: If a negative number is passed as an argument to the built-in sqrt() function then it will ...
🌐
Upgrad
upgrad.com › home › blog › artificial intelligence › how to find square root in python: techniques explained
How to Find Square Root in Python: Techniques Explained
October 13, 2025 - The ** operator is Python's standard operator for raising a number to a power. It's concise, readable, and a favorite among many developers for its simplicity. ... This works just like math.sqrt() but without the need for an import statement.
🌐
W3Schools
w3schools.com › python › ref_math_sqrt.asp
Python math.sqrt() Method
The math.sqrt() method returns the square root of a number.
🌐
Cloudinary
cloudinary.com › home › how do you calculate square roots in python?
How Do You Calculate Square Roots in Python?
August 19, 2025 - The cmath module is Python’s complex math library. It returns the square root as a complex number (e.g., 2j means √-4).
🌐
Python-bloggers
python-bloggers.com › 2022 › 12 › python-square-roots-5-ways-to-take-square-roots-in-python
Python Square Roots: 5 Ways to Take Square Roots in Python | Python-bloggers
December 1, 2022 - There's no way to calculate a square root of a negative number by using real numbers. Two negative numbers multiplied will always result in a positive number. ... import math a = -10 # Square root of a negative number a_sqrt = math.sqrt(a) # Print print("Square root of a negative number") print("--------------------------------------------------") print(f"Square root of {a} = {a_sqrt}")
🌐
Enki
enki.com › post › how-to-square-numbers-in-python-sqrt
Enki | Blog - How to Square Numbers in Python - sqrt
While we are focused on squaring, understanding square roots is equally important, especially when you need to reverse a squaring operation. In Python, you can easily calculate square roots using the math.sqrt() function:
🌐
Real Python
realpython.com › videos › intro-square-roots-math-python
An Introduction to Square Roots in Math and in Python (Video) – Real Python
03:18 As I’m just using the vanilla sqrt() here I get a ValueError instead. 03:28 In addition to the sqrt() function, Python 3.8 introduced the isqrt() function in the math module.
Published: July 13, 2021
🌐
Stack Abuse
stackabuse.com › calculate-square-root-of-a-number-in-python
Calculate Square Root of a Number in Python
May 16, 2023 - So really, finding the square root of a number can be expressed as raising the number to a power of ½. math.pow() takes two arguments - the base and the exponent, and rises the base to the power of an exponent: ... The ** operator is a binary operator which means it works with two values, ...
🌐
Hyperskill
hyperskill.org › university › python › math-in-python
Math in Python
August 2, 2024 - To use the exponentiation operator for square roots, you can write: ... This method allows quick and efficient calculation of square roots without additional steps or dependencies on external libraries. When evaluating efficiency it's helpful to compare how well the sqrt function performs.
🌐
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).
🌐
Altcademy
altcademy.com › blog › how-to-do-square-root-in-python
How to do square root in Python - Altcademy.com
June 13, 2023 - In this tutorial, we have learned how to find the square root of a number in Python using three different approaches: the built-in math.sqrt() function, the exponentiation operator, and the Babylonian method.
🌐
Python Central
pythoncentral.io › python-square-root
How to Calculate Square Root with Python | Python Central
March 4, 2025 - While the math.sqrt function requires you to import the math module, this operator does not import any modules. With the pow() function in Python, square root calculation is a walk in the park.