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


Python math.pow()用法及代碼示例

Python math.pow() 方法

math.pow() 方法是 math 模塊的一個庫方法,它用於計算一個基數的給定冪,它接受兩個數字並返回浮點數中第一個數字的第二個數字的冪。

math.pow() 方法的語法:

    math.pow(a, b)

參數: a, b– 需要計算其冪的數字(這裏,a是基礎和b是計算結果的能力a的力量b)。

返回值: float– 它返回一個浮點值,它是a的力量b

例:

    Input:
    a = 2
    b = 3

    # function call
    print(math.pow(a, b))

    Output:
    8.0

用於演示 math.pow() 方法示例的 Python 代碼

# python code to demonstrate example of 
# math.pow() method 

# importing math module
import math 

# numbers 
a = 2
b = 3
# calculate a to the power of b
print(a, " to the power of ", b, " is = ", math.pow(a,b))

# numbers 
a = 2.2
b = 3.0
# calculate a to the power of b
print(a, " to the power of ", b, " is = ", math.pow(a,b))

# numbers 
a = 2
b = 0
# calculate a to the power of b
print(a, " to the power of ", b, " is = ", math.pow(a,b))

# numbers 
a = 0
b = 3
# calculate a to the power of b
print(a, " to the power of ", b, " is = ", math.pow(a,b))

輸出

2  to the power of  3  is =  8.0
2.2  to the power of  3.0  is =  10.648000000000003
2  to the power of  0  is =  1.0
0  to the power of  3  is =  0.0


相關用法


注:本文由純淨天空篩選整理自 math.pow() method with example in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。