Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do 1_000 and 100_000 mean? [duplicate]

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.

like image 784
Shaken_not_stirred. Avatar asked Apr 15 '18 00:04

Shaken_not_stirred.


1 Answers

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
like image 73
miradulo Avatar answered Oct 14 '22 17:10

miradulo