How do you show powers in python?

Exponentiation is a mathematical operation where a value is multiplied a certain number of times with itself. Let’s see how we perform that task in Python.

IN THIS ARTICLE:

  • Calculate exponents in the Python programming language
  • Calculate Python exponents with the ** operator
    • Example: do exponentiation in Python with **
  • Calculate Python exponents with the pow() function
    • Example: raise numbers to a power with pow()
  • Raise numbers to a power with Python’s math.pow()
    • Example: raise numbers to a power with math.pow()
  • Process a list or array: calculate the exponent for each value
    • Exponentiate list values with Python’s list comprehension
    • Raise values to a power with Python’s for loop
  • Summary

# Calculate exponents in the Python programming language

In mathematics, an exponent of a number says how many times that number is repeatedly multiplied with itself (Wikipedia, 2019). We usually express that operation as bn, where b is the base and n is the exponent or power. We often call that type of operation “b raised to the n-th power”, “b raised to the power of n”, or most briefly as “b to the n” (Wikipedia, 2019).

Python has three ways to exponentiate values:

  • The ** operator. To program 25 we do 2 ** 5.
  • The built-in pow() function. 23 coded becomes pow(2, 3).
  • The math.pow() function. To calculate 35, we do math.pow(3, 5).

Since each approach gives the right answer, feel free to pick any. If you aren’t sure, use pow() if you need an integer outcome and math.pow() for a floating-point result.

Let’s take a closer look at each option Python offers.

# Calculate Python exponents with the ** operator

The first way to raise a number to a power is with Python’s ** operator (Matthes, 2016). This operator is also called the exponent operator (Sweigart, 2015) or power operator (Python Docs, n.d. c).

The ** operator works with two values, just like regular multiplication with * does. This time, however, we raise its left argument to the power of its right argument (Python Docs, n.d. c). Let’s say we want to calculate 33. We do that with ** like so:

The ** operator returns a ZeroDivisionError when we raise 0.0 to a negative power. And when we raise a negative number to a fractional power, it returns a complex number (Python.org, n.d. c).

# Example: do exponentiation in Python with **

Let’s see how we can use the ** operator in a Python program. The code below raises several values to a certain exponent, and then outputs the results:

# Some random values
valueA = 3
valueB = 144
valueC = -987
valueD = 25
valueE = -0.25

# Calculate the exponent for the variables
aExp = valueA ** 2
bExp = valueB ** 3
cExp = valueC ** 4
dExp = valueD ** -5
eExp = valueE ** 0.125

# Output the results
print(valueA, "^2 = ", aExp, sep="")
print(valueB, "^3 = ", bExp, sep="")
print(valueC, "^4 = ", cExp, sep="")
print(valueD, "^-5 = ", dExp, sep="")
print(valueE, "^0.125 = ", eExp, sep="")

Here we first make five different variables. We name them valueA through valueE. They have positive, negative, and floating-point values.

Then we raise each variable to a certain exponent with the ** operator. Those exponents range from -5 to 4. We store the results in new variables (aExp through eExp).

The last bit of code outputs the original and exponentiated value with Python’s print() function. As we can see from the output, most results are as expected (although -0.250.125 returned a complex number):

3^2 = 9
144^3 = 2985984
-987^4 = 949005240561
25^-5 = 1.024e-07
-0.25^0.125 = (0.7768869870150186+0.3217971264527913j)

# Calculate Python exponents with the pow() function

Another way to exponentiate values is with the built-in pow() function (Python.org, n.d. a). This function accepts two arguments. The first is the base, or the number that we want to raise to a particular power. The second is the exponent to use. pow() always calculates an exact integer power.

So to calculate 32, we use the pow() function like this:

pow() can also accept three arguments. In that case the third argument specifies the modulo of the exponentiation (Python Docs, n.d. a). That returns the remainder of exponentiation. Using pow() in that way is more efficient than the equivalent pow(base, exp) % mod.

By the way, the pow() function returns a complex number when we use it with a non-integer exponent. This differs from the math.pow() function, which errors in that case.

# Example: raise numbers to a power with pow()

Let’s look at a Python program that uses the pow() function. The code below raises 5 different numbers to as many different exponents:

# Some random values
valueA = 3
valueB = 144
valueC = -987
valueD = 25
valueE = -0.25

# Raise the variables to different powers
aExp = pow(valueA, 2)
bExp = pow(valueB, 3)
cExp = pow(valueC, 4)
dExp = pow(valueD, -5)
eExp = pow(valueE, 0.125)

# Output results
print(valueA, "^2 = ", aExp, sep="")
print(valueB, "^3 = ", bExp, sep="")
print(valueC, "^4 = ", cExp, sep="")
print(valueD, "^-5 = ", dExp, sep="")
print(valueE, "^0.125 = ", eExp, sep="")

First we make five different variables. They are positive, negative, and there’s a floating-point value. We name those variables valueA through valueE.

Then we raise each variable to a particular power. For that we call the pow() function with two arguments. The first is the value to exponentiate, the second the exponent. We put the outcome that pow() returns in variables aExp through eExp.

Next several print() statements output both the original and pow() outcome. Of note is the complex number that pow() returned for -0.250.125:

3^2 = 9
144^3 = 2985984
-987^4 = 949005240561
25^-5 = 1.024e-07
-0.25^0.125 = (0.7768869870150186+0.3217971264527913j)

# Raise numbers to a power with Python’s math.pow()

Python’s math.pow() function provides yet another way to multiply a number several times with itself. For that the function accepts two arguments: the base number and exponent (Python Docs, n.d. b).

So why another way to exponentiate values? What makes math.pow() different is that it converts both arguments to floating-point values (Python Docs, n.d. b). As a result, the function always returns a float. (For exact integer powers use the pow() function or the ** operator discussed above.)

A quick example of math.pow() is:

import math

math.pow(3, 2)
# Returns: 9.0

Here’s how math.pow() handles uncommon cases. math.pow(1.0, x) and math.pow(x, 0.0) always return 1.0. That happens even when x is zero or NaN (Python.org, n.d. b).

Also, math.pow() raises a ValueError exception when: both arguments are finite, the first argument is negative, or the second argument is not an integer (Python Docs, n.d. b).

# Example: raise numbers to a power with math.pow()

To see how the math.pow() function works in practice, let’s consider the following example program. The following code raises 5 different values to various powers with math.pow().

import math

# Some numerical values
valueA = 3
valueB = 144
valueC = -987
valueD = 25
valueE = -0.25

# Raise each variable to a certain power
aExp = math.pow(valueA, 2)
bExp = math.pow(valueB, 3)
cExp = math.pow(valueC, 4)
dExp = math.pow(valueD, -5)
eExp = math.pow(valueE, -45)

# Output the results
print(valueA, "^2 = ", aExp, sep="")
print(valueB, "^3 = ", bExp, sep="")
print(valueC, "^4 = ", cExp, sep="")
print(valueD, "^-5 = ", dExp, sep="")
print(valueE, "^-45 = ", eExp, sep="")

Before we can use the math.pow() function we have to import the math module. Then we make five different variables, each with a numerical value. We name them valueA through valueE.

Next we raise each variable to a certain power. For that we call math.pow() with two arguments. The first is the variable we made earlier. The second a positive or negative exponent. We store the function’s outcome in new variables, aExp through eExp.

Then we output the original and exponentiated value with Python’s print() function. This is what that displays:

3^2 = 9.0
144^3 = 2985984.0
-987^4 = 949005240561.0
25^-5 = 1.024e-07
-0.25^-45 = -1.2379400392853803e+27

# Process a list or array: calculate the exponent for each value

In the examples above we each time raised a single value to a certain exponent. But what if we want to exponentiate a list or array of values? Let’s find out.

# Exponentiate list values with Python’s list comprehension

One way to raise each list value to a particular power is with a list comprehension. This requires just a little bit of code and runs efficiently.

Here’s how we do that:

# Some random values
values = [
    12, 89, -12.5, 0.443,
    1310, 3110, 125, 54
]

# Raise each number to the power 3
exponents = [pow(value, 3) for value in values]

# Output both lists
print("Original list:\n", values)
print("Raised to the power 3:\n", exponents)

This code first makes a list of integer and floating-point numbers (named values). Its contents are both positive and negative values.

Then we make a list comprehension. Here the pow() function raises each value variable to the power of 3. That value variable is something that for value in values generates. That in-line for loop goes through each element in the values list, and makes that element’s value accessible through the value variable.

That’s how our list comprehension processes the entire list, executing pow() on each element. We put the resulting values in the exponents list for use later.

The last bit of code has the print() function display both the original list (values) and those numbers raised to the 3rd power (exponents). This is what that displays:

Original list:
 [12, 89, -12.5, 0.443, 1310, 3110, 125, 54]
Raised to the power 3:
 [1728, 704969, -1953.125, 0.086938307, 2248091000, 30080231000, 1953125, 157464]

By the way, making a second list isn’t always needed. When you don’t have to keep the original values, you can overwrite the list with its exponentiated values. For example:

# Raise each number to 3, and replace
# the numbers in the original 'values' list
values = [pow(value, 3) for value in values]

# Raise values to a power with Python’s for loop

The for loop is another option to process each value in a list or array. This requires a bit more code than a list comprehension, but a for loop makes more advanced behaviour possible. Plus, when the code is complex a for loop is easier to read.

Here’s how we raise values to a particular value with a regular for loop:

# Some random values
values = [
    12, 89, -12.5, 0.443,
    1310, 3110, 125, 54
]

# Exponents
powers = [
    2, 3, 4, 5,
    1.5, -3, 2, 0.5
]

results = []

# Loop through the values, and raise
# each to the specified power
for i, value in enumerate(values):
    results.append(pow(value, powers[i]))

# Output data
print("Original values:\n", values)
print("Exponents:\n", powers)
print("Outcome:\n", results)

This mini-program makes three lists. The first, values, holds the numbers we want to raise to a certain power. The second (powers) has the different exponents. With the third list (results) we collect the results of the exponentiation. This list starts off empty.

Then we make a for loop. This loop goes through all numbers in the values list. With Python’s enumerate() function we make both the list value and its index available (in the value and i variables). With that latter we have a value to index the powers list. That way we match each value (from the values list) with its corresponding exponent (from the powers list).

Inside the loop we add a new value to the results list with its append() method. To calculate that value we use Python’s built-in pow() function with two arguments. The first is a number from the values list. The other, powers[i], fetches the exponent from the powers list. (Since both lists have the same length, we can match the values in one with those in the other.)

After the loop ends we populated the results list with the outcomes from exponentiation. We display the three lists next with Python’s print() function:

Original values:
 [12, 89, -12.5, 0.443, 1310, 3110, 125, 54]
Exponents:
 [2, 3, 4, 5, 1.5, -3, 2, 0.5]
Outcome:
 [144, 704969, 24414.0625, 0.017061555810443, 47414.03800563711, 3.3244425549790494e-11, 15625, 7.3484692283495345]

If you don’t need to keep the original list, you can also overwrite its values with the exponentiated result. For that we can also use the enumerate() function. Here’s an example:

# Loop through the original 'values' list, and
# raise each number to a power (replacing the original)
for index, value in enumerate(values):
    values[index] = pow(value, 5)

LEARN MORE

  • How to square a number in Python?
  • How to get the square root of a number in Python?
  • Python code that checks if a number is a perfect square

# Summary

Exponentiation (bn) is the mathematical operation that multiples a number (b) a certain number of times (n) with itself. There are three ways to program that behaviour in Python.

The power operator (**) raises the left value to the power of the second value. For example: 2 ** 3.

The built-in pow() function does the same thing: it raises its first argument to the power of its second argument. Like this: pow(2, 3).

The math.pow() function also does exponentiation, but without exact integer powers and it always returns a floating-point value. To use this function we do: math.pow(2, 3).

References

Matthes, E. (2016). Python Crash Course: A Hands-On, Project-Based Introduction to Programming. San Francisco, CA: No Starch Press.

Python.org (n.d. a). Built-in Functions. Retrieved on October 22, 2019, from https://docs.python.org/3.8/library/functions.html

Python.org (n.d. b). math — Mathematical functions. Retrieved on October 22, 2019, from https://docs.python.org/3.8/library/math.html

Python.org (n.d. c). Expressions. Retrieved on October 30, 2019, from https://docs.python.org/3.8/reference/expressions.html

Sweigart, A. (2015). Automate The Boring Stuff With Python: Practical Programming for Total Beginners. San Francisco, CA: No Starch Press.

Wikipedia (2019, October 26). Exponentiation. Retrieved on October 30, 2019, from https://en.wikipedia.org/wiki/Exponentiation

Published December 20, 2019.

« All Python math articles

How do you write a power expression in Python?

Python pow() Function The pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z.

How do you write to the power of 10 in Python?

To raise a number to a power in Python, use the Python exponent ** operator. Python exponent is also related to another similar topic. The Python exponent notation is a way to express big or small numbers with loads of zeros. You can use the exponent notation e or E to replace the powers of ten.

What is the code for power in Python?

Python Power: ** Operator The Python ** operator is used to raise a number in Python to the power of an exponent. In other words, ** is the power operator in Python. Our program returns the following result: 25. In this expression, 5 is raised 2nd power.

How do you print to the power of 2 in Python?

Note that the operator that is used to calculate the power is ** in Python. Thus, 2 ** 2 will give 4, 2 ** 3 will give 8, and so on. You can also use the normal function to calculate the powers of two as well. The function will keep calculating and printing the power of 2 as per the instructions provided by the user.