To calculate the square root in Python, you can use several methods:

  • math.sqrt(x): The most common and recommended method for non-negative numbers. Import the math module first, then call math.sqrt(x). It returns a float and raises a ValueError for negative inputs.
    Example: import math; math.sqrt(16)4.0

  • Exponentiation (x ** 0.5): A quick alternative that uses Python’s power operator. Works for non-negative numbers and returns a float.
    Example: 16 ** 0.54.0

  • cmath.sqrt(x): Use this for negative or complex numbers. It returns a complex number.
    Example: import cmath; cmath.sqrt(-9)3j

  • math.isqrt(x): Returns the integer square root (floor of the square root) for non-negative integers.
    Example: math.isqrt(12)3

  • Newton’s Method: Implement manually for educational or custom precision purposes.
    Example: Use iterative approximation with x = 0.5 * (x + n/x).

For vectorized operations on arrays, use numpy.sqrt() from the NumPy library.

Note: math.sqrt() only works with non-negative numbers. Use abs() or cmath for negative inputs.

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

🌐
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.
🌐
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 »
Discussions

How to find square root accurately
Hi I am trying to find the square root of 34,005,370,812,130,263,200. However Python is giving the wrong answer. The correct answer is 5,831,412,420.0000005830491406059734, but Python gives 5,831,412,420 and no decimal points. How can I resole this ? More on discuss.python.org
🌐 discuss.python.org
0
August 23, 2025
How to get the square root of a number to 100 decimal places without using any libraries or modules (Python)
Python has arbitrary length integers. You can implement fixed point numbers with 100 decimal places on top of integers instead of using floating point. Then you will have to implement your own version of square root. You can do it with Newton's method, dunno if you've seen that in your class yet. The fixed point function for sqrt(a) is f(x) = a / x. Edit: I think you'll need more than 100 digit precision to end up with 100 digit precision after dividing. I think you need 200 digits for the dividend and 100 for the divisor to get 100 for the answer. More on reddit.com
🌐 r/AskProgramming
10
6
September 1, 2020
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
January 23, 2017
Fast algorithm to calculate integer square root
The low-arithmetic method is simply to work from highest-order bit to lowest, turning on bits that don't make the square exceed n. The square can be updated with only bit shifts and additions, since you're only adding 1< More on reddit.com
🌐 r/algorithms
13
18
February 16, 2014
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
Since the square root of a negative number gives a complex answer, We recommend using cmath.sqrt(), as shown at the end of the next section. Note that the second operand of ** can be any real number. Thus, you only use 0.5 when looking for the square root.
🌐
Medium
medium.com › data-science › python-square-roots-5-ways-to-take-square-roots-in-python-445ea9b3fb6f
Python Square Roots: 5 Ways to Take Square Roots in Python | by Dario Radečić | TDS Archive | Medium
December 2, 2022 - This article will teach you five distinct ways to take square roots in Python and will finish off with a bonus section on cube roots and square roots of Python lists.
🌐
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).
Find elsewhere
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
1 week ago - Return the integer square root of the nonnegative integer n.
🌐
Replit
replit.com › home › discover › how to do square root in python
How to do square root in Python
4 weeks ago - The math.sqrt() function is a precise tool for finding a square root. It lives inside Python's standard math module, so you must import the module before using it.
🌐
Oreate AI
oreateai.com › blog › unlocking-the-power-of-pythons-sqrt-function › 90248c12535df26c277ed3f8197b467c
Unlocking the Power of Python's SQRT Function - Oreate AI Blog
January 8, 2026 - This function computes the integer square root of a non-negative integer and guarantees an exact result without any floating-point errors. Imagine you're working on a competitive programming problem where every millisecond counts and accuracy ...
🌐
Scaler
scaler.com › home › topics › calculate square root in python
How to Find Square Roots in Python? - Scaler Topics
February 26, 2024 - Let's learn how to find square root in python using the Exponentiation operator. This method is simple and does not require importing any external libraries.
🌐
PythonGeeks
pythongeeks.net › головна › python square root and math functions explained
Python Square Root and Math Functions Explained
October 27, 2025 - To use this function, one must import the math library and call math.sqrt(number), with number being the value whose square root is desired. Understanding how to do sqrt in Python is crucial for developers involved in scientific or financial ...
🌐
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 - Learn how to find the square root in Python using different techniques. Our guide covers math.sqrt(), the power operator, NumPy, and custom programs.
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-sqrt-in-python
numpy.sqrt() in Python - GeeksforGeeks
July 12, 2025 - numpy.sqrt() in Python is a function from the NumPy library used to compute the square root of each element in an array or a single number. It returns a new array of the same shape with the square roots of the input values.
🌐
Tutorialspoint
tutorialspoint.com › home › python › python square root function
Python Square Root Function
February 21, 2009 - Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object. Following is the syntax of Python math.sqrt() method
🌐
Python.org
discuss.python.org › python help
How to find square root accurately - Python Help - Discussions on Python.org
August 23, 2025 - Hi I am trying to find the square root of 34,005,370,812,130,263,200. However Python is giving the wrong answer. The correct answer is 5,831,412,420.0000005830491406059734, but Python gives 5,831,412,420 and no decimal p…
🌐
TradingCode
tradingcode.net › python › math › square-root
How to calculate the square root in Python? • TradingCode
Python can calculate the square root of a number in three ways: math.sqrt(), pow(), or **. This article explains each with easy-to-follow code.
🌐
LeetCode
leetcode.com › problems › sqrtx
Sqrt(x) - LeetCode
* For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Example 1: Input: x = 4 Output: 2 Explanation: The square root of 4 is 2, so we return 2. Example 2: Input: x = 8 Output: 2 Explanation: The square root of 8 is 2.82842..., ...
🌐
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).