Math.pow意思
math.pow 是 Python 中的一個函式,它屬於 math 模組,用於計算一個數的乘方。它的語法如下:
math.pow(base, exponent)
其中:
base是底數,即乘方的數。exponent是指數,即乘方的次數。
函式 math.pow 返回 base 的 exponent 次方。如果 exponent 是一個小數,它將返回 base 的 exponent 次方的浮點數。
下面是一個簡單的例子:
import math
# 計算 2 的 3 次方
result = math.pow(2, 3)
print(result) # 輸出: 8
# 計算 3 的 2.5 次方
result = math.pow(3, 2.5)
print(result) # 輸出: 13.41640625(這是一個浮點數)
在 Python 中,你也可以使用 ** 操作符來計算乘方,這是更常用的方法,因為 math.pow 需要先導入 math 模組。
# 使用 ** 操作符
result = 2 ** 3
print(result) # 輸出: 8
result = 3 ** 2.5
print(result) # 輸出: 13.41640625