Square Root Calculation In Python


Python provides different functions and operators in order to calculate the square root of the given number. This number can be positive, negative, zero, real or complex number. There is also the Numpy library which providessqrt() function in order to calculate square root.

Math Module sqrt() Function

The most known and popular function is the sqrt() function which is provided by the math module or library. We can just provide the number we want to calculate square root for this function. In the following example, we will import the math module and then use the sqrt() function.

import math

math.sqrt(25)
//Result will be 5.0

math.sqrt(9)
//Result will be 3.0

math.sqrt(90)
//Result will be 9.486832980505138

math.sqrt(900)
//Result will be 30.0

math.sqrt(90000)
//Result will be 300.0
math.sqrt(49)
//Result will be 7.0
Math Module sqrt() Function
Math Module sqrt() Function

Square Root of Zero

Zero is a special number where it can produce different results according to the different operations. When we take the square root of the zero we will get zero as a result.

import math

math.sqrt(0)
//The result is 0.0

math.sqrt(0.0)
//The result is 0.0
Square Root of Zero
Square Root of Zero

Square Root Of Floating-Point Numbers

Floating-point numbers are different from decimal numbers. They provide some floating-point parts which can be confusing while calculating the square root. In this example, we will look at examples of calculating floating-point numbers.

import math

math.sqrt(9.9)
// Result is 3.146426544510455

math.sqrt(9.0)
// Result is 3.0

math.sqrt(9.9225)
// Result is 3.15

math.sqrt(99.81)
// Result is 9.990495483208027

math.sqrt(111.408025)
// Result is 10.555
Square Root Of Floating-Point Numbers
Square Root Of Floating-Point Numbers

Square Root of Negative Numbers

The square of any real number can not be negative. So if we try to get the square root of a negative number we will get an error that is related with the math domain as we can see below.

import math

math.sqrt(-25)
math.sqrt(-0)
Square Root of Negative Numbers
Square Root of Negative Numbers

Interestingly if we try to get zero as negative number square root we will get zero. Because zero cannot be positive or negative. For other negative numbers, we will get the ValueError and math domain error.

Cmath Module sqrt() Function

Math Module pow() Function

math module also provides the pow() function which is used to calculate the square of the given number. There is also ** operator which is the same as pow() function. We will provide the number and 1/2 as the square root number.

import math

math.pow(9,1/2)
//The result is 3.0

math.pow(9,0.5)
//The result is 3.0

math.pow(99.81,0.5)
//The result is 9.990495483208027

math.pow(111.408025,0.5)
//The result is 10.555

math.pow(111.408025,1/2)
//The result is 10.555
Math Module pow() Function
Math Module pow() Function

Real or Complex Numbers Square Root

We can also calculate the square root of the real or complex numbers we can use the cmath library which should be imported before usage. In these examples, we will provide the real numbers like 1+2j , 5+10j etc.

import cmath

cmath.sqrt(1+2j)
//The result is (1.272019649514069+0.7861513777574233j)

cmath.sqrt(10+2j)
//The result is (3.177895453534113+0.3146736620576702j)

cmath.sqrt(10+20j)
//The result is (4.022479320953552+2.486028939392892j)

cmath.sqrt(9+9j)
//The result is (3.29605234040343+1.365269581686682j)


cmath.sqrt(64+25j)
//The result is (8.14584352738277+1.5345249338488343j)
Real or Complex Numbers Square Root
Real or Complex Numbers Square Root

Square Root Calculation with Square Operator

Mathematics is magic where we can express different calculations in different ways. We can use square operator in order to calculate the square root. The square ** operator is used with the 1/2 number to calculate square root. We can also use 0.5 according to the 1/2 which is the same value with a different presentation.

a = (9)**(1/2)
// The result is 3.0

a=(100)**(1/2)
// The result is 10.0

a=(64)**(1/2)
// The result is 8.0

a=(64)**(0.5)
// The result is 8.0

a=(99.81)**(0.5)
// The result is 9.990495483208027

a=(111.408025)**(0.5)
// The result is 10.555
Square Root Calculation with Square Operator
Square Root Calculation with Square Operator

Numpy sqrt() Function

numpy is a third party library and module which provides calculations about matrix, series, big data, etc. numpy also provides the sqrt() and pow() functions and we can use these function to calculate square root.

import numy

numpy.sqrt(9)
//The result is 3

numpy.pow(9,1/2)
//The result is 3

Leave a Comment