How to find the square root in python

 

Python program to find the square root


In this program,you  will about the python program how to find the square root of any number

by two method:

By using exponent function 

By using the cmath module. 

For understand the program you have the knowledge:

  • Python input,output.

  • Python data types.

  • Python operators.



Example for square root of positive number

Code:



    # Python Program to calculate the square root for positive number

number = 10

number_sqrt = number ** 0.5

print('square root %0.3f is %0.3f'%(number ,number_sqrt))


Output:


    square root 10.000 is 3.162



In the above program,value is store in the number variable and then exponent with help ** operator 

with power 0.5(½) store in the number_sqrt variable.At the end of the program,display with 

help of print function.



Example for For real and complex number:

Source code:

`       


import cmath

 

number = 2+2j

 

number_sqrt = cmath.sqrt(number)

print('square root  {0} = {1:0.3f}+{2:0.3f}j' 

.format(number ,number_sqrt.real,number_sqrt.imag))



In the above example,import the library cmath and store the  complex number 

in the number variable .Use the cmath.sqrt( ) function to find the square root. 

Display with help of print function.


   



Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout