EN
Python - math.sqrt() method example
0 points
math.sqrt
is a method that returns a number which is the square root of input value. The method works only on positive real numbers.
xxxxxxxxxx
1
import math
2
3
print( math.sqrt( 4 ) ) # 2
4
print( math.sqrt( 9 ) ) # 3
5
6
print( math.sqrt( 2 ) ) # 1.4142135623730951
7
print( math.sqrt( 0.5 ) ) # 0.7071067811865476
8
print( math.sqrt( 0 ) ) # 0
9
print( math.sqrt( -1 ) ) # ValueError
Syntax |
xxxxxxxxxx 1 math.sqrt(x) |
Parameters | x - number to find the square of. Value in the range 0 to infinity . |
Result |
Square root If the operation can not be executed |
Description | sqrt is a method that returns a number which is square root of input value. The method works only on positive real numbers. |
In this example, the way how to calculate square root using power function is presented.
xxxxxxxxxx
1
import math
2
3
4
def calculate_sqrt(value):
5
return math.pow(value, 0.5)
6
7
8
#Examples:
9
print( calculate_sqrt( 4 ) ) # 2
10
print( calculate_sqrt( 9 ) ) # 3
11
12
print( calculate_sqrt( 2 ) ) # 1.4142135623730951
13
print( calculate_sqrt( 0.5 ) ) # 0.7071067811865476
14
print( calculate_sqrt( 0 ) ) # 0
15
print( calculate_sqrt( -1 ) ) # ValueError