gpt4 book ai didi

python - 如何为 Python 整数文字使用数字分隔符?

转载 作者:IT老高 更新时间:2023-10-28 22:21:42 25 4
gpt4 key购买 nike

有没有办法在 Python 代码中对数字进行分组以提高代码的易读性?我试过 '_digit separators其他一些语言,但无济于事。

一个奇怪的操作符将它的左手边和右手边连接起来也可以解决。

最佳答案

几年后更新:Python 3.6 现在支持 PEP515 ,因此您可以使用 _ 来提高浮点和整数字面量的可读性。

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1_1000
11000
>>>

对于历史引用,您可以查看严格定义的词法分析python2.7 , python3.5 ...

对于 python3.6.0a2 及更早版本,您应该会收到类似于以下内容的错误消息:

Python 3.6.0a2 (v3.6.0a2:378893423552, Jun 13 2016, 14:44:21) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1_000
File "<stdin>", line 1
1_000
^
SyntaxError: invalid syntax
>>> amount = 10_000_000.0
File "<stdin>", line 1
amount = 10_000_000.0
^
SyntaxError: invalid syntax

关于python - 如何为 Python 整数文字使用数字分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38155177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com