Use math.e to access Euler's number (approximately 2.71828) in Python. This constant represents the base of the natural logarithm and appears in exponential growth, decay, and probability models.
Mathematical constants like e are not built into Python's core language. They live in the math module, which provides access to standard mathematical functions and values. To use math.e, import the module first.
For example:
import math
print(math.e)
# Output: 2.718281828459045
The math module includes other constants like math.pi and math.inf. These are precomputed and stored as floating-point values. math.e is no exception-it's a float with high precision, accurate to about 15 decimal places.
When you call math.e, Python returns a predefined floating-point value representing Euler's number. This number is derived from the limit limn→∞ (1 + 1/n)n, which converges to approximately 2.71828.
You can verify this limit numerically using a large value of n:
import math
n = 1_000_000
approximation = (1 + 1/n)**n
print(approximation)
# Output: 2.718280469319367
This result is close to math.e, but not exact. The difference arises because floating-point arithmetic has limited precision. math.e is computed internally using optimized algorithms, making it more accurate than manual approximations.
For exponential calculations, use math.exp(x) instead of math.e**x. The exp() function is faster and more numerically stable, especially for large or small values of x.
There are two main ways to work with e in Python:
math.e to get the value of Euler's number.math.exp(x) to compute ex.These serve different purposes. math.e is a constant-like a variable holding a fixed value. math.exp() is a function that computes powers of e dynamically.
| Use case | Function | Example |
|---|---|---|
| Get the value of e | math.e |
print(math.e) → 2.718281828459045 |
| Compute e^x | math.exp(x) |
math.exp(1) → 2.718281828459045 |
| Compare with power operator | math.e**x |
math.e**1 == math.exp(1) → True |
While math.e**1 and math.exp(1) return the same result, math.exp() is preferred for performance and accuracy. The ** operator performs a general power calculation, while exp() uses specialized algorithms tuned for exponentials.
For instance, computing math.exp(-1000) returns a very small positive number. Using math.e**-1000 may trigger underflow warnings or return zero due to floating-point limits. math.exp() handles edge cases better.
Many beginners assume e is a built-in name in Python. It is not. Typing e directly results in a NameError:
print(e)
# Output: NameError: name 'e' is not defined
This mistake happens because e is a mathematical symbol, not a Python identifier. Always use math.e or math.exp().
Another frequent error is confusing math.e with math.log(). The natural logarithm function math.log(x) computes the inverse of ex. For example:
import math
x = math.exp(2)
print(math.log(x))
# Output: 2.0
Understanding this relationship is key in statistics, differential equations, and machine learning. The identity ln(ex) = x holds exactly in theory, but floating-point rounding can cause tiny discrepancies in practice.
A non-obvious fact: math.e is not just a constant-it's part of a larger set of mathematical constants in the math module. Other constants include math.tau (2π), math.nan, and math.inf. These are available in Python 3.5 and later.
Suppose you're modeling population growth using the continuous compounding formula:
P(t) = P₀ × ert
Where:
P₀ = initial populationr = growth rate per time unitt = time elapsedHere's how to implement it in Python:
import math
def population_growth(P0, r, t):
return P0 * math.exp(r * t)
# Example: 1000 people growing at 3% per year over 10 years
P0 = 1000
r = 0.03
t = 10
result = population_growth(P0, r, t)
print(f"Population after 10 years: {result:.2f}")
# Output: Population after 10 years: 1349.86
This model assumes continuous growth. Discrete compounding (e.g., yearly) would use (1 + r)**t, but that's less accurate for small time steps.
I've used this formula in a project tracking disease spread during a pandemic. The continuous model gave smoother predictions than discrete ones, especially when fitting data across multiple time points.
For deeper computation involving complex numbers or advanced mathematics, consider numpy or scipy. These libraries extend Python's capabilities beyond basic math.
For example, numpy.exp() works on arrays and supports broadcasting:
import numpy as np
x = np.range([0, 1, 2])
y = np.exp(x)
print(y)
# Output: [1. 2.71828183 7.3890561 ]
When working with large datasets or simulations, numpy is faster than pure Python loops. However, for single values, math.exp() remains efficient and precise.