Python pow()
Python pow() builtin function is used to compute base to the power of a number. Optionally we may provide a value for mod
parameter to pow() function, that computes pow(base, exponent) % mod
.
In this tutorial, we will learn about the syntax of Python pow() function, and learn how to use this function with the help of examples.
Syntax
The syntax of pow() function is
pow(base, exp[, mod])
where
Parameter | Required/ Optional | Description |
---|---|---|
base | Required | A numeric value. |
exp | Required | A numeric value. |
mod | Optional | A numeric value. |
Returns
The function returns an Integer or floating point value based on the computational result.
Examples
1. Compute Base to the Power of Exponent
In this example, we will take 5 for base, 3 for exponent and compute the power of base to exponent using pow() function.
Python Program
base = 5
exp = 3
result = pow(base, exp)
print('Result :', result)
Output
Result : 125
2. Compute Base to the Power of Negative Exponent
In this example, we will take 5 for base, -2 for exponent and compute the power of base to exponent using pow() function.
Python Program
base = 5
exp = -3
result = pow(base, exp)
print('Result :', result)
Output
Result : 0.008
3. (Base to the Power of Exponent) % mod
In this example, we will pass a value for optional parameter mod, and compute pow(base, exponent) % mod.
Python Program
base = 5
exp = 3
mod = 20
result = pow(base, exp, mod)
print('Result :', result)
Output
Result : 5
Conclusion
In this Python Tutorial, we have learnt the syntax of Python pow() builtin function, and also learned how to use this function, with the help of Python example programs.