Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the pow() function in Python?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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).

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring