<分区>
我想知道为什么以下变量被视为数字?
a = 1_000_000
print (a)
1000000
print(a)
不应该返回 1_000_000
吗?
标签 python python-3.x
<分区>
我想知道为什么以下变量被视为数字?
a = 1_000_000
print (a)
1000000
print(a)
不应该返回 1_000_000
吗?
最佳答案
在 Python 3.6(和 PEP-515)中,引入了一种新的大数字便利符号,它允许您在数字文字中划分数字组,以便更容易阅读。
使用示例:
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
关于python - 数字中的下划线是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54009778/