import math
#First Method - No need to import math module
number = 8
sq_root = number ** 0.5
print(sqrt)
#Second Method - Using math.sqrt() method
sq_root = math.sqrt(number)
print(sq_root)
#Third Method - Using math.pow() method
sq_root = math.pow(number, 0.5)
print(sq_root)
We are using different methods in the above code snippet to calculate the square root of a number in python. The description of methods that can be helpful to find the square root of a number is as below.
We are using the exponent operator (**) here to calculate the square root of a number. You do not need to import any python module to find the square root using this method. For example, if you want to calculate the square root of 64 using the exponent operator you can use the below code.
num = 64
sq_root = num ** 0.5
print(sq_root)
8.0
You can also calculate the square root of a number in python using math.sqrt() function. But you need to import the math module into your code first. To calculate the square root of 25 you can use below code
import math
num = 25
sq_root = math.sqrt(num)
print(sq_root)
Output
5.0
math.sqrt() takes one required parameter as the number of which the square root needs to be calculated.
You can also use math.pow() method to calculate the square root. It takes two required parameters:
import math
math.pow(number, power_value)
number: [Required] The number of which the square root needs to be calculated.
power_value: [Required] The value of power used in mathematics.
As we know that math.pow() is used to calculate the power of a number.
To calculate the square root using math.pow() you can use below code.
import math
number = 50
sq_root = math.pow(number, 0.5)
print(sq_root)
Output
7.0710678118654755
0 Comments