Python program to find the square root
Python example to find the square root of both integer and complex numbers in Python 3.

Square root of Positive Numbers
Code
num = 4
sqrt_num = num ** 0.5
print(f"Square root of {num} is {sqrt_num}")
Output
Square root of 4 is 2.0
Square root of Positive numbers - through User Input
Code
num = input("Enter a number: ")
sqrt_num = float(num) ** 0.5
print(f"Square root of {num} is {sqrt_num}")
Output
Enter a number: 9
Square root of 9 is 3.0
Now be careful with this method. We are using the input()
built-in function of Python. Since it returns a string, we convert the string into a number using the float()
function. Then, the square root is calculated.
Using the built-in int()
function will chop off the decimal places.
Square root of Complex Numbers
Code
import cmath
num = 3+5j
sqrt_num = cmath.sqrt(num)
print(f"Square root of {num} is {sqrt_num}")
Output
Square root of (3+5j) is (2.101303392521568+1.189737764140758j)
Square root of Complex Numbers - through user input
Code
import cmath
user_input = input("Enter a complex number: ")
num = eval(user_input)
sqrt_num = cmath.sqrt(num)
print(f"Square root of {num} is {sqrt_num}")
Here we are using the eval()
built-in function in Python instead of float()
to converting a user input string to a complex number.
Output
Enter a complex number: 2+3j
Square root of (2+3j) is (1.6741492280355401+0.8959774761298381j)
All the above codes use f-strings. Learn about python f-strings here.