带有Python示例的math.pow()方法

Python math.pow() 方法

math.pow()方法数学模块的库方法,用于计算基数的给定幂,它接受两个数字并将第一个数字返回浮点数中第二个数字的幂。

它的语法 math.pow() 方法:

    math.pow(a, b)

Parameter(s): a,b –需要计算其幂的数字(此处,a是基数,b是将结果a乘以b的幂的幂)。

返回值: float-它返回一个浮点值,即a乘以b的幂。

示例

    Input:
    a = 2
    b = 3

    # 函数调用
    print(math.pow(a, b))

    Output:
    8.0

Python代码演示示例 math.pow() 方法

# python代码演示示例 
# math.pow() method 

# 导入数学模块
import math 

# 数字 
a = 2
b = 3
# 计算a到b的幂
print(a, " to the power of ", b, " is = ", math.pow(a,b))

# 数字 
a = 2.2
b = 3.0
# 计算a到b的幂
print(a, " to the power of ", b, " is = ", math.pow(a,b))

# 数字 
a = 2
b = 0
# 计算a到b的幂
print(a, " to the power of ", b, " is = ", math.pow(a,b))

# 数字 
a = 0
b = 3
# 计算a到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