Python Examples

Python Program - Square Root of a Number



If a number is multiplied by itself (n*n), the final number will be the square of that number and finding the square root of a number is inverse operation of squaring the number. If x is the square root of y, it can be expressed as below:

square root

Alternatively, it can also be expressed as:

x2 = y

Method 1: Using exponential (**) operator

The exponential (**) operator of Python can be used to calculate the square root of a number. See the example below for syntax.

x = 16
y = 25
x1 = x**0.5
y1 = y**0.5
print("Square root of", x, "is",x1)
print("Square root of", y, "is",y1)

The above code will give the following output:

Square root of 16 is 4.0
Square root of 25 is 5.0

Method 2: Using sqrt() method of math Module

The sqrt() method of math module can also be used to calculate square root of a number.

import math

x = 16
y = 25
x1 = math.sqrt(x)
y1 = math.sqrt(y)
print("Square root of", x, "is",x1)
print("Square root of", y, "is",y1)

The above code will give the following output:

Square root of 16 is 4.0
Square root of 25 is 5.0

Method 3: Using pow() method of math Module

The pow() method of math module can also be used to calculate square root of a number.

import math

x = 16
y = 25
x1 = math.pow(x, 0.5)
y1 = math.pow(y, 0.5)
print("Square root of", x, "is",x1)
print("Square root of", y, "is",y1)

The above code will give the following output:

Square root of 16 is 4.0
Square root of 25 is 5.0