    # How do you find the roots in math in Python?

In this program, we store the number in num and find the square root using the `**` exponent operator. This program works for all positive real numbers. But for negative or complex numbers, it can be done as follows.

## Source code: For real or complex numbers

``````# Find square root of real or complex numbers
# Importing the complex math module
import cmath

num = 1+2j

# To take input from the user
#num = eval(input('Enter a number: '))

num_sqrt = cmath.sqrt(num)
print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
``````

Output

`The square root of (1+2j) is 1.272+0.786j`

In this program, we use the `sqrt()` function in the `cmath` (complex math) module.

Note: If we want to take complex number as input directly, like `3+4j`, we have to use the `eval()` function instead of `float()`.

The `eval()` method can be used to convert complex numbers as input to the

```The square root of 8.000 is 2.828
```

In this short tutorial, we look at how we can calculate the square root in Python. We look at four different methods and break them down for you.

• What is a square root?
• Using the pow() function to calculate the square root in Python
• Calculating the square root in Python using the exponent operator
• Using the sqrt() function to calculate square root in Python
• Calculating the square root in Python using the cmath module
• Closing thoughts

## What is a square root?

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

For example, 6 x 6 = 36, so a square root of 36 is 6. However -6 x -6 is 36 too, so -6 is also a square root of 36.

In Python or any other Programming Language we have various methods to calculate the square root of a number. And in this tutorial, we will cover four different methods to calculate the square root of a number.

## Using the pow() function to calculate the square root

The

``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
2 function is a quick method of finding the square root in Python.

Let us first understand how the
``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
2 function works in Python.

The

``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
2 function takes 2 parameters, the first parameter is the numerical value, and the second parameter is the power of the numerical value.

#### Syntax:

``pow(x,y) # where y is the power of x``

#### Input:

``````# Using the pow() function
import math
num = float(input(" Enter a number: "))
sqRoot = math.pow(num, 0.5)
print("The square root of a given number {0} = {1}".format(num, sqRoot))
``````

#### Output:

``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````

## Calculating the square root in using the exponent operator

The exponential operator, denoted with ** performs the square root operation as same as the

``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
2 function.

To make things more interesting, let’s find the square root of a number by defining a function of our own.

#### Input:

``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````

#### Output:

``6.0``

We have begun by defining a function named

``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
6. We have then added the equation,
``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
7, which is to calculate the square root and store the result in the variable
``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
8. When we call the function we have defined, we input the number we want to find the square root of in the place of the argument or parameter
``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
9. The function is then called to implement the action and print the result.

## Using the sqrt() function to calculate square root

The

``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
0 function is a predefined method used to find the square root in Python. Firstly, we import the
``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
1 module to use the
``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
0 function.

#### Input:

``````# Using the sqrt() function to calculate the square root in Python
import math
num = int(input("Enter a number:"))
sqRoot = math.sqrt(num)
print (f"The square root of {num} is " ,sqRoot)
``````

#### Output:

``````Enter a number:16
The square root of 16 is 4.0``````

In the first line, we begin with importing the

``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
1 module, then in the next line we take the input from the user. After that we find the square root of the number using the
``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
0 method and the result will be stored in the variable, sqRoot. The final line of code makes sure that the result is printed out.

## Calculating the square root in Python using the cmath module

The

``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
5 module is used to calculate the square root of a Real or Complex number in Python. The various methods we have used so far will work fine for all positive Real numbers. But for negative or complex numbers, the cmath module proves to be useful.

#### Input:

``````# Using the cmath module to calculate the square root of real or complex numbers in Python
import math
num = eval(input(“Enter a number: “)
num_sqRoot = cmath.sqrt(num)
print(“The square root of {0} is {1:0.3f}+{2:0.3f}j”.format(num, num_sqRoot.real, num_sqRoot.imag))
``````

#### Output:

``````Enter a number: 4+4j
The square root of (4+4j) is 2.197+0.910j``````

In this program, we used the

``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
0 function in the cmath module. Note that we have used the eval() function to convert the inputs to complex numbers as well.

The

``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
7 also can be used to return the square-root of a negative number. For example-

#### Input:

``````import cmath
a = -25
print(cmath.sqrt(a))
``````

#### Output:

``````# Using the pow() function
import math
num = float(input(" Enter a number: "))
sqRoot = math.pow(num, 0.5)
print("The square root of a given number {0} = {1}".format(num, sqRoot))
``````
0

## Closing thoughts

In this tutorial, we have learnt how to calculate the square root of a number in Python by using the

``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
0 function, the exponent operator, the
``````Enter a number: 25
The square root of a given number 25.0 = 5.0
``````
2 function and the cmath module.

If you need to work with whole numbers instead of floating point numbers;

``6.0``
0 outputs the square as an integer and rounds down to the nearest whole number. The
``````# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
``````
0 function can also be used with libraries other than the “math” library such as
``6.0``
2, a Python library used for working with arrays.

### How do I find roots in Python?

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number.

### What is root () in Python?

The Python ** operator is used for calculating the power of a number. In this case, 5 squared, or 5 to the power of 2, is 25. The square root, then, is the number n, which when multiplied by itself yields the square, x. In this example, n, the square root, is 5.

### How do you find the roots of a quadratic equation in Python?

Example -.
# Python program to find roots of quadratic equation..
import math..
# function for finding roots..
def findRoots(a, b, c):.
dis_form = b * b - 4 * a * c..
sqrt_val = math.sqrt(abs(dis_form)).
if dis_form > 0:.
print(" real and different roots ").

### How do you do 3 square roots in Python?

Python 3 - Number sqrt() Method.
Description. The sqrt() method returns the square root of x for x > 0..
Syntax. Following is the syntax for sqrt() method − import math math. ... .
Parameters. x − This is a numeric expression..
Return Value. This method returns square root of x for x > 0..
Example. ... .
Output.. 