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.
print(pow(2, 3))
print(2 ** 3)
print(pow(2, 3, 5))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)returnsbaseraised toexp.pow(base, exp)is equivalent tobase ** 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
pow(base, exp)
pow(base, exp, mod)baseis the number being raised.expis the exponent.modis 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
print(pow(2, 3))
print(pow(5, 2))
print(pow(10, 0))
print(pow(2, -3))
print(pow(9, 0.5))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.
print(2 ** 3)
print(4 ** 0.5)
print(2 ** -1)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.
print(pow(3, 4, 5))
print(pow(2, 10, 1000))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.
print(pow(2, -3))
print(pow(10, -2))
print(type(pow(2, -3)))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.
print(pow(38, -1, 97))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.
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))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.
import math
print(pow(2, 100))
print(math.pow(2, 100))
print(type(pow(2, 100)))
print(type(math.pow(2, 100)))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
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))This prints 81, 81, 729, 729, 3.0, and 3.0.
For square root, math.sqrt(x) is often clearer:
import math
print(math.sqrt(9))This prints 3.0.
Power of each number in a list
For small lists, a list comprehension is the simplest approach.
numbers = [1, 2, 3, 4]
squares = [pow(n, 2) for n in numbers]
cubes = [n ** 3 for n in numbers]
print(squares)
print(cubes)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()
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)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 0ValueError: 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()withmath.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) % modfor huge numbers. - Passing float values with the third
modargument when integer modular exponentiation is required. - Saying
modmust be positive; it must be nonzero for integerpow(). - 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.

