The square root is a mathematical operation and has several practical uses. It is utilized in numerous fields, such as engineering, geometry, architecture, and physics. It is also used to determine the area and volume of land, solve quadratic equations, and other mathematical problems. Users can also calculate the square root in different programming languages including Python.

This guide will exemplify all the methods to find the square root of certain numbers in Python.

Quick Outline

How to Find a Square Root in Python?

## How to Find a Square Root in Python?

A number’s square root is a particular value that, when multiplied by itself, gives that same (original) number whose square root is computed. The number’s square root is the reverse method of squaring a number. For example, the square of “3” is “9” and the square root of “9” is “3”.

Python provides various methods and functions to find the square root of positive, negative, real, or complex numbers. These include the “sqrt()” function, “pow()” function, “**” operator, “cmath” module, “numpy.sqrt()” function, etc.

Let’s discuss all these methods with different examples!

## Method 1: Find the Square Root Through the “sqrt()” Function

The “sqrt()” function is a built-in Python function utilized to determine the square root of the desired number. This function is defined in the “math” module of the Python library. It calculates the square root of positive numbers, such as integers, float values, and zero. Users simply need to pass the specific number as a parameter to the “sqrt()” function to find its square root.

Syntax

```math.sqrt(<num>)
```

### Parameter

The “sqrt()” function takes only one parameter i.e. “num”, which is a number whose square root is required. The parameter (num) must be greater than or equal to “0”.

### Return Value

This function returns the specified number’s square root passed in the “sqrt()” function as a parameter.

Example

In this example, we are calculating a square root of an integer, float value, and zero using the “sqrt()” function:

```import math

sqrt1 = math.sqrt(64)          #calculating square root of an integer
sqrt2 = math.sqrt(7.9)         #calculating square root of float
sqrt3 = math.sqrt(0)           #calculating square root of zero

print('Square Root of an Integer Number: ', sqrt1)
print('\nSquare Root of a Float Number: ', sqrt2)
print('\nSquare Root of Zero: ', sqrt3)
```

The below output shows the calculated square roots of the specified numbers:

Note: The “sqrt()” function does not accept a negative value as a parameter. Therefore, if the user passes any negative value to this function, it will return the error.

Look at the screenshot below. Here, we have provided a negative value i.e. “-5” to the “sqrt()” function and it has returned the ValueError as seen below:

## Method 2: Find the Square Root Through the “pow()” Function

The “pow()” function is also a built-in Python function that is defined in the “math” module. It can be used to determine the square root of a specific number in Python. Users need to pass the desired number and a power value i.e. “0.5” to the “pow()” function to calculate the square root.

Syntax

```pow(<number>, 0.5)
```

### Parameter

This function takes two parameters i.e. number and power (0.5).

• number” is the base value. It is the specific number whose square root is required.
• 0.5” is the exponent value to get the square root of the number.

### Return Value

This function returns the desired number’s square root passed in the “pow()” function as a parameter.

Example

In this example, we are calculating a square root of an integer, float value, and zero using the “pow()” function:

```import math

sqrt4 = math.pow(81, 0.5)           #calculating square root of an integer
sqrt5 = math.pow(10.5, 0.5)         #calculating square root of float
sqrt6 = math.pow(0, 0.5)            #calculating square root of zero

print('Square Root of an Integer Number: ', sqrt4)
print('\nSquare Root of a Float Number: ', sqrt5)
print('\nSquare Root of Zero: ', sqrt6)
```

In the below output, the calculated square roots of the specified numbers can be seen:

Note: The “pow()” function does not accept a negative value as a parameter. So, if the user passes any negative value to this function, it will display the error.

For example, we have provided a negative value i.e. “-81” to the “pow()” function and it has returned the ValueError as seen below:

## Method 3: Find the Square Root Through the Exponent Operator

The exponent operator is basically a “**” operator. This operator can also be utilized to compute the square root of the numbers in Python. Users need to specify the desired number and use the “**” operator with the power value “0.5” to find the square root of the specified number.

Syntax

```<num>**0.5
```

Here, replace the “<num>” with the specific number whose square root needs to be calculated.

Example

Here, we are calculating the square root of “225”, “67.34”, and “0” using the “**” operator:

```sqrt7 = 225 **0.5            #calculating square root of an Integer
sqrt8 = 67.34 **0.5          #calculating square root of a float
sqrt9 = 0 **0.5             #calculating square root of zero

print('Square root of the Integer Number: ', sqrt7)
print('\nSquare root of the Float Number: ', sqrt8)
print('\nSquare root of Zero: ', sqrt9)
```

This has calculated the square root of the provided numbers as seen below:

## Method 4: Find the Square Root Through the “cmath” Module

The “cmath” module is Python’s library module that can be utilized to calculate the square root of negative, real, or complex numbers. Users are required to import this module in the Python program and use the “sqrt()” function to find the square root by passing the specific number as a parameter to it.

Syntax

```cmath.sqrt(<num>)
```

Here, replace the “<num>” with any negative, real, or complex number.

Example

For example, we are calculating the square root of complex and negative numbers using the “cmath.sqrt()” method as seen below:

```import cmath

sqrt10 = cmath.sqrt(5 + 9j)   #calculating square root of complex numbers
sqrt11 = cmath.sqrt(-121)     #calculating square root of negative numbers

print ("Square root of Complex Number: ", sqrt10)
print ("\nSquare root of Negative Number: ", sqrt11)
```

The below output shows the calculated square roots of the complex and negative numbers:

## Method 5: Find the Square Root Through the “numpy.sqrt()” Function

Sometimes, users may want to determine the square root of array elements. Python provides the “numpy.sqrt()” function to find the element-wise square root of a specific array. To find the square root of array elements, import the “numpy” module and pass the desired array as a parameter to the “numpy.sqrt()” function.

Syntax

```numpy.sqrt(<array>)
```

Example

Here, we have defined an array named “my_arr”. Then, we are passing this array as a parameter to the “np.sqrt()” function to calculate the square root of its elements:

```import numpy as np

my_arr = np.array([0, 9, 25, 49, 81, 121])
print('NumPy array:', my_arr)

sqrt12 = np.sqrt(my_arr)      #calculating square root of array elements

print('\nSquare roots of Array Elements:', sqrt12)
```

As you can see, the element-wise square root of the specified array has been calculated:

That was all about finding the square root in Python.

Note: Access our Google Colab Notebook by clicking on the provided link.

## Final Thoughts

In Python, there are different methods to determine the square root of numbers. Users can use the “sqrt()” function, “pow()” function, or “**” operator to find the square root of positive numbers i.e. integer and float values. For the negative, real, or complex numbers, the “cmath.sqrt()” function can be utilized. Moreover, the “numpy.sqrt()” function is also used to get the square root of array elements. This guide has exemplified different methods to find the square root in Python.