How to Find Square Root of a Real or Complex Number in Python?

Yo fam! We gonna learn the way to find the square root of a real or a complex number in Python. We shall use import, data types, and operators in Python. Precisely speaking, we use sqrt() function in the cmath.

Codevarsity
Coding Tutorials
2 min readJan 30, 2019

--

The square root of a number is a value that, when multiplied by itself, gives the number. In 3 x 3 = 9 , 3 is a square root of 9 . Also, note that (-3) x (-3) also equals to 9 . Hence -3 is also a square root of 9 . But, ‘’ always denotes a positive square root such as 16 is always equal to +4 .

Python Code

Output

Explanation

For finding the square root of a positive real number, we can use the ** otherwise called, exponent operator as mentioned here, but for negative or complex numbers, we have to use the sqrt() function in the cmath or the complex math module. Also, notice that we to use eval() function instead of float() to convert complex numbers as well.

You can also check the similar article on ‘codevarsity.org’

--

--