我是python的新手,我在python shell上输入时使用的是python 2.7:
print 01
print 010
print 0100
print 01000
Run Code Online (Sandbox Code Playgroud)
它给出了这个结果
1
8
64
512
Run Code Online (Sandbox Code Playgroud)
我试图理解为什么它给了这个但不幸的是我没有明白这一点.
Doo*_*nob 22
如果一个数字开头0,它被解释为八进制或基数8.只需:
print 1
print 10
print 100
print 1000
Run Code Online (Sandbox Code Playgroud)
而你的问题将得到解决.
有关八进制的更多信息:http://en.wikipedia.org/wiki/Octal
这是一种更容易理解八进制的方法:
八进制1是十进制(正常数字)1
八进制2:小数2
...
八进制7:十进制7
八进制10:十进制8
八进制11:十进制9
八进制12:十进制10
...
八进制17:十进制15
八进制20:十进制16
等等.Octal只使用0到7之间的数字.
希望这有帮助!
Chr*_*our 10
蟒解释一个数开始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
Run Code Online (Sandbox Code Playgroud)
在任何基数中,符号1始终是decimal值,1因为b^0 === 1对于所有人来说b,从右到左阅读数字的索引始于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
Run Code Online (Sandbox Code Playgroud)
一旦基地被解释(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
Run Code Online (Sandbox Code Playgroud)
可以用数字表示的最大值是位数(b^n)-1在哪里n.给定一个3位数最大十进制值是(10^3)-1 = 999,以八进制(8^3)-1 = 511 (decimal)其是777在基座8和二进制(2^3)-1 = 7 (decimal)其是111在基体2.因此可以看到,用更少的符号(下底)可以代表降低的值给定的固定数量的数字.