本文共 2286 字,大约阅读时间需要 7 分钟。
python pow
Python pow() function usually takes two arguments and returns their power.
Python pow()函数通常采用两个参数并返回其幂。
Python pow() function syntax is:
Python pow()函数语法为:
pow(x, y[, z])
Let’s look into some pow() function examples.
让我们看一些pow()函数示例。
print(pow(10, 2))print(pow(-10, 3))print(pow(10, 2, 3))
Output:
输出:
100-10001
print(pow(100, -1))print(pow(100.0, 2))
Output:
输出:
0.0110000.0
print(pow(0b11, 2)) # 0b11 = 3print(pow(0b11, 2, 2))print(pow(0o11, 2)) # 0o11 = 9print(pow(0o11, 2, 2))print(pow(0xF, 2)) # 0xF = 15print(pow(0xF, 2, 2))
Output:
输出:
918112251
print(pow(2 + 3j, 2))
Output: (-5+12j)
输出: (-5+12j)
print(pow(2 + 3j, 2, 3))
Error: ValueError: complex modulo
错误: ValueError: complex modulo
print(pow(100.0, 2, 3))
Error: TypeError: pow() 3rd argument not allowed unless all arguments are integers
错误: TypeError: pow() 3rd argument not allowed unless all arguments are integers
print(pow(100, -1, 2))
Error: ValueError: pow() 2nd argument cannot be negative when 3rd argument specified
错误: ValueError: pow() 2nd argument cannot be negative when 3rd argument specified
Python also has a pow() function but the built-in function is more powerful because we can perform modulo operation too after power. Also, we don’t need to import math module for a single functionality.
Python 还具有pow()函数,但是内置函数更强大,因为我们在通电后也可以执行模运算。 另外,我们不需要为单个功能导入数学模块。
Reference:
参考:
翻译自:
python pow
转载地址:http://cxmzd.baihongyu.com/