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

How to calculate square root safely in Python? - TestMu AI Community
I’m working with positive integers in Python and need to calculate square roots—like getting 3 for √9 or 1.4142 for √2. What’s the most reliable way to compute a square root in Python, especially when handling large numbers? Are there any caveats I should be aware of? More on community.testmuai.com
🌐 community.testmuai.com
0
June 23, 2025
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
Hey can anyone help me add square root calculation to my code?
Format your code More on reddit.com
🌐 r/learnpython
12
0
May 22, 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
August 26, 2020
🌐
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 »
🌐
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
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to calculate square root safely in Python? - TestMu AI Community
June 23, 2025 - How do you calculate the square root of a number using Python safely and accurately? I’m working with positive integers in Python and need to calculate square roots—like getting 3 for √9 or 1.4142 for √2. What’s the mos…
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › how to find square root of a number using python
How to Find Square Root of a Number Using Python - Shiksha Online
August 23, 2023 - In this method, we use the sqrt() function of the cmath module to calculate the square root of a real or complex number in Python.
🌐
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.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
2 weeks ago - Return the integer square root of the nonnegative integer n.
🌐
Onestopdataanalysis
onestopdataanalysis.com › home › the easiest way to use the python square root function
The Easiest Way to Use the Python Square Root Function
May 10, 2020 - The square root function is defined as a function that takes any positive number as its input and returns the positive number that might need to be squared i.e., in other words, the output of square root function, when multiplied by itself will ...
🌐
Sololearn
sololearn.com › en › Discuss › 1603250 › add-a-square-root-function-in-python-3
Add a square root function in Python 3 | Sololearn: Learn to code for FREE!
November 28, 2018 - For square root, you need only 1 operand (1 input number) Calculation in Python is simple: result = num1 ** 0.5 eg.
🌐
CodeFatherTech
codefather.tech › home › blog › how to calculate the square root in python: multiple approaches
How to Calculate the Square Root in Python: Multiple Approaches
June 27, 2025 - The math module gives access to a wide range of mathematical functions. You don’t need to install this module separately because it is already part of the Python installation. Use the sqrt() function of the math module which returns the square root of a number.
🌐
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…
🌐
DEV Community
dev.to › itsmycode › how-to-find-square-root-in-python-4ga2
How to find Square Root in Python? - DEV Community
September 28, 2021 - Square root, in mathematics, is a factor of a number that, when multiplied by itself, gives the original number. For example, both 3 and –3 are square roots of 9. The math module in Python has sqrt() and pow() functions, using which you can ...
🌐
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 ...
🌐
Reddit
reddit.com › r/learnpython › hey can anyone help me add square root calculation to my code?
r/learnpython on Reddit: Hey can anyone help me add square root calculation to my code?
May 22, 2025 -

Import math

x = input("use +, -, , / ") num1 = int(input("number 1 ")) num2 = int(input("number 2 ")) if x == "+": print(f"your answer is {num1 + num2}") elif x == "-": print(f"your answer is {num1 - num2}") elif x == "": print(f"your answer is {num1 * num2}") elif x == "/": print(f"your answer is {num1 / num2}") else: print(f"{x} is an invalid operator")

🌐
Programiz
programiz.com › python-programming › examples
Python Examples | Programiz
Python Program to Find the Square Root · Python Program to Calculate the Area of a Triangle · Python Program to Solve Quadratic Equation · Python Program to Swap Two Variables · Python Program to Generate a Random Number · Python Program to Convert Kilometers to Miles ·
🌐
Codedamn
codedamn.com › news › python
How to calculate square root in Python (with examples)
March 18, 2024 - The syntax of the function is straightforward: math.sqrt(x), where x is the number you want to find the square root of. It’s important to note that x must be a non-negative number; otherwise, a ValueError will be raised. Here are a few examples demonstrating how to use the math.sqrt() function...