# How to calculate square root of a number in Python?

In this article, we will show you how to calculate the square root of a number in Python. Below are the various methods to accomplish this task −

• Calculating square root using sqrt()
• Calculating the square root using the pow() function
• Calculating the square root using the exponent operator
• Calculating square root using the cmath module

## What is the square root of a number?

The square root of a number is a value when multiplied by itself returns that same number.

Example − 5 x 5 = 25, hence the square root of 25 is 5. However -5 x -5 is 25 too, so -5 is also a square root of 25.

## Calculating the square root using sqrt()

Using the sqrt() function defined in math module of the Python library is the easiest way to calculate the square root of a number.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword to import the math module.
• Create a variable to store the input number.
• Use the sqrt() function of the math module to get the square root of an input number by passing the number as an argument to it.
• Print the resultant square root of an input number.

### Example

The following program returns the square root of a number using math.sqrt() function −

# importing math module
import math

# input number
inputNumber = 25

# getting the square root of an input number
squareRoot = math.sqrt(inputNumber)

# printing the square root of a number
print ("Square root of",inputNumber,"is: ", squareRoot)


### Output

On executing, the above program will generate the following output −

Square root of 25 is: 5.0


## Calculating the square root using the pow() function

In Python, the square root can be quickly determined using the pow() function.

It returns the value of x to the power of y (x^y).

### Syntax

pow(x,y)


### Parameters

• x- It is the numerical value (base value)
• y- It is the power of numerical value (exponent value)

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the pow() function of the math module to get the square root of an input number by passing the number(base value), and power(exponent value) as an argument to it. Here 0.5 is the exponent value which represents the square root.
• Print the resultant square root of an input number.

### Example

The following program returns the square root of a number using the pow() function −

# importing math module
import math

# input number
inputNumber = 25

# getting the square root of an input number
# here 0.5 is the exponent value which represents the square root
squareRoot = math.pow(inputNumber, 0.5)

# printing the square root of a number
print ("Square root of",inputNumber,"is: ", squareRoot)


### Output

On executing, the above program will generate the following output −

Square root of 25 is: 5.0


## Calculating the square root using the exponent operator

Similar to the pow() function, the exponential operator, denoted by **, performs the square root operation.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Create a function squareRoot() using the def keyword that returns the square root of a number passed to it as an argument.

• Use the if conditional statement to check whether the number is less than 0 (negative number).

• Return nothing if the condition is true i,e number is less than 0 using the return statement.

• Else return the square root of a number using the exponential operator(**).

• Create a variable to store the input number.

• Call the above-created squareRoot() function by passing the input number as an argument and print the resultant square root of a number.

### Example

The following program returns the square root of a number using the exponential operator(**).

# creating a function that returns the square root of a number passed to it
def squareRoot(num):

# returning nothing if the number is less than 0(negative number)
if num < 0:
return
else:
# else returning the square root of a number using the exponential operator(**)
return num**0.5

# input number
inputNumber = 16
# calling the squareRoot() function by passing the input number as an argument
print ("Square root of",inputNumber,"is: ", squareRoot(inputNumber))


### Output

On executing, the above program will generate the following output −

Square root of 16 is: 4.0


## Calculating the square root using the cmath module

In Python, one can use the cmath module to determine the square root of a Real or Complex number.

For all positive Real numbers, the various methods we have employed here so far will work properly. But the cmath module appears to be useful for negative or complex numbers.

The cmath.sqrt() is also used to get the square root of a negative number.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword to import the cmath module.
• Create a variable to store the input complex number.
• Use the sqrt() function of cmath module to get the square root of a complex number by passing the number as an argument to it.
• Print the square root of an input complex number

### Example

The following program returns the square root of a complex number using the cmath.sqrt() function −

# importing cmath module
import cmath

# input complex number
complexNumber = 2 + 3j

# getting the square root of a complex number using cmath.sqrt() function
squareRoot = cmath.sqrt(complexNumber)

# printing the square root of an input complex number
print ("Square root of",complexNumber,"is: ", squareRoot)


### Output

On executing, the above program will generate the following output −

Square root of (2+3j) is: (1.6741492280355401+0.8959774761298381j)


NOTE − You can also convert the inputs to complex numbers using the eval() function.

## For negative number

### Example

# importing cmath module
import cmath

# input complex number(negative number)
complexNumber = -16

# getting the square root of a complex number using cmath.sqrt() function
squareRoot = cmath.sqrt(complexNumber)

# printing the square root of an input complex number
print ("Square root of",complexNumber,"is: ", squareRoot)


### Output

On executing, the above program will generate the following output −

Square root of -16 is: 4j


## Conclusion

In this article, we learned four different Python methods for calculating the square root of a given number. We also learned how to use the cmath module to calculate the square root of a negative number.