What does underscore in a number mean? [duplicate]

  • A+
Category:Languages

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?

 


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 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: