英文文档
pow
(x, y[, z])Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently thanpow(x, y) % z). The two-argument formpow(x, y)is equivalent to using the power operator:x**y.
pow()
pow(x, y[, z])返回两个数值的幂运算值或其与指定整数的模值。
1、函数有两个必需参数x,y和一个可选参数z,结果返回x的y次幂乘(相当于x**y),如果可选参数z有传入值,则返回幂乘之后再对z取模(相当于pow(x,y)%z)
>>> pow(2,3)
8
>>> 2**3
8
>>> pow(2,3,5)
3
>>> pow(2,3)%5
3
2、所有的参数必须是数值类型。
>>> pow('2',3)
Traceback (most recent call last):
File " " , line 1, in <module>
pow('2',3)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
3、如果x,y有一个是浮点数,则结果将转换成浮点数。
>>> pow(2,0.1)
1.0717734625362931
>>> 2**0.1
1.0717734625362931
4、如果x,y都是整数,则结果也是整数,除非y是负数;如果y是负数,则结果返回的是浮点数,浮点数不能取模,所以可选参数z不能传入值。
>>> pow(10, 2)
100
>>> pow(10, -2)
0.01
>>> pow(10, -2, 3)
Traceback (most recent call last):
File " " , line 1, in <module>
pow(10, -2, 3)
ValueError: pow() 2nd argument cannot be negative when 3rd argument specified
5、如果可选参数z传入了值,x,y必须为整数,且y不能为负数。
>>> pow(2,3,5)
3
>>> pow(10,0.1,3)
Traceback (most recent call last):
File " " , line 1, in <module>
pow(10,0.1,3)
TypeError: pow() 3rd argument not allowed unless all arguments are integers
>>> pow(10,-2,3)
Traceback (most recent call last):
File " " , line 1, in <module>
pow(10,-2,3)
ValueError: pow() 2nd argument cannot be negative when 3rd argument specified
小结
希望通过上面的操作能帮助大家。如果你有什么好的意见,建议,或者有不同的看法,希望你留言和我进行交流、讨论。