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

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
🌐
Real Python
realpython.com › python-square-root-function
The Python Square Root Function – Real Python
November 3, 2024 - In this quick and practical tutorial, you'll learn what a square root is and how to calculate one in Python. You'll even see how you can use the Python square root function to solve a real-world problem.
People also ask

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
Can complicated numbers be handled by Square Root in Python?
No, only positive real values and 0 are compatible with the math module's sqrt() function. The Python cmath package can be used to find the square roots of complex numbers.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › square root in python
Exploring Square Root Calculation in Python: A Comprehensive Guide
Is it possible to conduct many square root calculations efficiently?
Yes, you may use the sqrt() function to multiply the values of a list or an iterable by using list comprehensions or the map() method.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › square root in python
Exploring Square Root Calculation in Python: A Comprehensive Guide
🌐
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 »
🌐
Programiz
programiz.com › python-programming › examples › square-root
Python Program to Find the Square Root
# Python Program to calculate the square root # Note: change this value for a different result num = 8 # To take the input from the user #num = float(input('Enter a number: ')) num_sqrt = num ** 0.5 print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
🌐
LearnDataSci
learndatasci.com › solutions › python-square-root
Python Square Root: Real and Complex – LearnDataSci
We'll look at how you can calculate the square root of a value using exponentiation, along with the math and numpy sqrt() functions, and also consider the advantages of each option. Using the exponentiation operator ** is an easy way of getting the square root of a number.
🌐
Flexiple
flexiple.com › python › square-root-in-python
How to calculate Square Root in Python? - Flexiple
We have begun by defining a function ... n. The function is then called to implement the action and print the result. The sqrt() function is a predefined method used to find the square root in Python....
🌐
Codedamn
codedamn.com › news › python
How to calculate square root in Python (with examples)
March 18, 2024 - For those looking to delve deeper ... the simplest ways to calculate the square root of a number in Python is by using the exponentiation operator (**)....
Find elsewhere
🌐
AskPython
askpython.com › home › 4 methods to calculate square root in python
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 ...
🌐
datagy
datagy.io › home › python posts › python square root: how to calculate a square root in python
Python Square Root: How to Calculate a Square Root in Python • datagy
December 20, 2022 - In this tutorial, you’ll learn how to calculate use Python to calculate the square root of a number, using the .sqrt() function. You’ll learn how to do this with, and without, the popular math library that comes built into Python.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › square root in python
Exploring Square Root Calculation in Python: A Comprehensive Guide
October 25, 2025 - For example, the square root of 2 (√2) is roughly 1.41421356 and continues indefinitely without repetition. These square roots are referred to as irrational numbers. Python's ‘math’ module has the sqrt() function for quickly calculating ...
🌐
Python Guides
pythonguides.com › square-root-in-python
Square Root In Python
November 21, 2024 - Learn how to calculate the square root in Python using the math.sqrt() function. This guide covers syntax, examples, and practical scenarios for efficient square root calculations.
🌐
Scaler
scaler.com › home › topics › calculate square root in python
Calculate Square Root in Python
February 26, 2024 - Explanation To find the square root of a number in python using the exponentiation operator, raise the number to the power of 0.5. ... The math.sqrt() function from the standard math module is used for simple and efficient square root calculation.
🌐
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:
🌐
Replit
replit.com › home › discover › how to do square root in python
How to do square root in Python
Its np.sqrt() function performs a vectorized operation, meaning it calculates the square root of every number in an array all at once. This is far more efficient than looping through each item individually.
🌐
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 Central
pythoncentral.io › python-square-root
How to Calculate Square Root with Python | Python Central
March 4, 2025 - Let's get started! The easiest and most straightforward way to calculate the square root with Python is using the math.sqrt() function. The math module is built in such a way that it gives you accurate results for floating-point numbers.
🌐
GeeksforGeeks
geeksforgeeks.org › python-math-function-sqrt
Python math.sqrt() function | Find Square Root in Python - GeeksforGeeks
February 14, 2025 - It calculates the hypotenuse c using the values of a and b. math.sqrt() does not work for negative numbers. It raises a ValueError if we pass a number less than 0. Python · import math # error when x<0 print(math.sqrt(-1)) Output · Traceback (most recent call last): File "/home/67438f8df14f0e41df1b55c6c21499ef.py", line 8, in print(math.sqrt(-1)) ValueError: math domain error · Explanation · Square roots of negative numbers are not real numbers.
🌐
Analytics Vidhya
analyticsvidhya.com › home › python square root
Python Square Root
October 10, 2024 - Learn how to calculate Python Square Root using various methods, including handling negative numbers and real-world examples.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-find-square-root-of-given-number
Find Square Root Of Given Number - Python - GeeksforGeeks
July 23, 2025 - We can also find the floor of the square root using Python’s built-in exponentiation operator (**) and then integer conversion. ... def countSquares(x): sqrt = x**0.5 result = int(sqrt) return result # driver code x = 9 print(countSquares(x)) ... Explanation: x**0.5 calculates the square root of x, and int(sqrt) converts it to the largest integer less than or equal to the sqrt.