File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Code example from Think Python, by Allen B. Downey.
3+ Available from http://thinkpython.com
4+
5+ Copyright 2013 Allen B. Downey.
6+ Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
7+
8+
9+ If you type an integer with a leading zero, you might get
10+ a confusing error:
11+
12+ >>> zipcode = 02492
13+ ^
14+ SyntaxError: invalid token
15+
16+ Other numbers seem to work, but the results are bizarre:
17+
18+ >>> zipcode = 02132
19+ >>> zipcode
20+ 1114
21+
22+ Can you figure out what is going on? Hint: display the
23+ values 01, 010, 0100 and 01000.
24+
25+ """
26+
27+ print 01 , 010 , 0100 , 01000
28+
29+
30+ """
31+
32+ The result is
33+
34+ 1 8 64 512
35+
36+ which you might recognize as powers of 8.
37+
38+ If a number begins with 0, Python treats it as an octal number,
39+ which means it is in base 8.
40+
41+ So in the example, 02132 is considered
42+
43+ """
44+
45+ print 2 * 512 + 1 * 64 + 3 * 8 + 2
46+
47+ """
48+
49+ which is 1114.
50+
51+ """
52+
You can’t perform that action at this time.
0 commit comments