书写很大的数时,可使用下划线将其中的数字分组,使其更清晰易读.
当你打印这种使用下划线定义的数字时,Python不会打印其中的下划线.
这是因为存储这种数时,Python会忽略其中的下划线.
将数字分组时,即使不是将每三位分成一组,也不会影响最终的值.
在Python看来,1000和1_000没有什么不同,1_000和10_00也没什么不同.
这种表示法适用于整数和浮点数,但只有Python3.6和更高的版本支持.
示例:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> a = 1_000_000
>>> a
1000000
>>>
>>> a = 3_00_0_2
>>> a
30002
>>>
>>> type(a)
<class 'int'>
>>>
>>>
>>> a = 3.68_796
>>> a
3.68796
>>> type(a)
<class 'float'>
>>>
>>> a = 3_.689
SyntaxError: invalid token
>>>
>>> a = 3.698_68_55
>>> a
3.6986855
>>> type(a)
<class 'float'>
>>>
>>>
>>>