当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。