Skip to main content
HomeTutorialsPython

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Feb 2024  · 9 min read

Exponentiation is fundamental in various programming areas, from data analysis to algorithm design. Its applications include analyzing exponential patterns in large datasets like social media trends and performing mathematical calculations like compound growth or interest rates.

Furthermore, exponentiation plays a crucial role in Machine Learning (ML) and Artificial Intelligence (AI), especially in neural networks and image recognition. Therefore, understanding how to perform calculations with exponents effectively in Python is essential.

In this guide, we’ll explore various methods for performing exponentiation in Python. We'll examine built-in functions, functions from the math module, and those from the Numpy library. Each method has its advantages.

Additionally, we’ll delve into a practical scenario to gain a clearer understanding of exponentiation uses.

Exponents in Python

Python offers multiple ways to calculate exponents:

  • **: The double asterisk operator (**) is the simplest and basic option for exponentiation. For example, x ** y computes x raised to the power of y.
  • pow(): This built-in function takes two arguments: the base and the exponent. It returns the result of raising the base to the exponent. It also allows an optional third argument for modular exponentiation. Example: pow(x, y).
  • math.pow(): This function from the math module is similar to pow(), but it always returns a floating-point result. It takes two arguments, both of which can be floats or integers. Example: math.pow(x, y).
  • math.exp(): Another function from the math module, it returns the natural exponential of a number, meaning e raised to the power of the argument. Example: math.exp(argument).
  • np.power(): This function from the NumPy library is designed for working with arrays or matrices. It performs element-wise exponentiation. Example: np.power(array, exponent).

Python Exponent Basics

The double asterisk operator (**) is Python's most straightforward way to calculate exponentiation. This operator raises the left operand (base) to the power of the right operand (exponent). It is also called the power operator or exponent operator.

If you want to improve your Python programming skills and learn how to code like a real programmer, check out the course Python Programming.

Positive Exponent

In this code snippet, we show how to calculate exponentiation using positive exponents. For example, 2 raised to the power of 3 is calculated as 2 × 2 × 2, resulting in 8. Similarly, -10 raised to the power of 4 is calculated as -10 × -10 × -10 × -10, resulting in 10000.

base = 2
exponent = 3
print(base**exponent)  # Output: 8

base = -10
exponent = 4
print(base**exponent)  # Output: 10000

Negative Exponent

This code computes the result of raising a base to a negative exponent. For example, 5 raised to the power of -2 equals 0.04. Similarly, 25 raised to the power of -5 results in a very small value represented in scientific notation (1.024e-07).

base = 5
exponent = -2
print(base**exponent) # 0.04

base = 25
exponent = -5
print(base**exponent) # 1.024e-07

Raising a number to a negative exponent is equivalent to taking the reciprocal of the number raised to the positive exponent.

image9.png

In Python, the ** operator returns a ZeroDivisionError if you raise 0.0 to a negative power because any number divided by zero is undefined.

base = 0
exponent = -5
print(base**exponent)

The output is:

image6.png

Floating Point Exponent

The code examples show that Python correctly calculates the result of raising a positive base like 2 to a fractional exponent like 1.5. However, using a negative base with a fractional exponent results in a complex number.

base = 2
exponent = 1.5
print(base**exponent) # 2.8284271247461903

base = -0.25
exponent = 1.25
print(base**exponent) # (-0.12500000000000003-0.125j)

Diving Deeper: Built-in Functions

Python provides various built-in functions for calculating exponents, but we will focus on two: pow() and math.pow(). At first glance, these functions may appear similar, but they have some significant differences. Let’s delve into each function in detail.

pow()

The pow() function takes two mandatory arguments: base and exponent. Optionally, pow() accepts an optional third argument to calculate the modulus of the exponential value. The syntax is as follows: pow(base, exp, mod), where 'mod' represents the modulus. This function returns the remainder of the exponentiation.

Basic Exponentiation

For basic calculations, simply provide the base and exponent to the function, which works similarly to the ** operator.

r = pow(2, 3)
print(r) # 8

r = pow(4, -10)
print(r) # 2.56e-06

Floating-point Exponentiation

In the code below, we're using floating-point numbers for both bases and exponents, some of which are negative. This approach works similarly to the ** operator.

r = pow(3.5, 2)
print(r)  # 12.25

r = pow(3.5, -2)
print(r)  # 0.08163265306122448

r = pow(3.5, 3.2)
print(r)  # 55.08301986166747

r = pow(-3.5, -3.3)
print(r)  # (-0.009414432347757688+0.012957854474952653j)

r = pow(-3.5, 3.3)
print(r)  # (-36.698070584660925-50.510560868902246j)

Note: If you're dealing with basic exponentiation without needing modular calculations, the ** operator is a more concise and readable solution compared to the pow() function.

Modular Exponentiation

The optional modulus parameter of the pow() function is highly valuable when working with modular exponentiation. This is particularly useful in cryptography or dealing with large numbers, as modular exponentiation offers improved efficiency. For example, when calculating 3 raised to the power of 4 modulo 5, the result is 1, since 81 modulo 5 equals 1.

r = pow(3, 4, 5)
print(r)  # 1

r = pow(123, 456, 789)
print(r)  # 699

r = pow(2, -3, 7)
print(r)  # 1

r = pow(-13, 8, 62)
print(r)  # 7

You might wonder why using pow() directly for modular exponentiation is more efficient than pow(base, exp) % mod or (base**exp) % mod. The built-in pow() function is specifically designed for this operation, whereas the other methods perform unnecessary intermediate calculations. This can make a significant difference in performance, especially with large values.

import timeit

start_pow_mod = timeit.default_timer()
pow(500000000, 3000, 4000)
stop_pow_mod = timeit.default_timer()

pow_mod_time = stop_pow_mod - start_pow_mod

print("Time using pow(base, exp, mod): ", pow_mod_time)

start_pow_other = timeit.default_timer()
pow(500000000, 3000) % 4000
stop_pow_other = timeit.default_timer()

pow_other_time = stop_pow_other - start_pow_other

print("Time using pow(base, exp) % mod:", pow_other_time)


start_aestrisk = timeit.default_timer()
(500000000**3000) % 4000
stop_aestrisk = timeit.default_timer()

aestrisk_time = stop_aestrisk - start_aestrisk

print("Time using (base ** exp) % mod: ", aestrisk_time)


print(
    f"Speed comparison: pow(base, exp, mod) was {pow_other_time / pow_mod_time} times faster than pow(base, exp) % mod and {aestrisk_time/pow_mod_time} times faster than (base ** exp) % mod"
)

The output is:

image4.png

The pow() function can throw different errors depending on the situation.

# Trying to take the modulo of complex numbers raises a ValueError.
print(pow(2 + 3j, 4, 5))  # ValueError: complex modulo

# Taking the third argument is not allowed if the second argument is negative.
print(pow(10, -11, 4))  # ValueError: base is not invertible for the given modulus

# If the first or second argument is a float, the third argument is not allowed.
print(pow(2.4, 3, 4))  # TypeError: pow() 3rd argument not allowed unless all arguments are integers

# Passing any argument as a string results in a TypeError.
print(pow(2, 4, '3'))  # TypeError: unsupported operand type(s) for pow(): 'int', 'int', 'str'

math.pow()

The math.pow(x, n) function raises x to the power of n. The math.pow() converts the arguments to floats and returns the result as precise floating-point exponentiation. This precision becomes crucial in fields like data analysis and scientific computing, where accuracy is paramount.

Here’s the simple code. The math.pow() function returns a floating-point value, whereas the pow() function returns an integer value.

import math
print(math.pow(6, 3))  # 216.0
print(pow(6, 3))  # 216

The math.pow() function does not accept imaginary numbers.

image1.png

The math.pow() does not accept negative bases with fractional exponents as it will raise a ValueError.

image3.png

Leveraging Libraries for Advanced Exponentiation

To handle advanced exponentiation tasks, such as finding the exponential value of a number or calculating element-wise exponentiation in arrays or matrices, you can leverage two functions: np.power() and math.exp() .

np.power()

In Python, exponentiation is not limited to built-in functions. Python has a powerful library called NumPy, which enables a wide variety of mathematical operations on arrays. Sometimes, you need to handle exponentiation across entire arrays, and this is where np.power() comes in. It is designed for element-wise exponentiation of arrays.

import numpy as np
r = np.power([2, 4, 8], 2)
print(r)  # [4, 16, 64]

r = np.power([2, 4, 8], [1, 2, 3])
print(r)  # [4 64 4096]

r = np.power([[2, 3, 4], [5, 6, 7]], [1, 2, 3])
print(r) # [[2 9 64] [5 36 343]]

The np.power() function does not accept a negative exponent.

import numpy as np
r = np.power([2, 4, 8], -2)
print(r)

The output is:

image5.png

If you attempt to raise a negative number to a non-integer power, NumPy will return a NaN (Not a Number) value.

image7.png

math.exp()

The math.exp(x) function computes the exponential value of x, which is equivalent to raising Euler's number e to the power of x. Euler's number is approximately equal to 2.71828, and in mathematical notation, this operation is represented as e^x.

import math
x = 2
r = math.exp(x)
print("Exponential of", x, ":", r) # 7.38905609893065

A significant feature of math.exp(x) is its adaptability. It can handle positive numbers, negative numbers, and even floating-point numbers.

import math
print(math.exp(-2))  # 0.1353352832366127
print(math.exp(-2.5))  # 0.0820849986238988
print(math.exp(2.5))  # 12.182493960703473

Practical Applications and Examples

Exponentiation is a fundamental operation in mathematics with numerous applications in data analysis and scientific computing.

If you want to master the basics of data analysis with Python, check out the course Introduction to Python for Data Science.

Let's say you have a CSV file containing data on principal amount, interest rate, and investment period for multiple rows. You can calculate the compound interest for each row using any of the methods discussed above.

Principal,Rate,Years
1000,0.05,5
2000,0.03,10
1500,0.08,3
3000,0.06,7
2500,0.04,9
1800,0.07,4
2200,0.025,6
3500,0.09,2
2800,0.035,8
4000,0.02,5

Here’s the code:

import csv


def compound_interest(principal, rate, years):
    return principal * (1 + rate) ** years


# Read data from CSV file

data = []
with open("data.csv", newline="") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        row["Compound Interest"] = compound_interest(
            float(row["Principal"]), float(row["Rate"]), int(row["Years"])
        )
        data.append(row)
# Write data to a new CSV file with compound interest column

output_file = "data.csv"
fieldnames = ["Principal", "Rate", "Years", "Compound Interest"]

with open(output_file, mode="w", newline="") as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for row in data:
        writer.writerow(row)
print("Compound interest calculations saved to", output_file)

The result is:

image2.png

Great! You've calculated compound interest for all rows and stored it in a separate column. You can use this data for further analysis. Similarly, there are hundreds of other use cases where exponentiation is used extensively, such as modeling population growth over time.

Conclusion

In this guide, we explored five different methods for calculating exponents in Python, each offering unique advantages. You can use them for many practical scenarios, such as finding compound interest, modeling population growth, and more.

Double-asterisk operator (**): Simple and direct for everyday use.

pow(): Versatile with an optional "modulus" trick.

math.pow(): Precise results as floating-point numbers.

np.power(): Powerful for handling arrays with error checking.

math.exp(): Dedicated tool for calculating the exponential value.

If you want to grow your statistical skills and learn how to collect, analyze, and draw accurate conclusions from data using Python, check out the course Introduction to Statistics in Python.


Photo of Satyam Tripathi
Author
Satyam Tripathi
Topics

Start Your Python Journey Today!

Course

Introduction to Statistics in Python

4 hr
95.4K
Grow your statistical skills and learn how to collect, analyze, and draw accurate conclusions from data using Python.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Exploring Matplotlib Inline: A Quick Tutorial

Learn how matplotlib inline can enable you to display your data visualizations directly in a notebook quickly and easily! In this article, we cover what matplotlib inline is, how to use it, and how to pair it with other libraries to create powerful visualizations.
Amberle McKee's photo

Amberle McKee

How to Use the NumPy linspace() Function

Learn how to use the NumPy linspace() function in this quick and easy tutorial.
Adel Nehme's photo

Adel Nehme

Python Absolute Value: A Quick Tutorial

Learn how to use Python's abs function to get a number's magnitude, ignoring its sign. This guide explains finding absolute values for both real and imaginary numbers, highlighting common errors.
Amberle McKee's photo

Amberle McKee

How to Check if a File Exists in Python

Learn how to check if a file exists in Python in this simple tutorial
Adel Nehme's photo

Adel Nehme

Writing Custom Context Managers in Python

Learn the advanced aspects of resource management in Python by mastering how to write custom context managers.
Bex Tuychiev's photo

Bex Tuychiev

How to Convert a List to a String in Python

Learn how to convert a list to a string in Python in this quick tutorial.
Adel Nehme's photo

Adel Nehme

See MoreSee More