Latest web development tutorials

Python3 sqrt() 函數

Python3 數字 Python3數字


描述

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


語法

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

import math

math.sqrt( x )

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


參數

  • x -- 數值表達式。

返回值

返回數字x的平方根。

實例

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

#!/usr/bin/python3
import math   # 导入 math 模块

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.6457513110645907
math.sqrt(math.pi) :  1.7724538509055159

Python3 數字 Python3數字