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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With