Python pow() built-in function

From the Python 3 documentation

The pow() function returns the power of a number.It takes two or three arguments: pow(base, exp): Returns base raised to the power of exp (base ** exp). pow(base, exp, mod): Returns (base ** exp) % mod (for modular arithmetic). Result is computed more efficiently than base ** exp % mod, if mod arg is present.

Introduction

The pow() function is used for exponentiation. It can take two or three arguments.

  • pow(base, exp): This is equivalent to base ** exp.
  • pow(base, exp, mod): This is equivalent to (base ** exp) % mod, but is more efficient. This is useful for modular arithmetic.

Examples

# Using two arguments (base ** exp)
print(pow(2, 3))    # Output: 8
print(pow(3, 2))    # Output: 9
print(pow(2, -3))   # Output: 0.125 (equivalent to 1 / (2**3))

# Using three arguments ((base ** exp) % mod)
print(pow(3, 2, 4)) # Output: 1 (since 3**2 is 9, and 9 % 4 is 1)
print(pow(2, 3, 5)) # Output: 3 (since 2**3 is 8, and 8 % 5 is 3)

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.