Square Root Calculation In Python – POFTUT

# 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 provides`sqrt()` 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. 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. 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. 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. 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`.

# 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. 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. 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. 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.