Can the square root be calculated using a method other than the exponentiation operator?

Question

In this exercise, the square root of a number is calculated using the exponentiation operator. Is there another method available to do that?

Answer

The Python math library includes many useful functions including a sqrt() function. The following code sample shows an example of its usage.

from math import sqrt
print(sqrt(16))
10 Likes

Yes! To get the square root of 4, you can use this: 4 ** (1/2)

Erm, excuse me. Did you read the question in the title of this topic?

You can also use math.pow, which is the same as the ** operator. However, a more helpful new addition to the topic is cmath, which will work with complex numbers. So you can use cmath.sqrt in these cases.