HOWTO · Python
How to Use Euler's Number in Python
Use math.e, math.exp(), and numpy.exp() correctly for Euler's number in Python.
On this page
Use math.e when you need the floating-point value of Euler’s number, math.exp(x) when you need e raised to one scalar exponent, and numpy.exp(values) when you need that calculation for every item in an array. The math module is part of Python’s standard library; NumPy is an optional dependency for array work. These choices describe input shape and intent, not a universal speed ranking.
Euler’s number, usually written as e, is the base of natural logarithms. Python stores it as a binary floating-point value, so print formatting can change its display without changing the value used in later calculations.
Read math.e When You Need the Constant
Import e from math to read the constant directly. This is useful for labels, formulas, or a calculation that explicitly needs the base value. The output is the normal Python float representation on the tested runtime.
from math import e
print(e)
2.718281828459045
Use formatting only at the presentation boundary. For example, f"{e:.5f}" displays 2.71828, but it does not make the stored float more precise. Keep the unformatted value while calculating and round only when a reader, file, or UI needs a particular number of decimal places.
Use math.exp() for One Scalar Exponent
For a scalar x, use math.exp(x) to calculate e raised to x. It states the operation clearly and Python documents it as generally more accurate than writing math.e ** x. math.expm1(x) is a related function for the specific expression exp(x) - 1; it avoids losing significant precision when x is very close to zero.
import math
print(math.exp(3))
print(math.expm1(1e-16))
20.085536923187668
1e-16
The first line is e cubed. The second line shows the small result produced by expm1; subtracting 1 from math.exp(1e-16) can round away information before the subtraction is complete.
math.exp() is limited by the range of Python’s float. A sufficiently large finite exponent raises OverflowError, so handle that boundary when input may be unbounded instead of assuming every exponent has a representable result.
import math
try:
math.exp(1000)
except OverflowError as error:
print(type(error).__name__)
OverflowError
If overflow is expected in an application, validate the input range, change the mathematical representation, or use a domain-appropriate numerical approach. Catching the exception is useful when it is part of the program’s intended behavior, but it should not hide an unexpected input error.
Use numpy.exp() for Array Values
numpy.exp() is an element-wise universal function. Give it an array-like collection when each exponent needs a corresponding result. Install NumPy first with your project’s normal dependency tool if it is not already available; the standard-library math module does not provide array operations.
import numpy as np
exponents = np.array([0.0, 1.0, 2.0])
print(np.exp(exponents))
[1. 2.71828183 7.3890561 ]
The returned array has the same shape as the input, with e raised to each element. Use math.exp() for one scalar and numpy.exp() when array semantics are needed; NumPy may also accept a scalar, but importing it solely for one scalar calculation is unnecessary.
Choose the Method by the Input
Use math.e to read the constant, math.exp() for one scalar exponent, and numpy.exp() for element-wise array calculations. Use math.expm1() when the required result is exp(x) - 1 near zero. In all cases, remember that these APIs use finite floating-point values: formatting affects display, and very large exponential results may not fit in a float. See the official Python math documentation and NumPy exp documentation for the full API details.