Code is more often read than it is written, and reading a big/long number within other text can be a challenge. Here are two approaches to making numbers more readable:
-
Using underscores in Numeric Literals.
1_000_000
is more readable than1000000
, and10_100_201_330
is easier to scan than10100201330
. For more information, see PEP-0515. -
Using exponential notation or scientific notation. The e (or E) character followed by an integer represents the power of 10 by which the number preceding the e should be multiplied (ie:
1e6
, 1 is multiplied by 10 raised to the power of 6, which equals1000000
). For more details, check out this reference on scientific notation.
This exercise explores objects from Python's datetime
module: