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 xyx^y.
  • For pow(x, y, z) return value is equal to xyx^y % z.

Code

The following code shows how to use the pow() in python:

# create variable x, y
x = 4
y = 8
# store result
result = 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 z
print("--------------------------------")
z = 5
print(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 x and y and assign values to them.
  • Line 5: We pass the values to the pow() function and store the result in a new variable called result.
  • Line 6–7: We display values of x and y.
  • Line 9: We display the result of pow(x,y).
  • Line 13: We create the variable z and 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 called result1.
  • Line 16: We display the result of pow(x,y,z).