# 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:

4**0.5

Learn Data Science with


Out:
2.0

Learn Data Science with



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:

values = [16, 25, 36, 49, 64]
for x in values:
x_sqrt = x**0.5
print(f'value: {x} square root {x_sqrt}')

Learn Data Science with


Out:
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0

Learn Data Science with



We can also use the exponentiation operator with negative values:

-4**0.5

Learn Data Science with


Out:
-2.0

Learn Data Science with



In this case, Python perceives the operation as -(4**0.5), which gives us -2. However, (-4)**0.5 gives us:

(-4)**0.5

Learn Data Science with


Out:
(1.2246467991473532e-16+2j)

Learn Data Science with



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 **:

x = 4

print(f' x squared: {x**2}')
print(f' x cubed: {x**3}')
print(f' x to the power of -1 (x/x squared): {x**-1}')
print(f' x to the power of -2 (x/x cubed): {x**-2}')

Learn Data Science with


Out:
x squared: 16
x cubed: 64
x to the power of -1 (x/x squared): 0.25
x to the power of -2 (x/x cubed): 0.0625

Learn Data Science 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:

import math

values = [16, 25, 36, 49, 64]

for x in values:
x_sqrt = math.sqrt(x)
print(f'value: {x} square root {x_sqrt}')

Learn Data Science with


Out:
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0

Learn Data Science with



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:

math.sqrt(-4)

Learn Data Science with


Out:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-5c74ec90b955> in <module>
----> 1 math.sqrt(-4)

ValueError: math domain error

Learn Data Science with



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:

import cmath

c = (12 + 16j)

cmath.sqrt(c)

Learn Data Science with


Out:
(4+2j)

Learn Data Science with



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:

import math
import cmath

values = [16, -25, 36, -49, 64]

for x in values:
try:
x_sqrt = math.sqrt(x)
except ValueError:
x_sqrt = cmath.sqrt(x)

print(f'value: {x} square root {x_sqrt}')

Learn Data Science with


Out:
value: 16 square root 4.0
value: -25 square root 5j
value: 36 square root 6.0
value: -49 square root 7j
value: 64 square root 8.0

Learn Data Science with



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

import numpy as np

values_array = np.array([16, 25, 36, 49, 64])

sqrt_array = np.sqrt(values_array)

print(f'values: {values_array} square root of values: {sqrt_array}')

Learn Data Science with


Out:
values: [16 25 36 49 64] square root of values: [4. 5. 6. 7. 8.]

Learn Data Science with



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.

### Meet the Authors

Alfie graduated with a Master's degree in Mechanical Engineering from University College London. He's currently working as Data Scientist at Square Enix. Find him on LinkedIn.