Coding

e in Python: math.e, exp() and Scientific Notation (1e3)

In Python, e means one of two things: math.e is Euler's number, 2.718281828459045, and the e inside a number like 1e3 is scientific notation for "times ten to the power of", so 1e3 is 1000.0. They are unrelated, and mixing them up is the usual source of confusion.

import math

print(math.e)
print(1e3)
print(1.5e-4)

Example output:

2.718281828459045
1000.0
0.00015

How to get Euler's number (math.e)

Euler's number is a constant in the standard library math module. The Python documentation defines math.e as "the mathematical constant e = 2.718281…, to available precision". There is nothing to install.

import math

print(math.e)

from math import e
print(e)

Both forms give the same value. A bare e with no import raises NameError: name 'e' is not defined, because Python has no built-in constant with that name. Be careful with from math import e in code that also catches exceptions as e, for example except ValueError as e. That reuses the name and replaces the constant for the rest of that scope. Keeping the math. prefix avoids the clash. The same advice applies to pi in Python.

"Available precision" means a float. Python floats are 64-bit IEEE 754 values, so math.e is the nearest float to the real e, which has infinitely many digits. You get roughly 15 to 17 significant digits. For background on why floats behave this way, see Python float type explained.

How to raise e to a power: math.exp()

To compute e to the power x, use math.exp(x). The documentation describes it as returning "e raised to the power x" and adds that it "is usually more accurate than math.e ** x or pow(math.e, x)". Because math.e is already rounded, raising it to a power compounds that rounding, while exp() works from x directly.

import math

print(math.exp(0))
print(math.exp(1))
print(math.e ** 1)

Example output:

1.0
2.718281828459045
2.718281828459045

For the power operator itself, see Python exponent operator and pow().

Small values: math.expm1()

When x is very small, exp(x) - 1 loses precision, because the subtraction cancels the leading digits. The documentation provides math.expm1(x), which returns e to the power x minus 1 and keeps full precision. The docs show this comparison:

from math import exp, expm1

print(exp(1e-5) - 1)
print(expm1(1e-5))

Example output, taken from the Python documentation:

1.0000050000069649e-05
1.0000050000166668e-05

The two results agree only to about 11 places. Use expm1() for things like tiny interest rates or growth rates.

The natural logarithm: math.log()

The inverse of exp() is the natural logarithm, which is the logarithm to base e. In Python that is math.log(x) with one argument. With two arguments, math.log(x, base) returns the logarithm to the given base, calculated as log(x) / log(base).

import math

print(math.log(math.e))
print(math.log(100, 10))
print(math.log10(100))
print(math.log2(8))

Example output:

1.0
2.0
2.0
3.0

The documentation says log10() and log2() are usually more accurate than log(x, 10) and log(x, 2), so prefer them for those two bases. Note that math.log is the natural log, not base 10. People coming from other languages or calculators sometimes expect the opposite.

What 1e3 means: scientific notation in Python

The other e is part of the number syntax. The language reference says a float literal can end with an exponent: the letter e or E, an optional sign, and digits. The e or E "represents times ten raised to the power of". The reference gives these examples:

1.0e3        # 1.0 x 10**3, which is 1000.0
1.166e-5     # 1.166 x 10**-5, which is 0.00001166
6.02214076e+23   # 6.02214076 x 10**23
1e3          # same as 1.0e3, the decimal point may be omitted
0e0          # same as 0.

No spaces are allowed inside the literal, and the exponent must be an integer. 1e3 is a float, not an int, even though 1000 is a whole number. Check with type(1e3), which gives <class 'float'>. If you need an integer, write 1000, 10**3, or convert with int(1e3).

Scientific notation is handy for constants that are very large or very small, such as a tolerance (1e-9) or a timeout in nanoseconds (5e9). A reader can see the magnitude at a glance, which is harder with a long string of zeros. Underscores are also allowed in digit groups, so 1_000.5e3 is valid.

Why Python prints 1e-05 and 1e+16

Python's default output switches to scientific notation for very small and very large floats. This is only display. The stored number is identical.

print(0.0001)
print(0.00001)
print(1e15)
print(1e16)
print(6.02214076e23)

Example output:

0.0001
1e-05
1000000000000000.0
1e+16
6.02214076e+23

The exponent is printed with a sign and at least two digits, which is why you see e-05 and e+16, not e-5.

How to format numbers in scientific notation

To force scientific notation in output, use the e presentation type in an f-string or format(). According to the format specification, with a precision p the coefficient has one digit before the decimal point and p digits after it. With no precision, a float gets 6 digits after the point. Use E for an uppercase separator.

value = 1234.5678

print(f"{value:e}")
print(f"{value:.2e}")
print(f"{value:E}")
print(f"{value:g}")
print(f"{0.0000123:g}")

Example output:

1.234568e+03
1.23e+03
1.234568E+03
1234.57
1.23e-05

The g type is the general format. It rounds to a number of significant digits (6 by default for floats) and picks fixed-point or scientific notation by magnitude. For a float, scientific notation is used when the exponent is below -4 or at least the precision.

How to avoid scientific notation

If you want 0.00001 instead of 1e-05, use the fixed-point type f with an explicit number of decimal places.

value = 0.00001

print(value)
print(f"{value:.5f}")
print(f"{value:.10f}")

Example output:

1e-05
0.00001
0.0000100000

This changes only the text. If you pass the number on to a calculation, nothing is lost. If you are writing numbers to a CSV or reading them back, remember that float("1e-05") and float("1E-5") both parse correctly, but some other tools do not accept the exponent form. The larely JSON to CSV converter is useful for checking how a file looks after export.

Reading scientific notation from text

The float() constructor accepts the same exponent syntax as a string, so values like "2.5e3" from a file or an API convert directly.

print(float("2.5e3"))
print(float("1E-3"))
print(int(float("1e3")))

Example output:

2500.0
0.001
1000

int("1e3") raises ValueError: invalid literal for int() with base 10: '1e3', because int() parses integer text only. Convert through float() first if the text may contain an exponent, but be aware that very large values lose integer precision once they pass through a float.

If it still fails

  • NameError: name 'e' is not defined: add import math and write math.e.
  • AttributeError: module 'math' has no attribute 'e': a file named math.py in your project is shadowing the standard library. Rename it and delete its __pycache__ folder.
  • SyntaxError on a number like 1 e3 or 1e 3: remove the spaces. The exponent must be attached to the number.
  • e behaves like a number you did not expect after an except ... as e block: you shadowed math.e. Use math.e with the prefix, and name the exception variable error.
  • A printed value looks wrong, such as 1e-05 where you wanted 0.00001: this is display only. Format it with f"{value:.5f}".

Frequently asked questions

How do I write e in Python?
For Euler's number, run import math and use math.e, which prints as 2.718281828459045. To raise e to a power, call math.exp(x). If you mean the e inside a number such as 1e-5, that is scientific notation and you type it directly in the number.
What does 1e-5 mean in Python?
It means 1 times 10 to the power of -5, which is 0.00001. The Python language reference says the letter e or E in a float literal represents 'times ten raised to the power of'. Python prints this value as 1e-05.
Is the e in 1e3 the same as math.e?
No. The e in 1e3 is just a separator between the number and its power of ten, so 1e3 is 1000.0. math.e is the constant 2.718281828459045. The two are unrelated, and 1e3 does not use Euler's number.
How do I stop Python printing numbers like 1e-05?
Format the number with a fixed-point presentation type, for example f"{value:.5f}", which prints 0.00001. The default print uses scientific notation for very small and very large floats, but the stored value is the same either way.

Sources