當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python 3 Number sqrt()用法及代碼示例


描述

這個sqrt()方法返回 x > 0 時 x 的平方根。

用法

以下是語法sqrt()方法≫

import math

math.sqrt( x )

注意- 這個函數不能直接訪問,所以我們需要導入 math 模塊,然後我們需要使用 math 靜態對象調用這個函數。

參數

x─ 這是一個數字表達式。

返回值

此方法返回 x > 0 時 x 的平方根。

示例

下麵的例子展示了 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 3 - Number sqrt() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。