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

🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
Return the Euclidean norm, sqrt(sum(x**2 for x in coordinates)).
🌐
W3Schools
w3schools.com › python › ref_math_sqrt.asp
Python math.sqrt() Method
The math.sqrt() method returns the square root of a number.
Discussions

math - How do I calculate square root in Python? - Stack Overflow
Since complex numbers represent a plane, sqrt results are a circle on the complex plane. Picking one result on this circle is not numerically stable, hence some algorithms produce inaccurate results. 2022-01-21T05:41:47.663Z+00:00 ... Save this answer. ... Show activity on this post. 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... More on stackoverflow.com
🌐 stackoverflow.com
Designing a Sqrt function in Python - Stack Overflow
This is a bit of an academic exercise. I'm implementing a sqrt function in Python. Here's my code, def mySqrt(x): low, high = 1, x while low More on stackoverflow.com
🌐 stackoverflow.com
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
Taking the positive square root: math.sqrt, math.pow and **
You could try writing some python to iterate over values of x comparing the execution times and returned results from each of the three methods. I would expect some variation in time for each method but no difference in calculated value. More on reddit.com
🌐 r/learnpython
2
2
February 14, 2021
🌐
Hyperskill
hyperskill.org › university › python › math-in-python
Math in Python
August 2, 2024 - To utilize this root function start by importing the math module. Simply add import math before using the function. After importing the math module you can calculate the root by employing math.sqrt(x). In this case 'x' denotes the number you wish to find the root of.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-math-function-sqrt
Python math.sqrt() function | Find Square Root in Python - GeeksforGeeks
February 14, 2025 - math.sqrt() returns the square root of a number. It is an inbuilt function in the Python programming language, provided by the math module.
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
Find elsewhere
🌐
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.
🌐
YouTube
youtube.com › watch
How to Find Square Root of a Number in Python
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
LearnDataSci
learndatasci.com › solutions › python-square-root
Python Square Root: Real and Complex – LearnDataSci
An alternative way of calculating the square root of a value is by using the math.sqrt() function from the math library.
🌐
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
January 28, 2026 - 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).
🌐
Code.mu
code.mu › en › python › manual › math › sqrt
The sqrt method of the math module - square root of a number in Python
The sqrt method of the math module returns the square root of the number specified in the parameter.
🌐
Facebook
facebook.com › groups › pythondeveloper.usa › posts › 2561635344338409
Python math functions cheat sheet: sqrt, ceil, floor, pi
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
🌐
Python Academy
python-academy.org › function reference › math.sqrt
math.sqrt — Python Function Reference
For the integer square root of an integer there is math.isqrt, and for the root of a negative number there is cmath.sqrt.
🌐
Reddit
reddit.com › r/learnpython › taking the positive square root: math.sqrt, math.pow and **
r/learnpython on Reddit: Taking the positive square root: math.sqrt, math.pow and **
February 14, 2021 -
from math import sqrt, pow

sqrt(x)
pow(x, 0.5)
x ** 0.5

These three ways give the positive square root of x (a real number). What are the differences between them and [dis]advantages of each? Are they actually the same?

🌐
YouTube
youtube.com › watch
Using Square Root in Python - YouTube
Are you trying to solve a quadratic equation? Maybe you need to calculate the length of one side of a right triangle. You can use the math module’s sqrt() me...
Published: July 15, 2021
🌐
YouTube
youtube.com › watch
Python sqrt() function | math module | mathematical functions - YouTube
In this video, learn about the sqrt() function of the Python math module with an example.Python Full Course (English): https://bit.ly/3Bse2DTPython Full Cour...
Published: August 1, 2022
🌐
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.