Python Square Root: Real and Complex
LearnDataSci is reader-supported. When you purchase through links on our site, earned commissions help support our team of writers, researchers, and designers at no extra cost to you.
Calculating the Square Root in Python
A quick way of getting the square root of a value is using the exponentiation operator **
with 0.5 as the second parameter. See below for a quick example of this:
Using **
isn't the only way, or most accurate way, to calculate the square root in Python. 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.
Option 1: The Exponentiation Operator **0.5
Using the exponentiation operator **
is an easy way of getting the square root of a number. This operator raises the first operand to the power of the second operand.
To get square root, you need to use 0.5 as your second operand, as shown in the introduction.
The following snippet shows another example of how we can use **0.5
to calculate the square root for a range of values:
We can also use the exponentiation operator with negative values:
In this case, Python perceives the operation as -(4**0.5)
, which gives us -2. However, (-4)**0.5
gives us:
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. See below for some other examples of values you could calculate with **
:
Option 2: math.sqrt()
Example 1: Real Numbers
An alternative way of calculating the square root of a value is by using the math.sqrt()
function from the math
library. The example below demonstrates how we can apply the math.sqrt()
function to the example used in the **0.5
section:
As the results show, using math.sqrt()
gives the same results as **0.5
. The advantage of using **0.5
at the beginning of this article is that **
doesn't require an import like math.sqrt()
does.
On the other hand, many argue that math.sqrt()
usually executes faster; see this Stack Overflow page for a breakdown of the speeds.
Using a negative value with math.sqrt()
will throw the error ValueError: math domain error
, as shown below:
Depending on how you want to handle square roots of negatives, handling a ValueError
may be preferable. Alternatively, we can avoid this by using cmath.sqrt()
, as we'll see in the next section.
Example 2: Complex Numbers
You can also calculate the square root of negative and complex numbers using the cmath
library. See below for an example of this:
An excellent way to avoid getting ValueError: math domain error
is using cmath.sqrt()
to handle the exceptions.
For example, we can do this using the following script:
Option 3: numpy.sqrt()
If you're working with NumPy arrays, you also have the option of using numpy.sqrt()
(np.sqrt()
in the example).
Using this function with an array will create a new array containing all the square roots of the original array. The example below shows how we can create an array using the list from previous examples and then apply np.sqrt()
:
It's worth noting that you can also use np.sqrt()
on single values, but we don't recommend this as NumPy is optimized to work with arrays.
Summary
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. When working with arrays, you also have the option of using the numpy.sqrt()
function to calculate the square root of every value.