[英]Why isn't 0-padding allowed in Python?
I've just noticed 0-padding is not allowed in Python and I was wondering why this choice was made? 我刚刚注意到Python中不允许使用0-padding,我想知道为什么选择这个选项?
For example: 例如:
a = 09
doesn't work while 不起作用
a = 9
does 不
How's that?! 怎么样?!
Thank you very much for your answers! 非常感谢您的回答!
Python, as many other languages, treat numbers starting with 0 as being in octal notation. 与许多其他语言一样,Python将以0开头的数字视为八进制表示法。 09 is not valid as octal
09无效为八进制
See chapter 2.4.4 in the python language reference. 请参阅python语言参考中的第2.4.4节 。
To expand on what @nos said: 扩展@nos所说的内容:
>>> a = 01
>>> a
1
>>> a = 07
>>> a
7
>>> a = 010
>>> a
8
>>> a = 08
File "<stdin>", line 1
a = 08
^
SyntaxError: invalid token
So, a = 010
is octal 10
, which is decimal 8. Octal only knows digits 0 through 7. So that is why this isn't possible. 因此,
a = 010
是八进制10
,即十进制8.八进制只知道数字0到7.所以这就是为什么这是不可能的。
It's because numbers starting with 0 are octal numbers, and octal figures go from 0 to 7! 这是因为从0开始的数字是八进制数字,八进制数字从0到7!
Doing a = 07
is perfectly accepted, as an octal; 做
a = 07
完全被接受为八进制; so a = 061
will lead a
to contain 49. 所以
a = 061
将导致a
包含49。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.