为什么 Python 中的 08 或 09 无效? [英] Why is 08 or 09 in Python invalid?

查看:65
本文介绍了为什么 Python 中的 08 或 09 无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 解释器中,0809 似乎无效.示例:

<预><代码>>>>011>>>022>>>033>>>044>>>055>>>066>>>077>>>08文件<stdin>",第 1 行08^语法错误:无效的令牌>>>09文件<stdin>",第 1 行09^语法错误:无效的令牌

如您所见,只有 0809 似乎不起作用.这些是特殊值还是什么?

解决方案

带有前导零的数字被解释为八进制文字.所以 8 和 9 在八进制中是无效的.仅数字 0 到 7 有效.

在解释器中尝试:

<预><代码>>>>0119>>>01210>>>01311

In the Python interpreter, 08 and 09 seem invalid. Example:

>>> 01
1
>>> 02
2
>>> 03
3
>>> 04
4
>>> 05
5
>>> 06
6
>>> 07
7
>>> 08
  File "<stdin>", line 1
    08
     ^
SyntaxError: invalid token
>>> 09
  File "<stdin>", line 1
    09
     ^
SyntaxError: invalid token

As you can see, only 08 and 09 don't seem to work. Are these special values or something?

解决方案

A number with a leading zero is interpreted as octal literal. So 8 and 9 are invalid in octal. Only digits 0 to 7 are valid.

Try in interpreter:

>>> 011
9
>>> 012
10
>>> 013
11

这篇关于为什么 Python 中的 08 或 09 无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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