数字中的下划线是什么意思? [英] What do underscores in a number mean?

查看:146
本文介绍了数字中的下划线是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么以下变量被视为数字?

a = 1_000_000打印(一)

<块引用>

1000000

不应该print(a)返回1_000_000吗?

解决方案

使用 Python 3.6(和 PEP-515) 引入了一种新的大数字方便表示法,它允许您将数字文字中的数字分组,以便更容易阅读.

使用示例:

a = 1_00_00 # 你不需要将数字按 3 分组!b = 0xbad_c0ffee # 你可以用十六进制数字符号取笑c = 0b0101_01010101010_0100 # 使用二进制表示法f = 1_000_00.0打印(a,b,c,f)

<块引用>

10000

50159747054

174756

100000.0

print(int('1_000_000'))打印(整数('0xbad_c0ffee',16))打印(整数('0b0101_01010101010_0100',2))打印(浮动('1_000_00.0'))

<块引用>

1000000

50159747054

174756

100000.0

A = 1__000 # SyntaxError: invalid token

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

这篇关于数字中的下划线是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆