Python - Find the square root of a number

In this program, you will learn how to find the square root of the number.


Program Code : 1



    #Example 1: Square Root of the Number
    import math
    
    num = 25
    result = math.sqrt(num)
    print('The square root value of {0} number is {1}'.format(num,result))
    print('The square root value of {0} number is {1}'.format(num,int(result)))
            
            

Output


    The square root value of 25 number is 5.0
    The square root value of 25 number is 5
            

In this program, you should import math library for mathematical calculations. You should use sqrt function to calculate the square root value of the number.

An integer (int) value 25 has assgined to num variable.

In the first print statement, the result is 25.0 - the sqrt funcation returns the float value by defalut.
In the second print statement, the result is 25 - the integer value since it type casted to int data type.

Program Code : 2



    #Example 2: Square Root of the Float Number
                import math

    num = 2.25
 
    result = math.sqrt(num)
    print('The square value of {0} number is {1}'.format(num, result))
            
            

Output


    The square root value of 2.25 number is 1.5
            

In this program, you should import math library for mathematical calculations. You should use sqrt function to calculate the square root value of the number.

The decimal (float) value 2.25 has assgined to num variable.

The sqrt function returns the result is 1.5 - the float value.



The following topics can be useful to understand this program.



Related Tutorials