Level 1
0 / 100 XP

Writing big numbers cleanly

What's easier to read — 1000000 or 1_000_000?

Python lets you write large numbers with underscores as separators. The underscores are ignored when the code runs — they're just there for you.

The problem

Big numbers are hard to read at a glance:

Python
population = 1000000 national_debt = 33000000000000 speed_of_light = 299792458

How many zeros is that? You have to count. That's a bug waiting to happen.

The fix

Python
population = 1_000_000 national_debt = 33_000_000_000_000 speed_of_light = 299_792_458 print(population) # 1000000 print(national_debt) # 33000000000000 print(speed_of_light) # 299792458

Python strips the underscores completely — the values are identical to the versions without them.

It works on floats too

Python
precise_pi = 3.141_592_653 big_float = 1_234_567.89 print(precise_pi) # 3.141592653 print(big_float) # 1234567.89

The underscores can go anywhere

Python doesn't enforce where the underscores go — but grouping by thousands is the convention that makes the most sense to most readers:

Python
# All valid, but some are more readable than others a = 1_000_000 # clear b = 10_0000_0 # valid but confusing — don't do this

Stick to groups of three unless you have a good reason not to.

Key takeaways

  • Use _ to separate digits in large numbers — Python ignores them completely
  • Works on both integers and floats
  • Group by thousands (1_000_000) — it's the convention everyone expects