How to calculate powers in Python

Doing powers in Python is quick and easy. You can raise numbers to any power with just a couple of ways. The examples below use simple code you can type right into a REPL or script.

Two easy methods

Method one is the ** operator. It’s the shortest. For example, 2 ** 3 gives 8. Try other numbers. Negative exponents work too. For example, 2 ** -1 gives 0.5.

Method two is the pow() function. Use it like this: pow(2, 3). That also returns 8. It’s handy when you want a function call instead of an operator.

When you need modulo

pow() has a third option. You can do pow(2, 3, 5). That means (2 to the 3rd) mod 5, which gives 3. This is useful for big numbers and for things like cryptography. It avoids huge intermediate results.

math.pow vs pow()

There’s also math.pow in the math module. It always returns a float. Use it if you want a floating result. But pow() and ** will return an int when the result is a whole number.

Quick tips and common mistakes

Watch out for types. Negative exponents give floats. Large exponents can grow fast. Use the modulo option to keep numbers small when you can. Also, use parentheses when mixing operations so the order is clear.

But how does this affect you?

It makes math simple. You can do quick calculations, write algorithms, or test ideas in minutes. Try a few examples in your own code. Play with different bases and powers. You’ll get the hang of it fast.

Scroll to Top