How to Find Square Root in Python

Posted in

How to Find Square Root in Python
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    In this Python Tutorial, you will learn how to find the square root of a number in Python. There are various techniques in Python to calculate the square root of a number. Here in this Python tutorial, we have covered all those different Python techniques to find out the square root of a number.

    Prerequisites to Build the Program

    How to Find the Square Root in Python for Real Numbers:

    (1).  use python ** exponential method to calculate the square root

    ** is the Python exponent operator, which find the exponent value for a number. By specifying the right operand to 0.5 we can calculate the square root value for the left operand or number.

    num = float(input("Enter the First Number: "))
    res = num**0.5
    print("The Square root of {0} and is:".format(num),round(res,2))

    Output

    Enter the First Number: 10
    The Square root of 10.0 and is: 3.16

    Behind the code

    In this example, we have used the python logic to find the square root of a number. The round(res,2) statement set a precision floating point value which mean here 2 represents that the res value would have only 2 number after the decimal point.

    (2). python math module sqrt() method

    In python, we have module math which contains some of the math methods that can be used to perform mathematical operations on numeric data values. math module of python provides a method sqrt() which can be used to find the square root of a number.

    Example to find the square root of a Real number using sqrt() method:

    from math import sqrt
    num = float(input("Enter the First Number: "))
    res = sqrt(num)
    print("The Square root of {0} and is:".format(num),round(res,2))

    Output

    Enter the First Number: 10
    The Square root of 10.0 and is: 3.16

    With the help of this sqrt() method, we can find the square root of the complex number.

    Example to find the square root of a complex number using sqrt() method

    to find the square root of a complex number we import the python cmath module

    import cmath
    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

    Enter a number: 2+7j
    The square root of (2+7j) is 2.154+1.625j

    (3). math module pow() method

    The pow() method is used to find the raised to the power of any number, but using it we can also find the square root of any number. The pow(a, b) method accept two arguments, the first argument should be the number whose power we want to calculate and the second argument should be the number which represents the power. pow(4,3) is equivalent to 4 3

    Example to find the square root of a Real number using pow() method:

    from math import pow
    num = float(input("Enter the First Number: "))
    res = pow(num, 0.5)
    print("The Square root of {0} and is:".format(num),round(res,2))

    Output

    Enter the First Number: 10
    The Square root of 10.0 and is: 3.16

    Behind the code

    In the above example, we use the math module's pow() method to calculate the square root of the number entered by the user. However the main objective of pow() method to calculate the power of a number, but if we pass some floating-point number we can also calculate the root of a number. With this pow() method we applied the same concept that we used when we find the square root of a number using exponential symbol **.  If we raise any number to its power 0.5 then we get its square root.

    People are also reading:

    Leave a Comment on this Post

    0 Comments