Python sqrt() 函數

Python 數字 Python 數字


描述

sqrt() 方法返回數字x的平方根。


語法

以下是 sqrt() 方法的語法:

import math

math.sqrt( x )

注意:sqrt()是不能直接訪問的,需要導入 math 模組,通過靜態對象調用該方法。


參數

  • x -- 數值運算式。

返回值

返回數字x的平方根。

實例

以下展示了使用 sqrt() 方法的實例:

#!/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)

以上實例運行後輸出結果為:

math.sqrt(100) :  10.0
math.sqrt(7) :  2.64575131106
math.sqrt(math.pi) :  1.77245385091

Python 數字 Python 數字