Python數字sqrt()方法

Python數字sqrt()函數返回x的平方根(x > 0)。

語法

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

import math
math.sqrt( x )

注意 - 此函數不可直接訪問,需要導入math模組,然後需要使用math靜態對象調用此函數。

參數

  • x - 這是一個數字運算式。

返回值

  • 該方法返回x的平方根(x > 0)。

示例

以下示例顯示了sqrt()方法的用法。

#!/usr/bin/python3
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.6457513110645907
math.sqrt(math.pi) :  1.7724538509055159

上一篇: Python數字 下一篇: Python字串