If you want to raise e to a power in Python, the Python exponential function math.exp(x) is what you’re looking for. It takes a number x and gives you back e (about 2.71828) to the power x, from the built-in math module.
import math
print(math.exp(2))
# 7.38905609893065
In this tutorial, I’ll put math.exp() to work on compound interest, a sigmoid, NumPy arrays and its log() and expm1() helpers. You’ll see real output from Python 3.12.5 in Command Prompt.
Python math.exp() Syntax
math.exp(x)
| Parameter | Required | Default | Description |
|---|---|---|---|
x | Yes | – | The power to raise e to: an int or a float (negative is fine) |
Return value: a float equal to ex. Even math.exp(0) gives 1.0, not 1.
You need import math first, or from math import exp. The Python math module ships with Python, so there’s nothing to install.
Note: results above about e709 don’t fit in a float, so math.exp() raises OverflowError instead of returning infinity.
When to Use math.exp() in Python
- Continuous compound interest and loan math
- Growth and decay models, like cooling or half-life
- Sigmoid and softmax in machine learning code
- Probability formulas like the normal and Poisson distributions
For whole exponential distributions (random samples, PDFs), Python’s SciPy is the better tool: see the SciPy exponential distribution.
Example 1: Calculate e to the Power x with math.exp()
Let me start with a few bare calls, so you can see zero and a negative power:
import math
print(math.exp(1))
print(math.exp(0))
print(math.exp(2))
print(math.exp(-1))
print(math.exp(0.5))
Output:
2.718281828459045
1.0
7.38905609893065
0.36787944117144233
1.6487212707001282

How does this work? exp(1) printed e itself, 2.718281828459045. A negative power gives a fraction (exp(-1) is 1/e, 0.3678…), and exp(0.5) is the square root of e.
Example 2: Continuous Compound Interest with math.exp()
Continuous compounding uses A = P × ert. Here’s $10,000 at 4.5% APR:
import math
principal = 10_000.00
rate = 0.045 # 4.5% APR, compounded continuously
for years in (1, 5, 10):
balance = principal * math.exp(rate * years)
print(f"After {years:>2} years: ${balance:,.2f}")
yearly = principal * (1 + rate) ** 10
print(f"Yearly compounding: ${yearly:,.2f}")
Output:
After 1 years: $10,460.28
After 5 years: $12,523.23
After 10 years: $15,683.12
Yearly compounding: $15,529.69

How does this work? math.exp(rate * years) is the growth factor. After 10 years, continuous compounding reached $15,683.12, about $153 more than the yearly version.
Pro Tip: the :,.2f spec adds commas and cents. More in formatting decimal places with f-strings, and compare it with simple interest in Python.
Example 3: Build a Sigmoid and Softmax with math.exp()
Machine learning code leans on ex. Here’s a sigmoid, plus softmax over three model scores:
import math
def sigmoid(x):
return 1 / (1 + math.exp(-x))
print(round(sigmoid(0), 2), round(sigmoid(2), 4))
scores = [2.0, 1.0, 0.1]
exps = [math.exp(s) for s in scores]
probs = [e / sum(exps) for e in exps]
print([round(p, 3) for p in probs])
Output:
0.5 0.8808
[0.659, 0.242, 0.099]

How does this work? sigmoid(0) is exactly 0.5. The list comprehension exponentiates each score, and dividing by the total gives probabilities that add up to 1.
PyTorch has this built in: see the PyTorch nn.Sigmoid layer.
Example 4: Use np.exp() on a NumPy Array
math.exp() takes one number. For a whole array, use NumPy’s np.exp():
import math
import numpy as np
hours = np.array([0, 1, 2, 3])
print(np.exp(hours))
print(500 * np.exp(0.3 * hours))
try:
math.exp(hours)
except TypeError as err:
print("TypeError:", err)
Output:
[ 1. 2.71828183 7.3890561 20.08553692]
[ 500. 674.92940379 911.0594002 1229.80155558]
TypeError: only 0-dimensional arrays can be converted to Python scalars

How does this work? np.exp() ran on every element of the NumPy array, and the second line modeled 500 users growing 30% per hour, continuously. Passing the array to math.exp() raised TypeError.
Example 5: math.log(), expm1() and exp2()
You’ll often need one of these three helpers next to exp():
import math
y = math.exp(3)
print(y, math.log(y))
print(math.exp(1e-10) - 1)
print(math.expm1(1e-10))
print(math.exp2(10))
Output:
20.085536923187668 3.0
1.000000082740371e-10
1.00000000005e-10
1024.0
How does this work? math.log() undid exp(3) and returned 3.0. For a tiny x, exp(x) - 1 lost precision, while expm1() kept it. exp2(10) (Python 3.11+) is 210.
math.exp() vs ** vs pow() in Python
Python has no separate exponential operator. The ** operator and pow() raise any base to any power, and math.e is the constant itself, so math.exp(x) equals math.e ** x:
import math
print(math.exp(2))
print(math.e ** 2)
print(pow(math.e, 2))
print(math.pow(math.e, 2))
print(2 ** 10, pow(2, 10), math.pow(2, 10))
Output:
7.38905609893065
7.3890560989306495
7.3890560989306495
7.3890560989306495
1024 1024 1024.0
| Option | Base | Returns | Best for |
|---|---|---|---|
math.exp(x) | e only | float | ex, most accurate |
x ** y | any | int or float | everyday powers |
pow(x, y) | any | int or float | powers, optional modulo |
math.pow(x, y) | any | always float | float-only math |
The last digit differs: math.exp(2) is the more accurate one. For any other base, read how exponents work in Python and the Python pow() method.
Common Mistakes with Python math.exp()
import math
tests = [lambda: math.exp(1000),
lambda: math.exp("2"),
lambda: math.exp([1, 2]),
lambda: math.exp(1 + 2j),
lambda: exp(2)]
for n, test in enumerate(tests, 1):
try:
test()
except Exception as err:
print(f"{n}. {type(err).__name__}: {err}")
import cmath
print("6.", cmath.exp(1 + 2j))
Output:
1. OverflowError: math range error
2. TypeError: must be real number, not str
3. TypeError: must be real number, not list
4. TypeError: must be real number, not complex
5. NameError: name 'exp' is not defined
6. (-1.1312043837568135+2.4717266720048188j)

- Huge powers overflow. Cap x around 709 or use NumPy, which returns
inf. - Convert strings with
float()first. - Pass one number, not a list.
- For complex numbers, use
cmath.exp(), as line 6 shows. - Write
math.exp(), or import it withfrom math import exp.
Wrapping Up the Python Exponential Function
Python’s math.exp() returns e to the power x as a float, and it’s more accurate than math.e ** x. Use np.exp() for arrays and expm1() for tiny values.
Next, browse more Python built-in functions and methods.
Here are a few more math posts worth a look:
- fixing ValueError: math domain error
- the factorial of a number in Python
- converting scientific notation to a float
- absolute value in Python
Quick Answers on Python math.exp() Function
What is the e function in Python?
There’s no e() function. Use the constant math.e (2.718281828459045) or math.exp(x), which equals ex.
How do you write e^x in Python?
Write math.exp(x) after import math. math.e ** x works too but can be off in the last digit.