forked from AllenDowney/ThinkPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise-2.1.py
More file actions
54 lines (31 loc) · 896 Bytes
/
exercise-2.1.py
File metadata and controls
54 lines (31 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Code example from Think Python, by Allen B. Downey.
Available from http://thinkpython.com
Copyright 2013 Allen B. Downey.
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
If you type an integer with a leading zero, you might get
a confusing error:
>>> zipcode = 02492
^
SyntaxError: invalid token
Other numbers seem to work, but the results are bizarre:
>>> zipcode = 02132
>>> zipcode
1114
Can you figure out what is going on? Hint: display the
values 01, 010, 0100 and 01000.
"""
print 01, 010, 0100, 01000
"""
The result is
1 8 64 512
which you might recognize as powers of 8.
If a number begins with 0, Python treats it as an octal number,
which means it is in base 8.
So in the example, 02132 is considered
"""
print 2 * 512 + 1 * 64 + 3 * 8 + 2
"""
which is 1114.
In Python 3, this "feature" has been removed.
"""