Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

pow

pow() raises a base to an exponent, optionally reducing the result modulo a third argument. Its two-argument form is equivalent to the ** operator, while the three-argument form is optimized for modular exponentiation.

How it works

pow() raises base to exp. With mod supplied, it returns that power modulo mod and computes the modular form more efficiently than calculating the power first and then applying %; numeric coercion follows binary arithmetic rules.

Syntax

pow(base, exp, mod=None)

Parameters

NameTypeDescription
basenumericThe number to raise to a power.
expnumericThe exponent applied to base.
modnumeric, optionalOptional modulus for modular exponentiation; with integer operands it must be an integer other than zero.

Returns

Returns base raised to exp, or base raised to exp modulo mod when mod is supplied.

Key points

  • With two arguments, pow(base, exp) calculates base raised to exp.
  • For ordinary exponentiation, the function and ** operator are equivalent.
  • A third argument returns the exponentiation result reduced modulo mod.
  • Use the three-argument form directly for modular exponentiation instead of calculating a separate remainder.
  • Mixed built-in numeric arguments use the same coercion rules as binary arithmetic operations.
  • A positive integer exponent can produce an integer, while a negative exponent can produce a decimal float.

Common mistakes

  • When using three arguments with integer operands, mod must be an integer and cannot be zero.
  • A negative exponent changes the integer result behavior: operands are converted to floats and the result is a float.
  • A negative numeric base combined with a non-integral exponent produces a complex result.

Example: Integer and modular powers

result = pow(7, 3, 5)
print(result)  # 3

reciprocal = pow(10, -2)
print(reciprocal)  # 0.01

The three-argument form computes a modular power efficiently; a negative exponent in the two-argument form produces a float.

When to use it

General exponentiation

Compute ordinary powers while keeping the operation readable as a function call.

Modular arithmetic

Perform modular exponentiation efficiently for integer arithmetic.

Negative exponents

Calculate reciprocal-style values using a negative exponent, such as pow(10, -2).

Interview questions

QHow does pow() differ when a third argument is provided?

pow(base, exp) computes the same value as base ** exp. Adding mod computes the power modulo mod, and this is more efficient than pow(base, exp) % mod.

Follow-up: When is the three-argument form restricted? For integer operands, base, exp, and mod must be integers, and mod cannot be zero.

QWhat does pow() return for integer operands?

For integer operands, the result normally retains the operands' type after coercion. If exp is negative, the arguments are converted to float and a float result is returned, such as pow(10, -2) producing 0.01.

Follow-up: What happens with a negative base and a fractional exponent? Python returns a complex result, such as pow(-9, 0.5) being close to 3j.

QWhat is the signature and purpose of pow()?

The built-in pow() accepts base, exp, and an optional mod argument. The optional argument selects modular exponentiation; omitting it gives ordinary exponentiation.

Follow-up: How should modular exponentiation be written? Use pow(base, exp, mod), rather than computing pow(base, exp) % mod, because the documented modular form is more efficient.

  • **
  • math.pow()
  • %

Source: Built-in functions. Checked 2026-10-15.