Python pow() Function
Returns the value of x to the power of y, which is equivalent to using the **
operator, but note that the **
operator is usually faster.
Syntax
Python
Copy Code
pow(base, exp, mod = None)
Parameters
Parameter | Description |
---|---|
base |
Required. The value to be raised to the exp power |
exp |
Required. The power to raise base . |
mod |
Optional. If provided, the the function returns base**exp % mod . |
The difference between **
and the pow()
function is that
pow()
allows you to specify an optional modulus parameter, as described
in the Notes section below.
Example
Python
Copy Code
print('2 to the power of 3 is', pow(2, 3))
Output
2 to the power of 3 is 8
Notes
If the optional parameter mod is present, pow()
returns x to the power
of y modulo mod.
Example
Python
Copy Code
print('2 to the power of 3 modulo 8 is', pow(2, 3, 8))Output:
0