Python Exponentiation: Use Python to Raise Numbers to a Power

Python Exponentiation Cover Image

Exponentiation in Python can be done many different ways – learn which method works best for you with this tutorial.

You’ll learn how to use the built-in exponent operator, the built-in pow() function, and the math.pow() function to learn how to use Python to raise a number of a power.

The Quick Answer: Use pow()

Quick Answer - Python Exponentiation

What is Exponentiation?

Exponentiation is a mathematical operation, often called raising a number to a power, where a given number is multiplied by itself a given number of times. This is also often called the exponent of a given number. Exponents can be raised to the power of an integer, a floating point value, and negative numbers.

Exponents are often represented in math by using a superscript. For example, 2 to the power of 3, is often represented as 23.

Following the above example, 2 to the power of 3, means multiplying 2 by itself three times, like this: 2 * 2 * 2. It may seem easy enough to do this for integers, but the problem comes when you’re raising numbers to fractional powers, such as 21.4. For this, it’s much better to use the help of some mathematical tools.

Let’s get started with learning how to use Python for exponentiation.

Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!

Python Exponentiation with the Python Exponent Operator

Python comes with many different operators, one of which is the exponent operator, which is written as **. The operator is placed between two numbers, such as number_1 ** number_2, where number_1 is the base and number_2 is the power to raise the first number to.

The Python exponent operator works with both int and float datatypes, returning a float if any of the numbers are floats. If all the numbers are integers, then it returns an integer.

Let’s try a few different examples, passing in floats, integers, and a mix of both to see what the results will look like:

# Python Exponentiation with the Exponent Operator

print(2 ** 3)
print(2.0 ** 3)
print(2 ** 3.0)
print(2.0 ** 3.0)

# Returns:
# 8
# 8.0
# 8.0
# 8.0

In the next section, you’ll learn how to use the built-in pow() function to raise a given number to a power.

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

How to Use the Python pow Function to Raise a Power

While using the Python power exponent operator is very useful, it may not always be intuitive as to what you’re hoping to accomplish. Because of this, it can be helpful to use a function that guides you and readers of your code to see what you’re doing. For this, we can use the built-in pow() (power) function.

Let’s take a look at the function: pow(base, power). We can see that the function takes two main arguments:

  1. base is the number to use as a base
  2. power is the number to raise the base to

The python pow() function will always return an integer exponentiation, when the two values are positive integers. When returning a negative power or a float power, the values will be floats. Similarly, if any value is a float, a float will be returned.

Let’s try a few examples to see what the results look like:

# Python Exponentiation with pow()

print(pow(2,2))
print(pow(2,-2))
print(pow(2,0.5))
print(pow(2,2.0))
print(pow(2.0,2))

# Returns:
# 4
# 0.25
# 1.4142135623730951
# 4.0
# 4.0

In the next section, you’ll learn how to use the math.pow() function to raise a number to a power using Python.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

How to Python math.pow to Raise a Number to a Power

Similar to the built-in function pow(), the math library also has a function that let’s you raise a number to a power. This function can be called using the math.pow() function.

The function takes two main arguments:

  1. base – the number to use as the base
  2. power – the number to use as the exponent

One of the main differences between the built-in function and math.pow() function is that the math function will always convert both numbers to a float. Because of this, the result of the function will always be a float.

Let’s see how we can use the function to emulate the results from the section above:

# Python Exponentiation with math.pow()
import math

print(math.pow(2,2))
print(math.pow(2,-2))
print(math.pow(2,0.5))
print(math.pow(2,2.0))
print(math.pow(2.0,2))

# Returns:
# 4.0
# 0.25
# 1.4142135623730951
# 4.0
# 4.0

We can see here, that all numbers that are returned are of type float.

Want to learn more about calculating the square root in Python? Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions.

Raise All Numbers in a Python List to a Power

There may be many times where you’re working with a list of numbers and you want to raise them all to a particular power. For this, we can use either a for loop or a Python list comprehension.

Let’s see how we can first use a Python for loop to accomplish this:

# Use a for loop to raise a list to a power

a_list = [1,2,3,4,5,6]
power = list()
power_value = 3

for item in a_list:
    power.append(pow(item, power_value))

print(power)

# Returns: [1, 8, 27, 64, 125, 216]

What we do here is loop over each item in a list, apply the pow() function, and append it to a new empty list.

Now let’s see how we can turn this for loop into a list comprehension:

# Use a list comprehension to raise a list to a power

a_list = [1,2,3,4,5,6]
power_value = 3

power = [pow(item, power_value) for item in a_list]
print(power)

# Returns: [1, 8, 27, 64, 125, 216]

Conclusion

In this post, you learned how to use Python for exponentiation, meaning using Python to raise a number to a power. You learned how to use the exponent operator, **, the pow() function, and the math.pow() function. You learned what the differences are between these three approaches, allowing you to choose the most appropriate one for your use case.

To learn more about the math.pow() function, check the official documentation here.

Tags: