Square Root In Python

In this short guide, I’ll show you different ways for how to find square root in python. Let’s start by discussing its maths definition.

The square root of a number is another number that, when multiplied by itself, gives back the original number.

For example, square root of 4 is 2 and 2×2 is 4.

1. Find Square Root In Python By Using Exponent Operator

The exponent operator in python represented by ** is used to perform the arithmetic exponential function of two integers.

In this code example, we will use the Exponent operator with 1/2 to calculate the square root in python.

# program to find the square root in python

# input Number 
number = 25

root = number**(1/2) # calculate square root
print(f"Square Root of {number} :",root) # print it

Here is the output of the above code.

Output :

Output of program to find square root in python

2. By using math.sqrt Function

The sqrt(number) function from the builtin maths library takes an integer as the argument and returns its square root.

from math import sqrt

# input Number 
number = 25

root = sqrt(number) # calculate square root
print(f"Square Root of {number} :",root) # print it

3. Square Root Of Negative Numbers

In maths, The square root of negative numbers does not exist. Similarly, maths.sqrt function also throws an error when it receives a negative number.

from math import sqrt

# input Number 
number = -2

root = sqrt(number) # calculate square root
print(f"Square Root of {number} :",root) # print it

Output :

ValueError: math domain error

Also read about how to find factorial in python

Leave a Reply