Python Program to Find Square root | Here, we will discuss all possible methods to write a python program to calculate the square root of the number. We will give a number and the python program will calculate the square root of these numbers using various methods.

**Formula of ****square root**

x^{2 }= y or x = ±√y

**Mathematically,**

x2 = 16

x = √16

x = ±4

Now let’s see how to write python program for calculate square root of numbers.

## Square root in Python

This is the simplest and easiest way to print the square root program in python. We will take a number while declaring the variables and its square root value will be stored to sqrt variable, and finally, it will be displayed to the screen.

Program description:- **Write a Python program to find the square root**

```
# Python program to find square root of the number
# take inputs
num = 25
# calculate square root
sqrt = num ** 0.5
# display result
print('Square root of %0.2f is %0.2f '%(num, sqrt))
```

Output:-

Square root of 25.00 is 5.00

In this program, we have hardcoded the values of the number in the source code, 25 numeric values are assigned to them.

`num = 25`

Calculate square root of the number using the mathematics calculation.

`sqrt = num ** 0.5`

Finally, display the calculated value using the print() function.

`print('Square root of %0.2f is %0.2f '%(num, sqrt))`

**Taking input from the User**

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

```
# Python program to find square root of the number
# take inputs
num = float(input('Enter the number: '))
# calculate square root
sqrt = num ** 0.5
# display result
print('Square root of %0.2f is %0.2f '%(num, sqrt))
```

Output for the different input values:-

Enter the number: 4

Square root of 4.00 is 2.00

Enter the number: 13

Square root of 13.00 is 3.61

Enter the number: 8.5

Square root of 8.50 is 2.92

Enter the number: 23.6254

Square root of 23.63 is 4.86

In this program, inputs are scanned using the input() function and stored in variable num.

`num = float(input('Enter the number: '))`

Calculate the square root of the number using the mathematics calculation. Finally, display the calculated value using the print() function.

## Sqrt in Python using Math Function

In this program, calculate the square root of the number using the sqrt() method. Sqrt() method will be imported from the math file.

```
# Python program to find square root of the number
import math # math module
# take inputs
num = float(input('Enter the number: '))
# display result
print('Square root = ',math.sqrt(num))
```

Output for the different input values:-

Enter the number: 16

Square root = 4.0

Enter the number: 11

Square root = 3.3166247903554

This program work for all positive real number (x>=0).

When x<0 it does not executes due to runtime error

Example:-

```
import math
print(math.sqrt(-1))
```

Output:-

Traceback (most recent call last):

File “main.py”, line 3, in

print(math.sqrt(-1))

ValueError: math domain error

## Python Program to Find Square root of Complex Numbers

In this program, We will find the square root of complex numbers using the sqrt() function in the cmath (complex math) module.

```
# Python program to find square root of complex nuumber
import cmath # math module
# take inputs
num = 1+2j
# calculate square root
sqrt = cmath.sqrt(num)
# display result
print('The square root of {0} is {1:0.2f}+{2:0.2f}'.format(num,
sqrt.real,sqrt.imag))
```

Output:-

The square root of (1+2j) is 1.27+0.79

**inputs will be provided by the user**

```
# Python program to find square root of complex nuumber
import cmath # math module
# take inputs
num = eval(input('Enter the number: '))
# calculate square root
sqrt = cmath.sqrt(num)
# display result
print('The square root of {0} is {1:0.2f}+{2:0.2f}'.format(num,
sqrt.real,sqrt.imag))
```

Output for the different input values:-

Enter the number: 5+9j

The square root of (5+9j) is 2.77+1.63

Enter the number: 5+9

The square root of 14 is 3.74+0.00

Enter the number: 5

The square root of 5 is 2.24+0.00

Enter the number: 9j

The square root of 9j is 2.12+2.12

we have used the eval() function instead of float() to convert complex numbers as well.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Also See:- Python Program to Find Area of Circle

Lourdes CraneThanks for sharing your info. I truly appreciate your efforts and I am waiting for your next post thank you once again.