英文文檔:
-
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 than
pow(x, y) % z). The two-argument formpow(x, y)is equivalent to using the power operator:x**y. -
The arguments must have numeric types. With mixed operand types, the coercion rules for binary arithmetic operators apply. For
intoperands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example,10**2returns100, but10**-2returns0.01. If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative.
- 說明:
- 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 "<pyshell#13>", 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
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 "<pyshell#16>", 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 "<pyshell#17>", 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 "<pyshell#16>", line 1, in <module> pow(10,-2,3) ValueError: pow() 2nd argument cannot be negative when 3rd argument specified
