Python 将以0 开头的数字解释为octal,即base 8。您可以使用二进制字符串10 作为b^1 === b 计算基数,其中b 是基数。
# print the decimal value of the binary number 10
>>> print 0b10
2
# print the decimal value of the octal number 10
>>> print 010
8
# print the decimal value of the hexadecimal number 10
>>> print 0x10
16
在任何基础上,符号1 始终是decimal 值1,因为对于所有b,b^0 === 1 从右到左读取数字的索引从0 开始。
# print the decimal value of the binary number 1
>>> print 0b001
1
# print the decimal value of the octal number 1
>>> print 0001
1
# print the decimal value of the hexadecimal number 1
>>> print 0x001
1
一旦基数被解释 (0,0b,0x),前导 0 就不重要了。
基础所需的符号数为b,其中最大的符号等于b-1
Base (b) Number of Symbols (b) Symbols (0 : b-1)
Binary 2 2 0,1
Octal 8 8 0,1,2,3,4,5,7,6,7
Decimal 10 10 0,1,2,3,4,5,7,6,7,8,9
一个数字可以表示的最大值是(b^n)-1,其中n是位数。给定一个 3 位数字,最大十进制值为 (10^3)-1 = 999,八进制 (8^3)-1 = 511 (decimal) 是基数为 8 的 777,二进制 (2^3)-1 = 7 (decimal) 是基数为 2 的 111。所以你可以用更少的符号看到(较低的基数)在给定固定位数的情况下,您可以表示的值会减少。