Python pow() Function

Learn how Python pow() works with examples. Use pow(base, exp), the ** power operator, math.pow(), and pow(base, exp, mod) for modular exponentiation. Understand return types, negative powers, errors, and common mistakes.

Published

Updated

Read time 7 min read

Reviewed byDeepak Prasad

Python pow() Function

Python supports exponentiation with the built-in pow() function and the ** power operator. Both calculate a base raised to an exponent. The pow() function also accepts an optional third argument for modular exponentiation.

Use base ** exponent or pow(base, exponent) for everyday power calculations. Integer and float behavior for operands is covered in Python numbers. Use pow(base, exponent, mod) when you need (base ** exponent) % mod computed efficiently. The math.pow() function converts arguments to float, so it is not always the same as built-in pow() for exact integer powers.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


Quick answer: calculate power in Python

Use pow(2, 3) or 2 ** 3 to calculate 2 to the power of 3. Use pow(2, 3, 5) to calculate (2 ** 3) % 5 efficiently.

python
print(pow(2, 3))
print(2 ** 3)
print(pow(2, 3, 5))
Output

This prints 8, 8, and 3.


Python pow() quick reference

Task Use
2 to the power 3 pow(2, 3)
Power operator 2 ** 3
Square a number pow(x, 2) or x ** 2
Cube a number pow(x, 3) or x ** 3
Square root pow(x, 0.5)
Reciprocal power pow(x, -1)
Modular power pow(base, exp, mod)
Exact integer power pow() or **
Float math power math.pow(x, y)
NumPy array power numpy.power(array, exp)

What does pow() do in Python?

pow() is a built-in Python function.

  • pow(base, exp) returns base raised to exp.
  • pow(base, exp) is equivalent to base ** exp.
  • pow(base, exp, mod) returns modular exponentiation.
  • With two arguments, it works with integers, floats, and complex numbers.

The Python documentation defines the two-argument form as equivalent to base**exp.


Python pow() syntax

python
pow(base, exp)
pow(base, exp, mod)
Output
  • base is the number being raised.
  • exp is the exponent.
  • mod is the optional modulus.

For three-argument pow(), base, exp, and mod are used for modular exponentiation. For integer operands, mod must be an integer and must be nonzero.


Basic pow() examples

python
print(pow(2, 3))
print(pow(5, 2))
print(pow(10, 0))
print(pow(2, -3))
print(pow(9, 0.5))
Output

This prints 8, 25, 1, 0.125, and 3.0.


Python power operator ()

** is Python's exponentiation operator—the same role as pow() for two-argument math. For how ** differs from ^ and other symbols, see Python operators.

python
print(2 ** 3)
print(4 ** 0.5)
print(2 ** -1)
Output

This prints 8, 2.0, and 0.5. Python's expression reference notes that 2**-1 evaluates to 0.5.

For simple inline math, ** is usually the most readable choice.


pow() vs operator

Feature pow() **
Basic power Yes Yes
Easy inline math Good Best
Function call style Yes No
Modular exponentiation Yes, with third argument No direct third argument
Readability for simple powers Good Usually better
Readability for modular power Better Less direct

For simple exponentiation, use whichever is clearer. For modular exponentiation, prefer pow(base, exp, mod).


pow() with third argument: modular exponentiation

pow(base, exp, mod) computes (base ** exp) % mod. It is more efficient than calculating the full power first and then using %, especially for large integers.

python
print(pow(3, 4, 5))
print(pow(2, 10, 1000))
Output

The first line prints 1 because (3 ** 4) % 5 is 81 % 5. The second prints 24.

This form is useful in hashing, number theory, and cryptography. RSA and similar algorithms use modular exponentiation, but you do not need crypto background to use the three-argument form.

The Python docs state that three-argument pow() is computed more efficiently than pow(base, exp) % mod.


pow() with negative exponent

With two arguments, a negative exponent returns a reciprocal power and usually produces a float.

python
print(pow(2, -3))
print(pow(10, -2))
print(type(pow(2, -3)))
Output

This prints 0.125, 0.01, and <class 'float'>. pow(2, -3) does not return an integer.


pow() with negative exponent and mod

In modern Python, three-argument pow() can accept a negative exponent for integers when the base is relatively prime to mod. Python computes a modular inverse.

python
print(pow(38, -1, 97))
Output

This prints 23, the modular inverse of 38 modulo 97.

This is advanced usage. Learn two-argument pow() and positive-exponent modular pow() first.

Python 3.8 added support for negative exponents in three-argument pow() when the base has a modular inverse.


pow() return type

Return type depends on the arguments. For how Python represents integers, floats, and complex values, see Python numbers:

  • Integer base and non-negative integer exponent usually return int.
  • Negative exponent usually returns float.
  • Float inputs usually return float.
  • Negative base with non-integer exponent can return complex.
  • Three-argument integer pow() returns an integer result.
python
print(type(pow(2, 3)))
print(type(pow(2, -3)))
print(type(pow(4, 0.5)))
print(type(pow(4, 2, 5)))
print(pow(-4, 0.5))
Output

The first four lines print <class 'int'>, <class 'float'>, <class 'float'>, and <class 'int'>. The last line prints a complex result because (-4) ** 0.5 is not a real number.


built-in pow() vs math.pow()

Function Return behavior Best for
pow(x, y) Preserves integer arithmetic when possible General Python exponentiation
x ** y Same exponentiation operator Simple expressions
math.pow(x, y) Converts arguments to float Float math
pow(x, y, mod) Modular exponentiation Large integer modular math

math.pow() is not the same as built-in pow() for exact integer powers because math.pow() converts both arguments to float.

python
import math

print(pow(2, 100))
print(math.pow(2, 100))
print(type(pow(2, 100)))
print(type(math.pow(2, 100)))
Output

Both numeric results are 1267650600228229401496703205376, but built-in pow() returns int while math.pow() returns float.

The Python docs recommend built-in pow() or ** for exact integer powers because math.pow() converts both arguments to float.


Square, cube, and square root in Python

python
x = 9

print(x ** 2)
print(pow(x, 2))
print(x ** 3)
print(pow(x, 3))
print(x ** 0.5)
print(pow(x, 0.5))
Output

This prints 81, 81, 729, 729, 3.0, and 3.0.

For square root, math.sqrt(x) is often clearer:

python
import math

print(math.sqrt(9))
Output

This prints 3.0.


Power of each number in a list

For small lists, a list comprehension is the simplest approach.

python
numbers = [1, 2, 3, 4]

squares = [pow(n, 2) for n in numbers]
cubes = [n ** 3 for n in numbers]

print(squares)
print(cubes)
Output

This prints [1, 4, 9, 16] and [1, 8, 27, 64]. You can also use map() with a lambda when a functional style fits your code.


Common errors with pow()

python
calls = [
    lambda: pow(2),
    lambda: pow("a", 2),
    lambda: pow(3, 2, 0),
    lambda: pow(2, -1, 4),
]

for call in calls:
    try:
        call()
    except Exception as error:
        print(type(error).__name__ + ":", error)
Output

This prints:

  • TypeError: pow() missing required argument 'exp'
  • TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
  • ValueError: pow() 3rd argument cannot be 0
  • ValueError: base is not invertible for the given modulus

With integer operands and mod present, mod must be nonzero. A negative exponent requires the base to be relatively prime to mod. Passing a float mod with integer base and exp raises TypeError.


Common mistakes with Python pow()

  • Confusing built-in pow() with math.pow().
  • Using math.pow() when an exact integer result is needed.
  • Forgetting ** is the power operator, not ^.
  • Using ^ and expecting exponentiation; in Python, ^ is bitwise XOR.
  • Assuming pow(x, y, mod) has the same cost as (x ** y) % mod for huge numbers.
  • Passing float values with the third mod argument when integer modular exponentiation is required.
  • Saying mod must be positive; it must be nonzero for integer pow().
  • Expecting pow(2, -3) to return an integer.
  • Ignoring complex results for negative bases with fractional exponents.
  • Turning a beginner pow() guide into a cryptography article.

Summary

pow(base, exp) raises base to exp. base ** exp does the same for normal exponentiation. pow(base, exp, mod) performs efficient modular exponentiation. math.pow() converts arguments to float, so use built-in pow() or ** for exact integer powers. Use ** for simple power expressions and pow() when you need the optional mod argument.


References


Frequently Asked Questions

1. What does pow() do in Python?

pow(base, exp) returns base raised to exp. It is equivalent to base ** exp for normal exponentiation. pow(base, exp, mod) performs efficient modular exponentiation.

2. Is pow() the same as the ** operator in Python?

For two arguments, yes. pow(2, 3) and 2 ** 3 both return 8. Only pow() accepts a third mod argument for modular exponentiation.

3. What is the third argument to pow()?

The optional mod argument computes (base ** exp) % mod more efficiently than raising the full power first and then applying modulo.

4. What is the difference between pow() and math.pow()?

Built-in pow() preserves exact integer arithmetic when possible. math.pow() converts both arguments to float, so it is not the same for large exact integer powers.

5. Can pow() use negative exponents?

Yes with two arguments. pow(2, -3) returns 0.125. With three integer arguments, a negative exponent requires the base to be relatively prime to mod.

6. Does ^ mean power in Python?

No. ^ is bitwise XOR in Python. Use ** or pow() for exponentiation.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …