## TLDR

  • pow(a, n) raises a to the power n – same as a ** n
  • pow(a, n, b) adds modular exponentiation: (a ** n) % b – faster for large numbers
  • The third argument requires a positive exponent – negative exponents with modulus raise a ValueError
  • pow() beats math.pow() for integers – no float conversion, supports modulus, and handles big integers natively
  • Both two-argument and three-argument forms accept any numeric type, including negative exponents and fractions

## How pow() Works in Python

### Two-Argument Form


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

### Three-Argument Form (Modular Exponentiation)


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

### Supporting Both Forms in a Single Function


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

## pow() vs math.pow() – What’s the Difference?


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

### Performance Difference


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

## Practical Use Cases

### Modular Arithmetic in Cryptography


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

### Compound Interest Calculations


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

### Finding Large Powers Modulo a Number


x = pow(2, 5)      # 2 raised to the power 5
print(x)           # 32

y = pow(4, -2)     # 1 / (4 squared)
print(y)           # 0.0625

z = pow(2, 0.5)    # square root of 2
print(z)           # 1.4142135623730951

## FAQ

Share.
Leave A Reply