# Exponents in Python: A Complete Guide

Exponentiation is a mathematical operation that represents repeated multiplication of a number by itself. To raise a number to a power in Python, we can either use the exponentiation operator (`**`) or the `pow()` function as well as some other ways to raise a number to a power that we will cover in this post.

To use the Python exponentiation operator (`**`) to raise a number to a power, you have to place it between two or more real numbers. The number on the left will be the base while the number on the right the exponent. This operation returns a single number known as the power.

`pow()` is the official Python power function. To perform exponentiation using the `pow()` function, you have to pass in two positional parameters. The first parameter is the base and the second is the exponent. Then it returns the power of that exponentiation operation. The function also offers more features than the exponentiation operator that we will discuss in this post.

Let’s get started

## 1. Using the Exponentiation operator (**).

The simplest way to raise a number to a power in Python is to use the exponentiation operator (**). It is made of two asterisks, without a space between them (`**`). If placed between two numbers, the number on the right will act as the base and the one on the left as the exponent. The result will be the exponentiation of the two numbers.

For example, to calculate 2 to the power of 3 in Python, you would have to write 2**3, which would return the result 8.

### Examples of using the Exponentiation operator

Here are some examples of how to use the exponentiation operator (**) to calculate the exponent of numbers in Python:

``````print(2**3)  # Output: 8
print(3**4)  # Output: 81
print(4**2)  # Output: 16``````

You can also use the `%` operator in combination with the exponentiation operator ** to perform modulo exponentiation in Python. For example:

``````result = 2**3 % 5  # Calculate 2**3 % 5
print(result)  # Output: 3``````

In this example, the `**` operator calculates 2 to the power of 3 and the `%` operator returns the remainder when the result is divided by 5. The result is 3.

## 2. Using the `pow()` function.

In addition to the ** operator, Python has a built-in function called pow() that can also be used to perform exponentiation. The `pow()` function takes two arguments: the base and the exponent.

For example, to calculate 2 to the power of 3 using the `pow()` function, you would write `pow(2, 3)`. This would also return the result 8.

### Examples of using the `pow()` function

Here are some examples of using the `pow()` function to perform exponentiation in Python:

``````print(pow(2, 3))  # Output: 8
print(pow(3, 4))  # Output: 81
print(pow(4, 2))  # Output: 16``````

### Other Usage of the `pow()` function

The `pow()` function can also be used to perform modulo exponentiation, which is a type of exponentiation that calculates the remainder when a number is raised to a power and divided by a modulus.

To perform modulo exponentiation using the `pow()` function, you can pass a third argument as the modulus.

For example, to calculate 2 to the power of 3 modulo 5, you would write `pow(2, 3, 5)`. This would return the result 3.

Here is an example of using the `pow()` function to perform modulo exponentiation:

``````result = pow(2, 3, 5)  # Calculate 2**3 % 5
print(result)  # Output: 3``````

In this example, the `pow()` function calculates 2 to the power of 3 and then returns the remainder when the result is divided by 5. The result is 3.

## 3. Using the `math.pow()` function:

If you need to perform exponentiation with decimal bases or exponents, you can use the `math.pow()` function from the `math` module. This function works the same way as the `pow()` function, but it returns a float instead of an integer.

For example, to raise 2.5 to the power of 3:

``````import math
result = math.pow(2.5, 3)
print(result)  # Output: 15.625``````

In the above example, we start by importing the math module into the code. Then we use `math.pow()` to calculate 2.5 to the power of 3 which gives us 15.625.

Note that the `math.pow()` function is slower than the `**` operator or the `pow()` function, as it involves additional function calls and type conversions. Therefore, it is generally recommended to use the `**` operator or the `pow()` function for faster exponentiation in Python.

## Conclusion: Exponents in Python

In this post, we looked at how to perform exponentiation in Python using the `**` operator, the `pow()` function, and the `math.pow()` function. We also discussed the differences between these methods and when to use each one. With these techniques in your toolkit, you should now be able to easily perform exponentiation in your Python programs.

If you have any questions, let me know in the comments section below. Peace!

Exponents in Python: A Complete Guide
Scroll to top