pow() Builtin Function
Python pow()
Python pow() builtin function can be used to calculate the base to the power of a number.
In this tutorial, you will learn the syntax of pow() function, and then its usage with the help of example programs.
Syntax
The syntax of pow() function is
pow(base, exp[, mod])
where
Parameter | Description |
---|---|
base | A number. |
exp | A number. |
mod | [Optional] A number. |
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
1. Find base to the power of a number
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}.')
Output
5 to the power of 3 is 125.
2. 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}.')
Output
5 to the power of 0 is 1.
3. 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}.')
Output
5 to the power of -1 is 0.2.
4. 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}.')
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.