python - 数字中的下划线是什么意思?

标签 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/

相关文章:

python - 将类型(字符串)转换为 pandas.core.series.Series

python - 如何覆盖 python 中的嵌入函数/定义值

python - 具有意外结果的正弦波的傅里叶变换

python-3.x - 在 QGraphicsView 中禁用鼠标指针

python - 如何在Raspberry Pi中控制音频播放?

Django session cookie 过期日期

python - 使用 Python 访问 JSON API

python - 如何将 Python 列表中的后续数字连接成双(或更多)数字

python - 为什么这个解决方案会失败?嵌套和匹配的括号

python-3.x - 如何处理geopy的查询限制?