The square root of a number is a value that, when multiplied by itself, gives the original number. Every positive number has two square roots (the same value with positive and negative signs). The following is a notation of the square root: `√25 = ±5`

For a negative number, the result of extracting a square root involves complex numbers, a discussion of which is beyond the scope of this paper.

Table of Contents

## The Mathematical Representation of the Square of a Number

We all learned as children that when a number is multiplied by itself, we get its square. Also, the square of a number can be represented as a multiple multiplication of that number. Let’s try to figure this out using an example. Suppose we want to get the square of 5. If we multiply a number (in this case 5) by 5, we get the square of that number. The following notation is used to denote the square of a number: 52 = 25 When programming in Python, it is often necessary to use the square root function. There are several ways to find the square root of a number in Python.

## 1. Using the exponentiation operator

```
num = 25
sqrt = num ** (0.5)
print("The square root of "+str(num)+" is "+str(sqrt)
```

Conclusion:

`The square root of number 25 is 5.0`

Explanation: We can use the “**” operator in Python to get the square root. Any number raised to the power of 0.5 gives us the square root of that number.

## 2. Using math.sqrt()

The square root of a number can be obtained using the `sqrt()`

function from the `math`

module, as shown below. Next, we’ll see three scenarios in which we pass positive, zero, and negative numeric arguments to `sqrt()`

. a. Using a positive number as an argument.

```
import math
num = 25
sqrt = math.sqrt(num)
print("The square root of number " + str(num) + " is " + str(sqrt))
```

Conclusion: `The square root of 25 is 5.0`

. b. Using zero as an argument.

```
import math
num = 0
sqrt = math.sqrt(num)
print("The square root of a number " + str(num) + " is " + str(sqrt))
```

Conclusion: `The square root of 0 is 0.0`

. c. Using a negative number as an argument.

```
import math
num = -25
sqrt = math.sqrt(num)
print("The square root of a number " + str(num) + " is " + str(sqrt))
```

Conclusion:

```
Traceback (most recent call last):
File "C:\wb.py", line 3, in
sqrt = math.sqrt(num)
ValueError: math domain error
```

Explanation: When we pass a negative number as an argument, we get the following error “math domain error”. It follows that the argument must be greater than 0. So, to solve this problem, we have to use the `sqrt()`

function from the `cmath`

module.

## 3. Using cmath.sqrt()

The following are examples of how to use `cmath.sqrt()`

. а. Using a negative number as an argument.

```
import cmath
num = -25
sqrt = cmath.sqrt(num)
print("The square root of a number " + str(num) + " is " + str(sqrt))
```

Conclusion: `The square root of -25 is 5j`

. Explanation: For negative numbers we must use the `sqrt()`

function of the `cmath`

module, which deals with mathematical calculations on complex numbers. b. Using a complex number as an argument.

```
import cmath
num = 4 + 9j
sqrt = cmath.sqrt(num)
print("The square root of a number " + str(num) + " is " + str(sqrt))
```

Conclusion: `The square root of (4+9j) is (2.6314309606938298+1.7100961671491028j)`

. Explanation: We can also use `cmath.sqrt()`

to find the square root of a complex number.

## 4. Using np.sqrt()

```
import numpy as np
num = -25
sqrt = np.sqrt(num)
print("The square root of a number " + str(num) + " is " + str(sqrt))
```

Conclusion:

```
...
RuntimeWarning: invalid value encountered in sqrt
The square root of -25 is nan
```

## 5. Using scipy.sqrt()

```
import scipy as sc
num = 25
sqrt = sc.sqrt(num)
print("The square root of number " + str(num) + " is " + str(sqrt))
```

Conclusion: `The square root of 25 is 5.0`

. Explanation: Like the `sqrt()`

function of the numpy module, in scipy the square root of positive, zero and complex numbers can be calculated successfully, but for negative numbers it returns `nan`

with `RunTimeWarning`

.

## 6. Using sympy.sqrt()

```
import sympy as smp
num = 25
sqrt = smp.sqrt(num)
print("The square root of "+str(num)+" is "+str(sqrt)")
```

Conclusion: `The square root of 25 is 5`

. Explanation: sympy is a Python module for symbolic computation. With `sympy.sqrt()`

, we can get the square root of positive, zero, negative and complex numbers. The only difference between this and the other methods is that if the argument is an integer when using `sympy.sqrt()`

, the result is also an integer, unlike the other methods, where the return value is always a float, regardless of the data type of the argument.

## Conclusion

Finally, we come to the end of this article. At the beginning we briefly touched on the use of the square root in mathematics. We then went on to discuss the internals of the extraction function and how it can be implemented. Finally, we covered the different methods of using this function in Python.