python-2.7 - 以 0 开头的数字上的 int() 有什么作用?

标签 python-2.7 int

下面两行都会打印出12:

print int(12)
print int("012")

为什么下面打印出 10?

print int(012)

最佳答案

这是一个 octal (base 8)数字字面量,它在 Python 2 中原生支持(尽管令人困惑)以及十六进制(例如 0x1C)。但是,默认情况下,所有数字都以十进制打印,因此您得到返回给您的整数的十进制(以 10 为基数)版本:

(1 * 8) + (2 * 1) = 10

Python 2 输出

$ python2 -c "print 012"
10

python 3

备注 Python 3 this has changed :

$ python3 -c "print(010)"
  File "<string>", line 1
    print(010)
            ^
SyntaxError: invalid token

关于python-2.7 - 以 0 开头的数字上的 int() 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35637832/

相关文章:

python - time.mktime 不接受 1900 年之前的年份?

python - 如何将分类数据更改为二进制数据?

android - 使用 getCurrentTextColor() 设置文本颜色

python - 整数列表到字符串

java - 如何获取一个整数以将值设置为与扫描仪相同

python - 整数不转换为字符串不转换为字符串(python)

python - 类型错误:不支持的操作数类型 - : 'int' 和 'list'

python - 代理无法通过 SSL 连接工作

python - 如何将 Pandas 数据框的多行标题合并到单个单元格标题中?

python - 替代 python 的 .sort() (用于插入大列表并保持排序)