Python built-in functions (49) -- pow, python built-in 49pow
English document:
-
pow
(
X,
Y[,
Z])
-
Return
XTo the power
Y; If
ZIs present, return
XTo the power
Y, Modulo
Z(Computed more efficiently
pow(x, y) % z
). The two-argument form
pow(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.
int
Operands, 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**2
Returns
100
,
10**-2
Returns
0.01
. If the second argument is negative, the third argument must be omitted. If
ZIs present,
XAnd
YMust be of integer types, and
YMust be non-negative.
-
Note:
-
1. the function has two required parameters x, y, and an optional parameter z. The result returns the y Power Multiplication (equivalent to x ** y) of x. If the optional parameter z has an input value, returns the power multiplication and then modulo z (equivalent to pow (x, y) % z ).
>>> pow(2,3)8>>> 2**38>>> pow(2,3,5)3>>> pow(2,3)%53
2. All parameters must be numerical.
>>> 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. If one of x and y is a floating point number, the result is converted to a floating point number.
>>> pow(2,0.1)1.0717734625362931
4. if both x and y are integers, the result is also an integer unless y is a negative number. If y is a negative number, the result returns a floating point number. The floating point number cannot be modulo, values cannot be input for all optional z parameters.
>>> 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. If the optional parameter z is passed in, x and y must be integers and y cannot be negative.
>>> 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