Skip to Content

Square Root In Python Without Importing Math Library

Square Root In Python Without Importing Math Library

How do you find the square root of a number in Python? Can you find the square root without needing to import the math library?

The most common approach in calculating the square root of a number in Python is to import the math library and use the method math.sqrt(n) where n is the number you want to square root.

But there is an even easier way of being able to calculate the square root of a number in Python without having to import the math library. How is this done?

The technique that doesn’t involve importing the math library uses the power operator **.

The power operator is a simple operator that raises a number to an exponent. An example of this is as follows:

>>> 5 ** 2
25
>>> 5 ** 3
125

In mathematics if you raise a number to the power of 2 you are squaring that number. If you wanted to square root a number you can simply raise a number to the power of one half.

Here is an example demonstrating this code:

>>> 25 ** (1/2)
5.0
>>> 64 ** (1/2)
8.0

As you can see from the above code by raising a number to the power of one half you get the square root of that number.

You could similarly apply the same technique to cube rooting in Python using the power operator raised to the exponent one-third as demonstrated in the following code below:

>>> 125 ** (1/3)
4.9999999999
>>> 27 ** (1/3)
3.0
>>> 64 ** (1/3)
3.9999999996

The only issue with using cube roots is some answers are not precise, which highlights an issue seen in other mathematical arithmetic operations when using Python. One workaround is to round the result to a defined number of decimals, as seen here:

>>> round(125 ** (1/3), 4)
5.0
>>> round(64 ** (1/3), 4)
4.0

Using Math Library

While there’s nothing wrong with importing the math library and using its methods if you did want to use this approach the math.sqrt(n) method takes a positive number as its sole parameter and calculates its square root.

Here are some examples demonstrating its use:

>>> import math
>>> math.sqrt(16)
4.0
>>> math.sqrt(81)
9.0

As you can see from the above examples the math square root method works in the same manner as the exponent operator.

Besides using the math.sqrt(n) method you could also use the other math method math.pow(n, p) which works in the same fashion as the power operator allowing for a number to be raised to an exponent.

Therefore, in the same way, the power operator helped in providing the square root by raising a number to the power of one half the same approach could be used by using this method:

>>> import math
>>> math.pow(16, 1/2)
4.0
>>> math.pow(81, 1/2)
9.0

As can be seen from the above results when compared to the previous results, using the math.sqrt() method, the results are the same.

Equally, the math.pow(n, p) approach can also be used to find the cube root of a number just like the approach above with the power operator.

Here’s how you would use the math.pow(n, p) function to obtain the cube root of a number:

>>> import math
>>> math.pow(125, 1/3)
4.999999999999999
>>> math.pow(27, 1/3)
3.0
>>> math.pow(64, 1/3)
3.9999999999999996

What Does ValueError: math domain error Mean?

If you are using the math library and perform an operation that doesn’t make mathematical sense, such as square rooting a negative number then you’re likely to get a ValueError, as demonstrated with the following example:

>>> import math
>>> math.sqrt(-25)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

>>> math.pow(-25, 1/2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

As you cannot square root a negative number you will get an error, but what happens when you square root a negative number using the exponent operator, interestingly you get a different response as demonstrated below:

>>> -25 ** (1/2)
-5.0

The reason for the different result is that Python has arranged the mathematical order of this calculation to be: 1. find the square root of 25; 2. then multiply by negative 1 which equals -5.

To force Python to associate the negative sign to the number being raised to the power of one half using the power operator wrap the number in parentheses, like so:

>>> (-25) ** (1/2)
(3.061616997868383e-16+5j)

As can be seen from the above code this gives an even weirder answer than before, what is (3.061616997868383e-16+5j)? We can some idea of what this means by writing the result to a string using f-strings:

>>> f'{(-25) ** (1/2):.20f}
'0.00000000000000030616+5.00000000000000000000j'

And what is revealed from this is that this result is made up of two answers: 0.00000000000000030616 and 5.00000000000000000000j which means the result is a complex number comprising of a real and imaginary component. Here’s further demonstration:

>>> n = (-25) ** (1/2)
>>> round(n.real, 4)
0.0
>>> round(n.imag, 4)
5.0
>>> round(n.real, 4) + round(n.imag, 4)
5.0

Therefore to achieve a simplified answer from this power operation on a negative number the result needs to be captured and then the real and imag components from the complex number need to be rounded and then added together as was shown above.

Summary

To square root a number in Python use either the power operator ** raised to the exponent of one half (1/2) or import the math library and use the math.sqrt(n) function.

Depending upon the type of answer sought and whether a negative number is permitted with your square root operation either the math.sqrt(n) method is to be preferred (if negative numbers are to throw an error), or if you want to handle complex number results you can use the power operator.