以下是sqrt()方法的語法:
1
2
3
|
import math math.sqrt( x ) |
注意:此函數是無法直接訪問的,所以我們需要導入math模塊,然后需要用math的靜態對象來調用這個函數。
參數
- x -- 這是一個數值表達式。
返回值
此方法返回x的平方根,對于x>0。
例子
下面的例子顯示了sqrt()方法的使用。
1
2
3
4
5
6
|
#!/usr/bin/python import math # This will import math module print "math.sqrt(100) : " , math.sqrt( 100 ) print "math.sqrt(7) : " , math.sqrt( 7 ) print "math.sqrt(math.pi) : " , math.sqrt(math.pi) |
當我們運行上面的程序,它會產生以下結果:
1
2
3
|
math.sqrt( 100 ) : 10.0 math.sqrt( 7 ) : 2.64575131106 math.sqrt(math.pi) : 1.77245385091 |