66

I am wondering why the following variable is treated like a number?

a = 1_000_000
print (a)

1000000

Shouldn't print(a) return 1_000_000?

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
sophros
  • 14,672
  • 11
  • 46
  • 75

3 Answers3

101

With Python 3.6 (and PEP-515) there is a new convenience notation for big numbers introduced which allows you to divide groups of digits in the number literal so that it is easier to read them.

Examples of use:

a = 1_00_00  # you do not need to group digits by 3!
b = 0xbad_c0ffee  # you can make fun with hex digit notation
c = 0b0101_01010101010_0100  # works with binary notation
f = 1_000_00.0
print(a,b,c,f)

10000

50159747054

174756

100000.0

print(int('1_000_000'))
print(int('0xbad_c0ffee', 16))
print(int('0b0101_01010101010_0100',2))
print(float('1_000_00.0'))

1000000

50159747054

174756

100000.0

A = 1__000  # SyntaxError: invalid token
sophros
  • 14,672
  • 11
  • 46
  • 75
  • 33
    can't help but rant a bit about this new "feature"... i had a script that attempted to convert a string into number while on failing simply returned the string itself. this is used for interpreting a mixed-type input data file. '_' was used as a delimiter to combine two integer id's in some fields and all of a sudden (after python 3.6 upgrade), these fields are converted into one integer! IMHO, this sort of warning-less change (silently break perfectly working non-hacky code) should only be allowed in the absolutely necessary scenarios. nicely formatting a large int hardly justifies it. – Kevin S. Aug 12 '19 at 05:32
  • 40
    @KevinS. https://xkcd.com/1172/ – Mike Martin Sep 19 '20 at 00:32
17

Python allows you to put underscores in numbers for convenience. They're used to separate groups of numbers, much like commas do in non-programming. Underscores are completely ignored in numbers, much like comments. So this:

x = 1_000_000

is interpreted to be the same as this:

x = 1000000

However, you can't put two underscores right next to each other like this:

x = 1__000__000 #SyntaxError
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
8

In English speaking countries, commas are generally used as thousand separators, while in many other countries, periods are used as thousand separators. Given the differing conventions, and the fact that both commas and periods are used for other things in Python, it was decided to use underscores as separators.

Acccumulation
  • 3,491
  • 1
  • 8
  • 12
  • 8
    In India, which is English speaking, the separator is not placed at thousands: https://en.wikipedia.org/wiki/Crore – Neil G Jan 09 '19 at 22:37
  • 6
    All the more reason to use the underscores, @NeilG : You can write `10_000`, `10000`, `10_0_00`, `1_0000`, etc. and they are all interpreted as "ten thousand". Essentially, the (singlet) underscore is ignored. – Mike Williamson Jul 05 '21 at 14:11