Contents
Python pow()
Python pow() builtin function can be used to calculate the base to the power of a number.
Syntax
The syntax of pow() function is
pow(base, exp[, mod])
pow() function returns the value base
to the power of exp
. mod
is optional. If mod is provided then pow() returns pow(base, exp) % mod
with the modulo operation done efficiently.
Examples
Find Base to the Power
In this example, we will compute 5 to the power of 3. The base value is 5 and the power is 3.
Python Program
base = 5
exp = 3
result = pow(base, exp)
print(f'{base} to the power of {exp} is {result}.')
Run Output
5 to the power of 3 is 125.
Find Base to the Power of Zero
In this example, we will try to find the base to the power of zero. For any base value, the result should be one.
Python Program
base = 5
exp = 0
result = pow(base, exp)
print(f'{base} to the power of {exp} is {result}.')
Run Output
5 to the power of 0 is 1.
Negative Power
We can also give a negative power. In this example, we will find the value of 5 to the power of -1.
Python Program
base = 5
exp = -1
result = pow(base, exp)
print(f'{base} to the power of {exp} is {result}.')
Run Output
5 to the power of -1 is 0.2.
pow() with mod parameter
In this example we will give mod parameter to pow() function and find 5 to the power of 3 modular division with 10.
Python Program
base = 5
exp = 3
mod = 10
result = pow(base, exp, mod)
print(f'{base} to the power of {exp}, % {mod} is {result}.')
Run Output
5 to the power of 3, % 10 is 5.
Summary
In this tutorial of Python Examples, we learned the syntax of pow() function, and how to find base to a given power using pow() function with examples.