25

Since I've been learning Python, I've sometimes seen beginner examples that look like this:

sum_sq = [None] * 1_000

I've bought three Python books and none have mentioned what the 1_000 and 100_000 means that I'm seeing in these examples.

My question is: is 1_000 the same as 1,000? And if so, why do they write it as 1_000? Does it have a special function that 1,000 does not? Things like:

if __name__ == __main__ 

have sensible reasons for using the underscore and I can't see a reason why 1_000 is used.

miradulo
  • 28,857
  • 6
  • 80
  • 93
Shaken_not_stirred.
  • 486
  • 1
  • 5
  • 11
  • Strongly related (not sure if duplicate): https://stackoverflow.com/q/43817634/4909087 – cs95 Apr 15 '18 at 01:16
  • 1
    The cool thing about python is that you can open up an interpreter and try things out. So what happens if you do `1000 == 1_000`? – Paul H Apr 15 '18 at 01:28
  • Paul H - yeah I got "true" when I tested your example in Idle, and the example I was trying ran the same regardless of whether I used 1,000 or 1_000. But I'm only using basic code, so I wondered if it made a difference in more complicated examples with greater complexity of time/efficiency. Thanks. – Shaken_not_stirred. Apr 15 '18 at 02:46

2 Answers2

21

These are allowed since Python 3.6, see PEP 515 - Underscores in Numeric Literals. As stated in the PEP

The underscores have no semantic meaning, and literals are parsed as if the underscores were absent.

So they are just available for readability for large numbers, binary literals, hex addresses, etc. Some examples from the PEP:

>>> assert 10_000_000 == 10000000

>>> assert 0xCAFE_F00D == 0xCAFEF00D

>>> assert 0b_0011_1111_0100_1110 == 0b0011111101001110
miradulo
  • 28,857
  • 6
  • 80
  • 93
8

Yes, it's the same as without the underscores. They have underscores there to act as thousand seperators, which for longer numbers can make them easier to read.

It's similar to how in writing we separate thousands with commas (or dots depending on your region):

1,000,000 # A million

Because of how that would interfere with tuple creation though:

n = 1,000,000
print(type(n))

<class 'tuple'> 

Python uses underscores instead of commas.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • 1
    "normal life" ... indeed, developers lead very abnormal lives :p – cs95 Apr 15 '18 at 00:56
  • @cᴏʟᴅsᴘᴇᴇᴅ I sat there for a couple seconds searching for the right word. Whenever I start talking about programming to most people, they look at me like I'm speaking Greek, so "normal" seemed appropriate. – Carcigenicate Apr 15 '18 at 00:58
  • Thank you. I spent some time in Italy and noticed they write 1000 as 1.000 which is very confusing as it looks like 1 Euro instead of a thousand. But I'd never seen 1_000 before. You're right - it would make a very long number easier to read. – Shaken_not_stirred. Apr 15 '18 at 02:51
  • 1
    @Shaken_not_stirred. Np. Ya, the . and , differences can be confusing. In North America (Canada specifically), we only use commas, which I prefer. And just an FYI, you can use scientific notation to write numbers to make them easier to read as well. A million can alternatively be written as `1e6` (1 x 10^6). That way, you don't have to count zeros. – Carcigenicate Apr 15 '18 at 03:02
  • Carcigenicate - thank you. The 1e6 format would definitely be my preference so I'll use that. – Shaken_not_stirred. Apr 15 '18 at 03:25