以下是sqrt()方法的語法:
1
2
3
|
import math math.sqrt( x ) |
注意:此函數(shù)是無法直接訪問的,所以我們需要導(dǎo)入math模塊,然后需要用math的靜態(tài)對象來調(diào)用這個函數(shù)。
參數(shù)
- x -- 這是一個數(shù)值表達(dá)式。
返回值
此方法返回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) |
當(dāng)我們運行上面的程序,它會產(chǎn)生以下結(jié)果:
1
2
3
|
math.sqrt( 100 ) : 10.0 math.sqrt( 7 ) : 2.64575131106 math.sqrt(math.pi) : 1.77245385091 |