Yandex

Python pow() Function – Raise a Number to a Power



Python pow() Function

The pow() function in Python returns the value of a number raised to a given power. It can also return the result modulo another number. This makes it very useful in both basic arithmetic and advanced algorithms like cryptography.

Syntax

pow(base, exponent[, modulus])

Parameters:

  • base – The number to be raised (required)
  • exponent – The power to raise the base to (required)
  • modulus – The number to divide the result by and return remainder (optional)

Returns:

  • If 2 arguments: base ** exponent
  • If 3 arguments: (base ** exponent) % modulus

Example 1: Basic Power Calculation

print(pow(2, 3))
8

Example 2: Power with Negative Exponent

print(pow(2, -2))
0.25

Example 3: Power with Modulus

print(pow(2, 3, 5))
3

This returns (2**3) % 5 = 8 % 5 = 3.

Use Cases

  • Quickly raise a number to a power
  • Perform modular exponentiation efficiently (especially in large number computations)
  • Used in cryptographic algorithms like RSA

Important Notes

  • pow(a, b, c) is more efficient than (a ** b) % c
  • If modulus is provided, all values must be integers
  • For floating-point results, omit the modulus

Common Mistakes

  • Using pow() with three arguments and passing floats — will raise TypeError
  • Forgetting to use ** instead of ^ for exponentiation (Python uses ^ as bitwise XOR)

Interview Tip

In competitive programming and coding interviews, pow(a, b, c) is a fast and safe way to compute large powers under a modulus, avoiding overflow errors.

Summary

  • pow(x, y) returns x**y
  • pow(x, y, z) returns (x**y) % z efficiently
  • Useful in math, encryption, and large number problems

Practice Problem

Write a program to compute the last digit of ab for given input values:

a = int(input("Enter base: "))
b = int(input("Enter exponent: "))
print("Last digit is:", pow(a, b, 10))


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M