nth root of x is x^(1/n), so you can do 9**(1/2) to find the 2nd root of 9, for example. In general, you can compute the nth root of x as:

x**(1/n)

Note: In Python 2, you had to do 1/float(n) or 1.0/n so that the result would be a float rather than an int. For more details, see Why does Python give the "wrong" answer for square root?

Answer from Hari Menon on Stack Overflow
🌐
ROOT
root.cern › manual › python
Python Interface - ROOT
It is HEP’s entrance to all C++ from Python, for example, for frameworks and their steering code. With you can access the full ROOT C++ functionality from Python while benefiting from the performance of the ROOT C++ libraries.
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.optimize.root.html
root — SciPy v1.18.0 Manual
>>> from scipy import optimize >>> sol = optimize.root(fun, [0, 0], jac=jac, method='hybr') >>> sol.x array([ 0.8411639, 0.1588361])
Discussions

math - Is there a short-hand for nth root of x in Python? - Stack Overflow
In maths, if I wish to calculate 3 to the power of 2 then no symbol is required, but I write the 2 small: 3². In Python this operation seems to be represented by the ** syntax. >>> 3**2 9 ... More on stackoverflow.com
🌐 stackoverflow.com
What does root mean? And do
to use tkinter you first have to initialize the embedded tcl/tk interpreter. every tkinter object represents an object in this embedded interpreter. the interpreter isn't initialized until you create the "root window". it's named "root" because all the tk widgets etc exist in a hierarchical structure and everything branches off of the "root". the name "root" is just a variable it has no special meaning, you could name it anything. I have seen names like root, window, and main. More on reddit.com
🌐 r/learnpython
13
6
May 19, 2024
math - How do I calculate square root in Python? - Stack Overflow
I need to calculate the square root of some numbers, for example √9 = 3 and √2 = 1.4142. How can I do it in Python? The inputs will probably be all positive integers, and relatively small (say less... More on stackoverflow.com
🌐 stackoverflow.com
How to have a project’s root always in the Python path?
If the project is a Python package, and the package is installed in "dev mode" (aka "editable mode"), then it's basically symlinked into your site-packages. So it's an environment-wide importable module (the same as if you'd done pip install some_project) but it also remains a project with editable source with your changes reflected live in the installed package. The root of the package is on the Python path because it's simlinked into site-packages. Many of the popular Python project Makefiles set up development and testing by installing from source in editable mode using pip or setuptools. All of mine do this, for instance, because it's just so much easier than fucking around with $PYTHONPATH. More on reddit.com
🌐 r/learnpython
8
4
April 26, 2024
🌐
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
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.

>>> 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 (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):

>>> 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
Find elsewhere
🌐
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.
🌐
Cms-opendata-workshop
cms-opendata-workshop.github.io › workshop-lesson-root › 03-root-and-pyroot › index.html
ROOT in C++ and Python – Efficient analysis with ROOT
July 6, 2021 - ROOT provides the Python bindings called PyROOT. PyROOT is not just ROOT from Python, but a full-featured interface to call C++ libraries in a pythonic way. This lets you import the ROOT module from Python and makes all features dynamically available.
🌐
Nitratine
nitratine.net › blog › post › understanding-the-python-root-folder
Understanding The Python Root Folder - Nitratine
June 11, 2019 - Where you located python.exe is also the root of your Python installation (assuming you aren't using a virtual environment). In this folder, you will find files relating directly to Python and modules you have installed.
🌐
Stack Abuse
stackabuse.com › bytes › get-the-root-project-directory-path-in-python
Get the Root Project Directory Path in Python
August 19, 2023 - To retrieve the root project directory path, Python provides the os module, which includes functions for interacting with the operating system. Among these functions is os.getcwd(), which returns the current working directory path.
🌐
Medium
medium.com › @ogiriagbehi › understanding-and-implementing-root-finding-algorithms-in-python-3d163aa0645b
Understanding and Implementing Root-Finding Algorithms in Python | by Ogiri Agbehi | Medium
August 26, 2024 - Understanding and Implementing Root-Finding Algorithms in Python Introduction to Root-Finding In numerical computing, a root of a function is a value that makes the function equal to zero …
🌐
GeeksforGeeks
geeksforgeeks.org › 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.
🌐
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.
🌐
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.
🌐
GitHub
github.com › scikit-hep › uproot
GitHub - scikit-hep/uproot: ROOT I/O in pure Python and NumPy. · GitHub
Uproot is a library for reading and writing ROOT files in pure Python and NumPy.
🌐
GitHub
github.com › SimpleArt › pyroot
GitHub - SimpleArt/pyroot: Python library for root-finding in one dimension · GitHub
The purpose of this Python library is to provide implementations of advanced bracketed root-finding methods for single-variable functions.
Author: SimpleArt