What is the pow() function in Python?
Overview
In python, we use the pow function to return the power of a given number.
Syntax
pow(x, y, z)
Parameters
x: This represents the base.y: This represents the exponent.z: This represents the modulus. This parameter is optional.
Note: pow(x, y, z) = x**y % z
Return value
- For
pow(x, y)return value will be . - For
pow(x, y, z)return value is equal to % z.
Code
The following code shows how to use the pow() in python:
# create variable x, yx = 4y = 8# store resultresult = pow(x, y)print(f"Value of x: {x}")print(f"Value of y: {y}")print(f"Result for pow(x,y): {result}")# create variable zprint("--------------------------------")z = 5print(f"Value of z: {z}")result1 = pow(x,y,z)print(f"Result for pow(x,y,z): {result1}")print("--------------------------------")
Explanation
- Line 2–3: We create variables
xandyand assign values to them. - Line 5: We pass the values to the
pow()function and store the result in a new variable calledresult. - Line 6–7: We display values of
xandy. - Line 9: We display the result of
pow(x,y). - Line 13: We create the variable
zand assign a value to it. - Line 14: We display the value of
z. - Line 15: We pass all three values to the
pow()function and store the result in a new variable calledresult1. - Line 16: We display the result of
pow(x,y,z).