How to find a square root of a number in python
When a number is multiplied with same number, it is called square. Square root means the number is factorised with two same numbers.
Eg:
The square root of 25 is 5.
(5x5 = 25).
The square root is represented by √.
Python implementation:
Python uses
the following modules and methods to find the square root of a number.
- ‘exponentiation’ method
- ‘math’ module
- ‘cmath’ module
- ‘numpy’ module.
Let us briefly discuss each method.
1. ‘exponentiation’ method:
This method uses
the concept ‘exponent’.
>>> no = 81
>>> sqrt = no ** 0.5
>>> print("The
Square root of the number is:", sqrt)
Output: The Square root of the number is: 9.0
Note : It returns float value.
2. ‘math’ module :
This method uses
the built-in module ‘math’.
>>> import math
>>> no = 81
>>> sqrt =
math.sqrt(no)
>>> print("The
Square root of the number is:", sqrt)
Output: The
Square root of the number is: 9.0
Note: This is suitable for non
-negative number.
3. ‘cmath’ module:
If you want to find the square root to a complex
number, this module is helpful to you.
>>>import cmath
>>> no = -81
>>> sqrt =
cmath.sqrt(no)
>>> print("The
Square root of the number is:", sqrt)
Output: The
Square root of the number is: 9j
4. ‘numpy’ Module
This module is used
for advanced Data operations. It is suitable for array also.
>>>import numpy as np
>>> no = 81
>>> sqrt = np.sqrt(no)
>>> print("The
Square root of the number is:", sqrt)
Output: The
Square root of the number is: 9j
Note : This
method is useful in scientific operations.
That’s all. these are four different ways of finding square
root of a number in python. Hope, you understand the concept. Keep coding!!!!
Comments
Post a Comment