# How to Calculate Power of a Number in Python?

How to calculate the power of a number in Python? Discover all the ways to calculate the exponent in Python!

**To raise a number to a power in Python, we use the Python exponent operator **.**

Therefore, to raise m to the power of n, we will use:

`n ** m`

For example:

`4 ** 2 # Result: 16`

In this comprehensive guide, let’s explore all the different operations that can be used to raise a number to a power.

Additionally, we will learn how exponent notation helps to write large numbers in a more compact format.

We will also see which way to raise the power is the most optimized and performant!

Let’s start without further ado with a reminder of what an exponent is.

## What is an exponent in mathematics?

**An exponent is the number of times a number is multiplied by itself. In mathematics, the exponent is indicated by a number in superscript, such as 2 ^{4}. In the example, 2 is the base, and 4 is the exponent: base ^ exponent.**

For example, four to the power 3 means 4^4^4, which is 64.

Now that we know what it means in math, let’s see how to calculate exponents in Python.

## How to do the exponent in Python?

There are three ways to raise a number to the power in Python:

- The operator
`**`

- The built-in function
`pow()`

- The function
`math.pow()`

from the`math`

module

Here is a table summarizing each method of calculating exponents in Python:

Method | Description | Example | Result |
---|---|---|---|

1. a ** b | raise a to the power of b | 2 ** 4 | 16 |

2. pow(a,b) | raise a to the power of b | pow(3,4) | 81 |

3. math.pow(a,b) | raise a to the power of b | math.pow(5,2) | 25 |

Let’s see how to use each of these methods.

### 1. Double asterisks (**) operator in Python

We can use the double asterisks operator `**`

to raise a number to a power in Python.

For example:

`2 ** 3 # gives 8`

This is a clear and efficient way to calculate powers in Python. This is the method I recommend for calculating powers in Python. But what about its performance? We will see that together later in the article.

### 2. The pow() function in Python

We can also use the built-in function `pow()`

to raise a number to the power.

For example:

`pow(2, 3) # gives 8`

### 3. The math.pow() function in Python

Finally, we can use another `pow()`

function, one that comes from Python’s `math`

module.

For example:

```
import math
math.pow(2, 3) # -> 8.0
```

This function does the same thing as the previous two approaches to calculating power. But it is more efficient with floating-point numbers.

But then why use `math.pow()`

rather than `pow()`

? And why not just use `**`

all the time?

## ** vs pow() vs math.pow()

As we have seen, there are three main ways to raise a number to the power in Python. Let’s see their main differences.

These three approaches work in almost the same way. But there are some slight differences that you might be interested in learning about.

`**`

is generally faster`pow()`

accepts a modulo as a third argument`math.pow()`

only uses decimal numbers`math.pow()`

does not allow imaginary numbers

Let’s go over each of these main differences in a little more detail.

### 1. How to do a power quickly in Python?

Raising to the power with the double asterisks `**`

is slightly faster than `pow()`

or `math.pow()`

. This is mainly because Python does not have to call a function but uses the operator directly.

For example, we can use `timeit`

in `Jupyter`

to directly get the execution time of the different methods.

```
## We loop 100 times ** and take the 5 best
%timeit -r5 -n100 2 ** 3
## We loop 100 times pow() and take the 5 best
%timeit -r5 -n100 pow(2, 3)
## We loop 100 times math.pow() and take the 5 best
import math
%timeit -r5 -n100 math.pow(2, 3)
```

Which gives:

- 🥇
`**`

runs in 9.54 ns - 🥈
`pow()`

runs in 59.6 ns - 🥉
`math.pow()`

runs in 81.1 ns

### 2. math.pow() for floats

`math.pow()`

treats its arguments differently from the built-in `pow()`

function or the `**`

operator. `math.pow()`

converts the arguments to floats and returns the result as a float. In comparison, the built-in `pow()`

function and the `**`

operator return the result as an integer number with integer inputs.

```
math.pow(4, 2) # 8.0
pow(4, 2) # 8
4 ** 2 # 8
```

If you want to raise a number to a power and get the result as a float, you can use `math.pow()`

. This way, you don’t have to separately convert the result to a float value.

### 3. Imaginary numbers and math.pow()

With `pow()`

and `**`

, you can use imaginary numbers but not with `math.pow()`

.

For example:

```
pow(2, 1 + 0.5j) # 1.8810842093664877+0.679354250205337j
2 ** 1 + 0.5j # 1.8810842093664877+0.679354250205337j
math.pow(2, 1 + 0.5j) # TypeError: can't convert complex to float
```

The `math.pow()`

method raises an error. So if you want to handle imaginary numbers with powers, use `pow()`

or `**`

.

### 4. Calculating modulo using pow()

The built-in function `pow()`

has a special use case for calculating a^{b} mod c. To do this, pass a third argument to the `pow()`

call.

For example, let’s calculate 3^{2} mod 4:

`pow(3, 2, 4) # gives 1`

We can also use the `**`

operator to do the same thing:

`(3 ** 2) % 4`

Let’s make a comparison using the timeit module and use significant numbers:

```
%timeit -r5 -n100 2 ** 3 % 4
%timeit -r5 -n100 pow(2, 3, 4)
```

- 🥇
`2 ** 3 % 4`

runs in 9.54 ns - 🥈
`pow(2, 3, 4)`

runs in 100 ns

In summary, there are small differences between `pow()`

, `math.pow()`

, and `**`

. It is not useful to unnecessarily clutter your brain if you are starting with Python. Just know that you will be better off using `**`

almost all the time. And that’s great because the operator is simpler to use than the other two functions!

So far, you have learned how to raise a number to a power in Python with exponents. But there is another important use case of exponents in Python that helps you to express small and large numbers.

## How to write 10 to the power in Python?

The Python exponent is also related to another similar subject. The exponent notation is a way to express large or small numbers with many zeros. You can use the e or E exponent notation to replace powers of ten.

For example, a billion (1,000,000,000) is worth 10^{9}. This means that it can be written with an exponential notation 1e09 using the letter e or E followed by the number of zeros:

```
1000000000 # A lot of 0s, difficult to read
1e09 # Much clearer, it's 1 billion
1E09 # Same as 1e09
1_000_000_000 # Since Python 3.6, you can also write large numbers like this
```

It’s the same for small numbers, for example, one billionth (0.000000001) can be difficult to read.

To denote a small number in exponent form, the notation e can be used. You can use a negative exponent (as the number is less than 1). Thus, billion becomes 1e-09.

```
0.000000001 # Hard to read
1e-09 # Easy to read
1E-09 # Same as 1e-09
```

## Going Further with Python

Congratulations! You now know how to represent numbers to the power with Python! You also know what the differences are between `**`

, `pow()`

, and `math.pow()`

(prefer `**`

, really 😛). Also, we have seen how to represent very large or very small scientific numbers.

Python is an exciting language, I often talk about it on this blog, here are some articles that might interest you:

Hey, I'm Thomas 👋 Here, you'll find articles about tech, and what I think about the world. I've been coding for 20+ years, built many projects including Startups. Check my About/Start here page to know more :).