Coding

Exponent in Python: The ** Operator, pow() and math.pow()

To use an exponent in Python, write x ** y, which raises x to the power y: 2 ** 10 gives 1024. The built-in pow(x, y) does the same thing, and pow(x, y, mod) adds a modulus.

Exponent in Python with the ** operator

Python has no ^ exponent operator. The power operator is a double asterisk, and it works on ints, floats and complex numbers without any import.

print(2 ** 10)
print(5 ** 3)
print(9 ** 0.5)
print(2 ** -1)

Example output:

1024
125
3.0
0.5

The language reference says the power operator "has the same semantics as the built-in pow() function, when called with two arguments". Numeric operands are first converted to a common type, and the result has that type.

Exponent in Python with pow()

The built-in pow(base, exp, mod=None) returns base to the power exp. The docs state that the two-argument form pow(base, exp) is equivalent to base**exp, so choose by style. A function call reads better inside a longer expression or when you pass it around, for example to map().

print(pow(10, 2))
print(pow(10, -2))
print(pow(-9, 2.0))

Example output (the first two values are the ones the docs give):

100
0.01
81.0

Will the result be an int or a float?

For int operands, the result is an int unless the exponent is negative. A negative exponent converts all arguments to float, so 10 ** 2 is 100 but 10 ** -2 is 0.01. If either operand is already a float, the result is a float.

Integer powers are exact and have no upper size limit other than memory. This matters for large values:

print(2 ** 100)

Example output:

1267650600228229401496703205376

That is the exact value. A float cannot hold it exactly, which is why the next section matters. For more on float precision, see the Python float type explained.

Operator precedence: why -2 ** 2 is -4

The reference says the power operator "binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right". Two results follow from that:

  • -2 ** 2 is parsed as -(2 ** 2), so it is -4. The docs' own example is -1**2, which results in -1.
  • 2 ** -1 is parsed as 2 ** (-1), so it is 0.5 and not a syntax error.

Use parentheses when you want the negative number itself raised to a power:

print(-2 ** 2)
print((-2) ** 2)

Example output:

-4
4

Chained powers are evaluated from right to left, as the docs say for an unparenthesized sequence of power and unary operators. That means 2 ** 3 ** 2 is 2 ** (3 ** 2), which is 2 ** 9, or 512. It is not (2 ** 3) ** 2, which is 64. Add parentheses in real code even when you know the rule, because the next reader may not.

For how ** sits among the other arithmetic operators, see Python operators explained.

Modular exponent with pow(base, exp, mod)

With a third argument, pow returns base to the power exp, modulo mod. The docs say this is "computed more efficiently than pow(base, exp) % mod", which is the reason to use it for large numbers.

print(pow(2, 10, 1000))
print(pow(38, -1, mod=97))
print(23 * 38 % 97 == 1)

Example output (the last two lines are the docs' own example):

24
23
True

The first line is 1024 % 1000. The second shows a modular inverse: for int operands, if exp is negative, base must be relatively prime to mod, and Python returns the power of an inverse of base. Negative exponents with a modulus need Python 3.8 or later. Keyword arguments such as mod=97 were also added in 3.8. With three arguments, all of them must be integers and mod must be nonzero.

math.pow() is different

The math module has its own math.pow(x, y). Its docs say: "Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers."

ExpressionResult typeBehavior
2 ** 3intExact, 8
pow(2, 3)intSame as **, 8
math.pow(2, 3)float8.0
pow(2, 3, 5)intModular, 3

The practical consequence is precision and range. math.pow(2, 100) returns 1.2676506002282294e+30, a rounded float, while 2 ** 100 is exact. A float also has a maximum size, so a huge result such as math.pow(10, 400) raises OverflowError, while 10 ** 400 works. The docs also note that math.pow follows IEEE 754 for special cases, for instance math.pow(1.0, x) and math.pow(x, 0.0) always return 1.0, even when x is NaN.

One more related function: for powers of e, use math.exp(x). The docs say it is "usually more accurate than math.e ** x or pow(math.e, x)".

Roots, negative bases and zero

A fractional exponent gives a root. 16 ** 0.5 is a square root, and 27 ** (1/3) is approximately a cube root, with a small floating-point error because 1/3 is not stored exactly. For the dedicated square-root tools, see square root in Python.

The edge cases are worth knowing, and the docs cover each one:

  • Negative base, fractional exponent. The built-in operator returns a complex number. The docs say pow(-9, 0.5) returns a value close to 3j. Earlier Python versions raised ValueError here. math.pow(-8, 1/3) still raises ValueError.
  • Negative base, integral exponent. The result is a float for a float exponent: pow(-9, 2.0) is 81.0.
  • Zero to a negative power. Raising 0.0 to a negative power raises ZeroDivisionError.
  • Zero to the zero power. Python defines pow(0, 0) and 0 ** 0 to be 1.

Common mistakes

Using ^. In Python ^ is bitwise XOR. 2 ^ 3 evaluates to 1, with no error, so this bug passes silently. Use 2 ** 3.

Forgetting parentheses with a negative base. -3 ** 2 is -9. Write (-3) ** 2 for 9.

Expecting an int from a negative exponent. 2 ** -1 is 0.5. If you need integer division instead, see Python division and floor division operators.

Using math.pow for big integers. You lose exactness and can hit OverflowError. Use ** or pow().

Calling pow with strings. The result of input() is a string, so int(input()) ** 2 is what you want. A string operand raises TypeError.

Which one should you use?

  • Everyday powers: x ** y.
  • Powers inside function-style code, or when a call reads better: pow(x, y).
  • Large integers with a modulus, such as modular inverses: pow(x, y, mod).
  • Always-float math that follows IEEE 754 special cases: math.pow(x, y).
  • Powers of e: math.exp(x).

Frequently asked questions

What is the exponent operator in Python?
The exponent operator is a double asterisk: x ** y raises x to the power y. The built-in two-argument pow(x, y) is documented as equivalent. For example, 2 ** 10 and pow(2, 10) both give 1024.
Why does -2 ** 2 give -4 in Python?
The power operator binds more tightly than a unary minus on its left, so -2 ** 2 is parsed as -(2 ** 2), which is -4. Write (-2) ** 2 to square the negative number and get 4.
What does the ^ operator do in Python?
In Python, ^ is bitwise XOR, not exponentiation. For example, 2 ^ 3 is 1, while 2 ** 3 is 8. Use ** or pow() whenever you mean a power.
What is the difference between ** and math.pow?
The ** operator keeps integers as integers, so 2 ** 100 is an exact int. math.pow converts both arguments to float, so math.pow(2, 100) returns 1.2676506002282294e+30. The Python docs recommend ** or pow() for exact integer powers.
Can pow() take three arguments?
Yes. pow(base, exp, mod) returns base to the power exp, modulo mod, and is computed more efficiently than pow(base, exp) % mod. For example, pow(2, 10, 1000) is 24.

Sources