Writing numeric values

Lesson 22 of 54 · Python Foundations

Long numeric literals are easier to read with underscores between groups of digits. They do not change the value. A comma has a different meaning in Python; it separates items rather than grouping digits in a number.

visitors = 1_250_000
print(visitors)
print(1.25e3)
print(6.4e-2)
1250000
1250.0
0.064

The e notation expresses a value multiplied by a power of ten: 1.25e3 means 1.25 × 10³; 6.4e-2 means 6.4 × 10⁻². The exponent must be an integer. Either lowercase e or uppercase E works. These literals are floats, including 1e3, and are subject to floating-point approximation. Use an integer literal when an exact whole-number count matters.

Chapter index