In this program, we store the number in num and find the square root using the
Source code: For real or complex numbers
The square root of (1+2j) is 1.272+0.786j
In this program, we use the
Note: If we want to take complex number as input directly, like
The square root of 8.000 is 2.8281 objects in Python. To learn more, visit Python eval() function.
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.
Table of contents
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
2 function is a quick method of finding the square root in Python.
Let us first understand how the
2 function works in Python.
2 function takes 2 parameters, the first parameter is the numerical value, and the second parameter is the power of the numerical value.
Calculating the square root in using the exponent operator
The exponential operator, denoted with ** performs the square root operation as same as the
To make things more interesting, let’s find the square root of a number by defining a function of our own.
We have begun by defining a function named
6. We have then added the equation,
7, which is to calculate the square root and store the result in the variable
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
9. The function is then called to implement the action and print the result.
Using the sqrt() function to calculate square root
0 function is a predefined method used to find the square root in Python. Firstly, we import the
1 module to use the
In the first line, we begin with importing the
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
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
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.
In this program, we used the
0 function in the cmath module. Note that we have used the eval() function to convert the inputs to complex numbers as well.
7 also can be used to return the square-root of a negative number. For example-
In this tutorial, we have learnt how to calculate the square root of a number in Python by using the
0 function, the exponent operator, the
2 function and the cmath module.
If you need to work with whole numbers instead of floating point numbers;
0 outputs the square as an integer and rounds down to the nearest whole number. The
0 function can also be used with libraries other than the “math” library such as
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?
# Python program to find roots of quadratic equation..
# 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. ... .