Skip to content

Commit c5a7dc3

Browse files
committed
Solutions
1 parent e425c34 commit c5a7dc3

File tree

294 files changed

+691
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

294 files changed

+691
-0
lines changed

CH2/Checkpoints CH02/Checkpoints

File renamed without changes.

CH2/EX2.1.py CH02/EX2.1.py

File renamed without changes.

CH2/EX2.10.py CH02/EX2.10.py

File renamed without changes.

CH2/EX2.11.py CH02/EX2.11.py

File renamed without changes.

CH2/EX2.12.py CH02/EX2.12.py

File renamed without changes.

CH2/EX2.13.py CH02/EX2.13.py

File renamed without changes.

CH2/EX2.14.py CH02/EX2.14.py

File renamed without changes.

CH2/EX2.15.py CH02/EX2.15.py

File renamed without changes.

CH2/EX2.16.py CH02/EX2.16.py

File renamed without changes.

CH2/EX2.17.py CH02/EX2.17.py

File renamed without changes.

CH2/EX2.18.py CH02/EX2.18.py

File renamed without changes.

CH2/EX2.19.py CH02/EX2.19.py

File renamed without changes.

CH2/EX2.2.py CH02/EX2.2.py

File renamed without changes.

CH2/EX2.20.py CH02/EX2.20.py

File renamed without changes.

CH2/EX2.21.py CH02/EX2.21.py

File renamed without changes.

CH2/EX2.22.py CH02/EX2.22.py

File renamed without changes.

CH2/EX2.23.py CH02/EX2.23.py

File renamed without changes.

CH2/EX2.24.py CH02/EX2.24.py

File renamed without changes.

CH2/EX2.25.py CH02/EX2.25.py

File renamed without changes.

CH2/EX2.26.py CH02/EX2.26.py

File renamed without changes.

CH2/EX2.3.py CH02/EX2.3.py

File renamed without changes.

CH2/EX2.4.py CH02/EX2.4.py

File renamed without changes.

CH2/EX2.5.py CH02/EX2.5.py

File renamed without changes.

CH2/EX2.6.py CH02/EX2.6.py

File renamed without changes.

CH2/EX2.7.py CH02/EX2.7.py

File renamed without changes.

CH2/EX2.8.py CH02/EX2.8.py

File renamed without changes.

CH2/EX2.9.py CH02/EX2.9.py

File renamed without changes.

CH3/Checkpoints CH03/Checkpoints

File renamed without changes.

CH3/EX3.1.py CH03/EX3.1.py

File renamed without changes.

CH3/EX3.10.py CH03/EX3.10.py

File renamed without changes.

CH3/EX3.11.py CH03/EX3.11.py

File renamed without changes.

CH3/EX3.12.py CH03/EX3.12.py

File renamed without changes.

CH3/EX3.13.py CH03/EX3.13.py

File renamed without changes.

CH3/EX3.14.py CH03/EX3.14.py

File renamed without changes.

CH3/EX3.15.py CH03/EX3.15.py

File renamed without changes.

CH3/EX3.16.py CH03/EX3.16.py

File renamed without changes.

CH3/EX3.17.py CH03/EX3.17.py

File renamed without changes.

CH3/EX3.18.py CH03/EX3.18.py

File renamed without changes.

CH3/EX3.19.py CH03/EX3.19.py

File renamed without changes.

CH3/EX3.2.py CH03/EX3.2.py

File renamed without changes.

CH3/EX3.3.py CH03/EX3.3.py

File renamed without changes.

CH3/EX3.4.py CH03/EX3.4.py

File renamed without changes.

CH3/EX3.5.py CH03/EX3.5.py

File renamed without changes.

CH3/EX3.6.py CH03/EX3.6.py

File renamed without changes.

CH3/EX3.7.py CH03/EX3.7.py

File renamed without changes.

CH3/EX3.8.py CH03/EX3.8.py

File renamed without changes.

CH3/EX3.9.py CH03/EX3.9.py

File renamed without changes.

CH4/Checkpoints CH04/Checkpoints

File renamed without changes.

CH4/EX4.1.py CH04/EX4.1.py

File renamed without changes.

CH4/EX4.10.py CH04/EX4.10.py

File renamed without changes.

CH4/EX4.11.py CH04/EX4.11.py

File renamed without changes.

CH4/EX4.12.py CH04/EX4.12.py

File renamed without changes.

CH4/EX4.13.py CH04/EX4.13.py

File renamed without changes.

CH4/EX4.14.py CH04/EX4.14.py

File renamed without changes.

CH4/EX4.15.py CH04/EX4.15.py

File renamed without changes.

CH4/EX4.16.py CH04/EX4.16.py

File renamed without changes.

CH4/EX4.17.py CH04/EX4.17.py

File renamed without changes.

CH4/EX4.18.py CH04/EX4.18.py

File renamed without changes.

CH4/EX4.19.py CH04/EX4.19.py

File renamed without changes.

CH4/EX4.2.py CH04/EX4.2.py

File renamed without changes.

CH4/EX4.20.py CH04/EX4.20.py

File renamed without changes.

CH4/EX4.21.py CH04/EX4.21.py

File renamed without changes.

CH4/EX4.22.py CH04/EX4.22.py

File renamed without changes.

CH4/EX4.23.py CH04/EX4.23.py

File renamed without changes.

CH4/EX4.24.py CH04/EX4.24.py

File renamed without changes.

CH4/EX4.25.py CH04/EX4.25.py

File renamed without changes.

CH4/EX4.26.py CH04/EX4.26.py

File renamed without changes.

CH4/EX4.27.py CH04/EX4.27.py

File renamed without changes.

CH4/EX4.28.py CH04/EX4.28.py

File renamed without changes.

CH4/EX4.29.py CH04/EX4.29.py

File renamed without changes.

CH4/EX4.3.py CH04/EX4.3.py

File renamed without changes.

CH4/EX4.30.py CH04/EX4.30.py

File renamed without changes.

CH4/EX4.31.py CH04/EX4.31.py

File renamed without changes.

CH4/EX4.32.py CH04/EX4.32.py

File renamed without changes.

CH4/EX4.33.py CH04/EX4.33.py

File renamed without changes.

CH4/EX4.34.py CH04/EX4.34.py

File renamed without changes.

CH4/EX4.35.py CH04/EX4.35.py

File renamed without changes.

CH4/EX4.36.py CH04/EX4.36.py

File renamed without changes.

CH4/EX4.37.py CH04/EX4.37.py

File renamed without changes.

CH4/EX4.38.py CH04/EX4.38.py

File renamed without changes.

CH4/EX4.39.py CH04/EX4.39.py

File renamed without changes.

CH4/EX4.4.py CH04/EX4.4.py

File renamed without changes.

CH4/EX4.5.py CH04/EX4.5.py

File renamed without changes.

CH4/EX4.6.py CH04/EX4.6.py

File renamed without changes.

CH4/EX4.7.py CH04/EX4.7.py

File renamed without changes.

CH4/EX4.8.py CH04/EX4.8.py

File renamed without changes.

CH4/EX4.9.py CH04/EX4.9.py

File renamed without changes.

CH5/Checkpoints CH05/Checkpoints

File renamed without changes.

CH5/EX5.1.py CH05/EX5.1.py

File renamed without changes.

CH5/EX5.10.py CH05/EX5.10.py

File renamed without changes.

CH5/EX5.11.py CH05/EX5.11.py

File renamed without changes.

CH5/EX5.12.py CH05/EX5.12.py

File renamed without changes.

CH5/EX5.13.py CH05/EX5.13.py

File renamed without changes.

CH5/EX5.14.py CH05/EX5.14.py

File renamed without changes.

CH5/EX5.15.py CH05/EX5.15.py

File renamed without changes.

CH5/EX5.16.py CH05/EX5.16.py

File renamed without changes.

CH5/EX5.17.py CH05/EX5.17.py

File renamed without changes.

CH5/EX5.18.py CH05/EX5.18.py

File renamed without changes.

CH5/EX5.19.py CH05/EX5.19.py

File renamed without changes.

CH5/EX5.2.py CH05/EX5.2.py

File renamed without changes.

CH5/EX5.20.py CH05/EX5.20.py

File renamed without changes.

CH5/EX5.21.py CH05/EX5.21.py

File renamed without changes.

CH5/EX5.22.py CH05/EX5.22.py

File renamed without changes.

CH5/EX5.23.py CH05/EX5.23.py

File renamed without changes.

CH5/EX5.24.py CH05/EX5.24.py

File renamed without changes.

CH5/EX5.25.py CH05/EX5.25.py

File renamed without changes.

CH5/EX5.26.py CH05/EX5.26.py

File renamed without changes.

CH5/EX5.27.py CH05/EX5.27.py

File renamed without changes.

CH5/EX5.28.py CH05/EX5.28.py

File renamed without changes.

CH5/EX5.29.py CH05/EX5.29.py

File renamed without changes.

CH5/EX5.3.py CH05/EX5.3.py

File renamed without changes.

CH5/EX5.30.py CH05/EX5.30.py

File renamed without changes.

CH5/EX5.31.py CH05/EX5.31.py

File renamed without changes.

CH5/EX5.32.py CH05/EX5.32.py

File renamed without changes.

CH5/EX5.33.py CH05/EX5.33.py

File renamed without changes.

CH5/EX5.34.py CH05/EX5.34.py

File renamed without changes.

CH5/EX5.35.py CH05/EX5.35.py

File renamed without changes.

CH5/EX5.36.py CH05/EX5.36.py

File renamed without changes.

CH5/EX5.37.py CH05/EX5.37.py

File renamed without changes.

CH5/EX5.38.py CH05/EX5.38.py

File renamed without changes.

CH5/EX5.39.py CH05/EX5.39.py

File renamed without changes.

CH5/EX5.4.py CH05/EX5.4.py

File renamed without changes.

CH5/EX5.40.py CH05/EX5.40.py

File renamed without changes.

CH5/EX5.41.py CH05/EX5.41.py

File renamed without changes.

CH5/EX5.42.py CH05/EX5.42.py

File renamed without changes.

CH5/EX5.43.py CH05/EX5.43.py

File renamed without changes.

CH5/EX5.44.py CH05/EX5.44.py

File renamed without changes.

CH5/EX5.45.py CH05/EX5.45.py

File renamed without changes.

CH5/EX5.46.py CH05/EX5.46.py

File renamed without changes.

CH5/EX5.47.py CH05/EX5.47.py

File renamed without changes.

CH5/EX5.48.py CH05/EX5.48.py

File renamed without changes.

CH5/EX5.49.py CH05/EX5.49.py

File renamed without changes.

CH5/EX5.5.py CH05/EX5.5.py

File renamed without changes.

CH5/EX5.50.py CH05/EX5.50.py

File renamed without changes.

CH5/EX5.51.py CH05/EX5.51.py

File renamed without changes.

CH5/EX5.52.py CH05/EX5.52.py

File renamed without changes.

CH5/EX5.53.py CH05/EX5.53.py

File renamed without changes.

CH5/EX5.54.py CH05/EX5.54.py

File renamed without changes.

CH5/EX5.55.py CH05/EX5.55.py

File renamed without changes.

CH5/EX5.6.py CH05/EX5.6.py

File renamed without changes.

CH5/EX5.7.py CH05/EX5.7.py

File renamed without changes.

CH5/EX5.8.py CH05/EX5.8.py

File renamed without changes.

CH5/EX5.9.py CH05/EX5.9.py

File renamed without changes.
File renamed without changes.

CH6/Checkpoints CH06/Checkpoints

File renamed without changes.

CH6/EX6.1.py CH06/EX6.1.py

File renamed without changes.

CH6/EX6.10.py CH06/EX6.10.py

File renamed without changes.

CH6/EX6.11.py CH06/EX6.11.py

File renamed without changes.

CH6/EX6.12.py CH06/EX6.12.py

File renamed without changes.

CH6/EX6.13.py CH06/EX6.13.py

File renamed without changes.

CH6/EX6.14.py CH06/EX6.14.py

File renamed without changes.

CH6/EX6.15.py CH06/EX6.15.py

File renamed without changes.

CH6/EX6.16.py CH06/EX6.16.py

File renamed without changes.

CH6/EX6.17.py CH06/EX6.17.py

File renamed without changes.

CH6/EX6.18.py CH06/EX6.18.py

File renamed without changes.

CH6/EX6.19.py CH06/EX6.19.py

File renamed without changes.

CH6/EX6.2.py CH06/EX6.2.py

File renamed without changes.

CH6/EX6.20.py CH06/EX6.20.py

File renamed without changes.

CH6/EX6.21.py CH06/EX6.21.py

File renamed without changes.

CH6/EX6.22.py CH06/EX6.22.py

File renamed without changes.

CH6/EX6.23.py CH06/EX6.23.py

File renamed without changes.

CH6/EX6.24.py CH06/EX6.24.py

File renamed without changes.

CH6/EX6.25.py CH06/EX6.25.py

File renamed without changes.

CH6/EX6.26.py CH06/EX6.26.py

File renamed without changes.

CH6/EX6.27.py CH06/EX6.27.py

File renamed without changes.

CH6/EX6.28.py CH06/EX6.28.py

File renamed without changes.

CH6/EX6.29.py CH06/EX6.29.py

File renamed without changes.

CH6/EX6.3.py CH06/EX6.3.py

File renamed without changes.

CH6/EX6.30.py CH06/EX6.30.py

File renamed without changes.

CH6/EX6.31.py CH06/EX6.31.py

File renamed without changes.

CH6/EX6.32.py CH06/EX6.32.py

File renamed without changes.

CH6/EX6.33.py CH06/EX6.33.py

File renamed without changes.

CH6/EX6.34.py CH06/EX6.34.py

File renamed without changes.

CH6/EX6.35.py CH06/EX6.35.py

File renamed without changes.

CH6/EX6.36.py CH06/EX6.36.py

File renamed without changes.

CH6/EX6.37.py CH06/EX6.37.py

File renamed without changes.

CH6/EX6.38.py CH06/EX6.38.py

File renamed without changes.

CH6/EX6.39.py CH06/EX6.39.py

File renamed without changes.

CH6/EX6.4.py CH06/EX6.4.py

File renamed without changes.

CH6/EX6.40.py CH06/EX6.40.py

File renamed without changes.

CH6/EX6.41.py CH06/EX6.41.py

File renamed without changes.

CH6/EX6.42.py CH06/EX6.42.py

File renamed without changes.

CH6/EX6.43.py CH06/EX6.43.py

File renamed without changes.

CH6/EX6.44.py CH06/EX6.44.py

File renamed without changes.

CH6/EX6.45.py CH06/EX6.45.py

File renamed without changes.

CH6/EX6.46.py CH06/EX6.46.py

File renamed without changes.

CH6/EX6.47.py CH06/EX6.47.py

File renamed without changes.

CH6/EX6.48.py CH06/EX6.48.py

File renamed without changes.

CH6/EX6.5.py CH06/EX6.5.py

File renamed without changes.

CH6/EX6.6.py CH06/EX6.6.py

File renamed without changes.

CH6/EX6.7.py CH06/EX6.7.py

File renamed without changes.

CH6/EX6.8.py CH06/EX6.8.py

File renamed without changes.

CH6/EX6.9.py CH06/EX6.9.py

File renamed without changes.

CH7/Account.py CH07/Account.py

File renamed without changes.

CH7/Checkpoints CH07/Checkpoints

File renamed without changes.

CH7/EX7.1.py CH07/EX7.1.py

File renamed without changes.

CH7/EX7.10.py CH07/EX7.10.py

File renamed without changes.

CH7/EX7.2.py CH07/EX7.2.py

File renamed without changes.

CH7/EX7.3.py CH07/EX7.3.py

File renamed without changes.

CH7/EX7.4.py CH07/EX7.4.py

File renamed without changes.

CH7/EX7.5.py CH07/EX7.5.py

File renamed without changes.

CH7/EX7.6.py CH07/EX7.6.py

File renamed without changes.

CH7/EX7.7.py CH07/EX7.7.py

File renamed without changes.

CH7/EX7.8.py CH07/EX7.8.py

File renamed without changes.

CH7/EX7.9.py CH07/EX7.9.py

File renamed without changes.

CH7/Fan.py CH07/Fan.py

File renamed without changes.
File renamed without changes.
File renamed without changes.

CH7/Rectangle.py CH07/Rectangle.py

File renamed without changes.
File renamed without changes.

CH7/Stock.py CH07/Stock.py

File renamed without changes.

CH7/StopWatch.py CH07/StopWatch.py

File renamed without changes.

CH7/Time.py CH07/Time.py

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

CH8/Checkpoints CH08/Checkpoints

File renamed without changes.

CH8/EX8.1.py CH08/EX8.1.py

File renamed without changes.

CH8/EX8.10.py CH08/EX8.10.py

File renamed without changes.

CH8/EX8.11.py CH08/EX8.11.py

File renamed without changes.

CH8/EX8.12.py CH08/EX8.12.py

File renamed without changes.

CH8/EX8.13.py CH08/EX8.13.py

File renamed without changes.

CH8/EX8.14.py CH08/EX8.14.py

File renamed without changes.

CH8/EX8.15.py CH08/EX8.15.py

File renamed without changes.

CH8/EX8.16.py CH08/EX8.16.py

File renamed without changes.

CH8/EX8.17.py CH08/EX8.17.py

File renamed without changes.

CH8/EX8.18.py CH08/EX8.18.py

File renamed without changes.

CH8/EX8.19.py CH08/EX8.19.py

File renamed without changes.

CH8/EX8.2.py CH08/EX8.2.py

File renamed without changes.

CH8/EX8.20.py CH08/EX8.20.py

File renamed without changes.

CH8/EX8.21.py CH08/EX8.21.py

File renamed without changes.

CH8/EX8.3.py CH08/EX8.3.py

File renamed without changes.

CH8/EX8.4.py CH08/EX8.4.py

File renamed without changes.

CH8/EX8.5.py CH08/EX8.5.py

File renamed without changes.

CH8/EX8.6.py CH08/EX8.6.py

File renamed without changes.

CH8/EX8.7.py CH08/EX8.7.py

File renamed without changes.

CH8/EX8.8.py CH08/EX8.8.py

File renamed without changes.

CH8/EX8.9.py CH08/EX8.9.py

File renamed without changes.

CH9/Checkpoints CH09/Checkpoints

File renamed without changes.

CH9/EX9.1.py CH09/EX9.1.py

File renamed without changes.

CH9/EX9.10.py CH09/EX9.10.py

File renamed without changes.

CH9/EX9.11.py CH09/EX9.11.py

File renamed without changes.

CH9/EX9.12.py CH09/EX9.12.py

File renamed without changes.

CH9/EX9.13.py CH09/EX9.13.py

File renamed without changes.

CH9/EX9.14.py CH09/EX9.14.py

File renamed without changes.

CH9/EX9.15.py CH09/EX9.15.py

File renamed without changes.

CH9/EX9.16.py CH09/EX9.16.py

File renamed without changes.

CH9/EX9.17.py CH09/EX9.17.py

File renamed without changes.

CH9/EX9.18.py CH09/EX9.18.py

File renamed without changes.

CH9/EX9.19.py CH09/EX9.19.py

File renamed without changes.

CH9/EX9.2.py CH09/EX9.2.py

File renamed without changes.

CH9/EX9.20.py CH09/EX9.20.py

File renamed without changes.

CH9/EX9.21.py CH09/EX9.21.py

File renamed without changes.

CH9/EX9.22.py CH09/EX9.22.py

File renamed without changes.

CH9/EX9.23.py CH09/EX9.23.py

File renamed without changes.

CH9/EX9.24.py CH09/EX9.24.py

File renamed without changes.

CH9/EX9.25.py CH09/EX9.25.py

File renamed without changes.

CH9/EX9.26.py CH09/EX9.26.py

File renamed without changes.

CH9/EX9.27.py CH09/EX9.27.py

File renamed without changes.

CH9/EX9.28.py CH09/EX9.28.py

File renamed without changes.

CH9/EX9.29.py CH09/EX9.29.py

File renamed without changes.

CH9/EX9.3.py CH09/EX9.3.py

File renamed without changes.

CH9/EX9.30.py CH09/EX9.30.py

File renamed without changes.

CH9/EX9.31.py CH09/EX9.31.py

File renamed without changes.

CH9/EX9.32.py CH09/EX9.32.py

File renamed without changes.

CH9/EX9.33.py CH09/EX9.33.py

File renamed without changes.

CH9/EX9.34.py CH09/EX9.34.py

File renamed without changes.

CH9/EX9.4.py CH09/EX9.4.py

File renamed without changes.

CH9/EX9.5.py CH09/EX9.5.py

File renamed without changes.

CH9/EX9.6.py CH09/EX9.6.py

File renamed without changes.

CH9/EX9.7.py CH09/EX9.7.py

File renamed without changes.

CH9/EX9.8.py CH09/EX9.8.py

File renamed without changes.

CH9/EX9.9.py CH09/EX9.9.py

File renamed without changes.

CH9/car.gif CH09/car.gif

File renamed without changes.

CH9/o.gif CH09/o.gif

File renamed without changes.

CH9/x.gif CH09/x.gif

File renamed without changes.

CH14/Checkpoints

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#14.1
2+
Tuples are like lists, but their elements are fixed; that is, once a tuple is created, you
3+
cannot add new elements, delete elements, replace elements, or reorder the elements in
4+
the tuple.
5+
To create a tuple from a list: t = tuple(lst)
6+
To create a list from a tuple: lst = list(tpl)
7+
8+
#14.2
9+
t is a tuple, which its elements are fixed; we
10+
cannot used append and remove functions or change its content.
11+
12+
#14.3
13+
Yes. Now t1 and t2 refer to the same tuple.
14+
15+
#14.4
16+
(1, 2, 3, 7, 9, 0, 5)
17+
1
18+
(2, 3)
19+
5
20+
(1, 2, 3, 7, 9, 0)
21+
(2, 3, 7, 9, 0)
22+
23+
#14.5
24+
9
25+
0
26+
27
27+
7
28+
29+
#14.6
30+
False
31+
True
32+
False
33+
True
34+
35+
#14.7
36+
st = set() or st = {}
37+
38+
#14.8
39+
Yes.
40+
41+
#14.9
42+
s = {1, 3, 4} # Correct
43+
s = {{1, 2}, {4, 5}} # Incorrect, because unhashable set
44+
s = {[1, 2], [4, 5]} # Incorrect, because unhashable list
45+
s = {(1, 2), (4, 5)} # Correct (1, 2), (4, 5) are tuples. They are hashable
46+
47+
#14.10
48+
Sets are like lists to store a collection of items. Unlike lists,
49+
the elements in a set are unique and are not placed in any particular ordered.
50+
To create a set from a list, use set(list). To create a list from a set, use list(set).
51+
52+
#14.11
53+
{'peter', 'john'}
54+
{'peter', 'john'}
55+
{'peter', 'peterson', 'john'}
56+
{'peterson', 'john'}
57+
58+
#14.12
59+
Yes. It will throw an exception.
60+
61+
#14.13
62+
True
63+
False
64+
False
65+
False
66+
True
67+
True
68+
69+
#14.14
70+
4
71+
6
72+
1
73+
16
74+
75+
#14.15
76+
{1, 3, 4, 5, 6, 7}
77+
{1, 3, 4, 5, 6, 7}
78+
{1, 6}
79+
{1, 6}
80+
{4, 5}
81+
{4, 5}
82+
{3, 4, 5, 7}
83+
{3, 4, 5, 7}
84+
85+
#14.16
86+
False
87+
True
88+
4
89+
11
90+
2
91+
23
92+
True
93+
True
94+
95+
#14.17
96+
{1, 2, 3} {3, 4, 5} {1, 2, 3, 4, 5}
97+
{1, 2, 3} {3, 4, 5} {1, 2}
98+
{1, 2, 3} {3, 4, 5} {3}
99+
{1, 2, 3} {3, 4, 5} {1, 2, 4, 5}
100+
101+
#14.18
102+
dict = {}
103+
104+
#14.19
105+
d = {1:[1, 2], 3:[3, 4]}
106+
d = {1:"john", 3:"peter"}
107+
d = {"john":1, "peter":3}
108+
109+
#14.20
110+
The two parts of a dictionary are the key and its corresponding value.
111+
112+
#14.21
113+
(a) Adds a new item "susan":5
114+
(b) Updates the value of the key "peter" to 5
115+
(c) Adding 5 to the existing value of key "peter"
116+
(d) Delete the item with key "peter"
117+
118+
#14.22
119+
(a) Prints the length of the dictionary, 2
120+
(b) Prints the keys of the dictionary, "john" and "peter"
121+
(c) Prints the values of the dictionary, 3 and 2
122+
(d) Prints the key/value pairs of the dictionary, {"john":3, "peter":2}
123+
124+
#14.23
125+
4
126+
['red', 'blue', 'green', 'yellow']
127+
[4, 1, 14, 2]
128+
True
129+
False
130+
11
131+
132+
#14.24
133+
5
134+
135+
#14.25
136+
In case of the key does not exist in the d,
137+
d[key] raises an exception while d.get(key) returns None.
138+
139+
140+

CH14/EX14.01.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 14.1 (Display keywords) Revise Listing 14.4 CountKeywords.py to display the keywords
2+
# in a Python source file as well as to count the number of the keywords.
3+
4+
import os.path
5+
import sys
6+
7+
8+
def main():
9+
keyWords = {"and": 0, "as": 0, "assert": 0, "break": 0, "class": 0,
10+
"continue": 0, "def": 0, "del": 0, "elif": 0, "else": 0,
11+
"except": 0, "False": 0, "finally": 0, "for": 0, "from": 0,
12+
"global": 0, "if": 0, "import": 0, "in": 0, "is": 0, "lambda": 0,
13+
"None": 0, "nonlocal": 0, "not": 0, "or": 0, "pass": 0, "raise": 0,
14+
"return": 0, "True": 0, "try": 0, "while": 0, "with": 0, "yield": 0}
15+
16+
filename = input("Enter a Python source code filename: ").strip()
17+
18+
if not os.path.isfile(filename): # Check if target file exists
19+
print("File", filename, "does not exist")
20+
sys.exit()
21+
22+
infile = open(filename, "r") # Open files for input
23+
24+
text = infile.read().split() # Read and split words from the file
25+
26+
count = 0
27+
for word in text:
28+
if word in keyWords:
29+
count += 1
30+
keyWords[word] += 1
31+
32+
print("The number of key words:", count)
33+
for w in keyWords:
34+
if keyWords[w] > 0:
35+
print(w, ":", keyWords[w])
36+
37+
38+
main()

CH14/EX14.02.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 14.2 (Count occurrences of numbers) Write a program that reads an unspecified number
2+
# of integers and finds the ones that have the most occurrences. For example, if
3+
# you enter 2 3 40 3 5 4 –3 3 3 2 0, the number 3 occurs most often. Enter all numbers
4+
# in one line. If not one but several numbers have the most occurrences, all of
5+
# them should be reported. For example, since 9 and 3 appear twice in the list 9 30
6+
# 3 9 3 2 4, both occurrences should be reported.
7+
8+
nums = input("Enter numbers in one line: ").split()
9+
10+
nums_dict = {}
11+
12+
for n in nums:
13+
if n in nums_dict:
14+
nums_dict[n] += 1
15+
else:
16+
nums_dict[n] = 0
17+
18+
pairs = list(nums_dict.items())
19+
nums = [[x, y] for (y, x) in pairs]
20+
nums.sort(reverse=True)
21+
22+
print(nums[0][1], end=' ')
23+
24+
for i in range(1, len(nums)):
25+
if nums[i][0] == nums[0][0]:
26+
print(nums[i][1], end=' ')
27+
else:
28+
print()
29+
break

CH14/EX14.03.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 14.3 (Count the occurrences of each keyword) Write a program that reads in a Python
2+
# source code file and counts the occurrence of each keyword in the file. Your program
3+
# should prompt the user to enter the Python source code filename.
4+
5+
import os.path
6+
import sys
7+
8+
9+
def main():
10+
keyWords = {"and": 0, "as": 0, "assert": 0, "break": 0, "class": 0,
11+
"continue": 0, "def": 0, "del": 0, "elif": 0, "else": 0,
12+
"except": 0, "False": 0, "finally": 0, "for": 0, "from": 0,
13+
"global": 0, "if": 0, "import": 0, "in": 0, "is": 0, "lambda": 0,
14+
"None": 0, "nonlocal": 0, "not": 0, "or": 0, "pass": 0, "raise": 0,
15+
"return": 0, "True": 0, "try": 0, "while": 0, "with": 0, "yield": 0}
16+
17+
filename = input("Enter a Python source code filename: ").strip()
18+
19+
if not os.path.isfile(filename): # Check if target file exists
20+
print("File", filename, "does not exist")
21+
sys.exit()
22+
23+
infile = open(filename, "r") # Open files for input
24+
25+
# text = infile.read().split() # Read and split words from the file
26+
text = infile.readlines()
27+
for i in range(len(text)):
28+
indx = text[i].find(chr(35)) # remove comments, I use chr(35) instead of writing # directly as the # may be used in literal strings
29+
if indx != -1:
30+
line = text[i]
31+
line = line[0:indx]
32+
text[i] = line
33+
34+
for line in text:
35+
for word in line.split():
36+
if word in keyWords:
37+
keyWords[word] += 1
38+
39+
for itm in keyWords:
40+
print(itm, ":", keyWords[itm])
41+
42+
43+
main()

CH14/EX14.04.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 14.4 (Tkinter: Count the occurrences of each letter) Rewrite Listing 14.5 using a GUI
2+
# program to let the user enter the file from an entry field, as shown in Figure 14.3a.
3+
# You can also select a file by clicking the Browse button to display an Open file dialog
4+
# box, as shown in Figure 14.3b. The file selected is then displayed in the entry
5+
# field. Clicking the Show Result button displays the result in a text widget. You
6+
# need to display a message in a message box if the file does not exist.
7+
8+
from tkinter import * # Import tkinter
9+
import tkinter.messagebox # Import tkinter.messagebox
10+
from tkinter.filedialog import askopenfilename
11+
12+
13+
def showResult():
14+
analyzeFile(filename.get())
15+
16+
17+
def analyzeFile(filename):
18+
try:
19+
infile = open(filename, "r") # Open the file
20+
21+
counts = 26 * [0] # Create and initialize counts
22+
for line in infile:
23+
# Invoke the countLetters function to count each letter
24+
countLetters(line.lower(), counts)
25+
26+
# Display results
27+
for i in range(len(counts)):
28+
if counts[i] != 0:
29+
text.insert(END, chr(ord('a') + i) + " appears " + str(counts[i])
30+
+ (" time" if counts[i] == 1 else " times") + "\n")
31+
32+
infile.close() # Close file
33+
except IOError:
34+
tkinter.messagebox.showwarning("Analyze File",
35+
"File " + filename + " does not exist")
36+
# Count each letter in the string
37+
38+
39+
def countLetters(line, counts):
40+
for ch in line:
41+
if ch.isalpha():
42+
counts[ord(ch) - ord('a')] += 1
43+
44+
45+
def openFile():
46+
filenameforReading = askopenfilename()
47+
filename.set(filenameforReading)
48+
49+
50+
window = Tk() # Create a window
51+
window.title("Occurrence of Letters") # Set title
52+
53+
frame1 = Frame(window) # Hold four labels for displaying cards
54+
frame1.pack()
55+
56+
scrollbar = Scrollbar(frame1)
57+
scrollbar.pack(side=RIGHT, fill=Y)
58+
text = Text(frame1, width=40, height=10, wrap=WORD,
59+
yscrollcommand=scrollbar.set)
60+
text.pack()
61+
scrollbar.config(command=text.yview)
62+
63+
frame2 = Frame(window) # Hold four labels for displaying cards
64+
frame2.pack()
65+
66+
Label(frame2, text="Enter a filename: ").pack(side=LEFT)
67+
filename = StringVar()
68+
Entry(frame2, width=20, textvariable=filename).pack(side=LEFT)
69+
Button(frame2, text="Browse", command=openFile).pack(side=LEFT)
70+
Button(frame2, text="Show Result", command=showResult).pack(side=LEFT)
71+
72+
window.mainloop() # Create an event loop

CH14/EX14.05.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 14.5 (Tkinter: Count the occurrences of each letter) Revise the preceding exercise to
2+
# display a histogram for the result, as shown in Figure 14.4. You need to display a
3+
# message in a message box if the file does not exist.
4+
5+
from tkinter import * # Import tkinter
6+
import tkinter.messagebox # Import tkinter.messagebox
7+
from tkinter.filedialog import askopenfilename
8+
9+
LINE_WIDTH = 23
10+
BASE = 350
11+
12+
def showResult():
13+
file = open(filename.get(), 'r')
14+
lines = file.readlines()
15+
for i in range(len(lines)):
16+
lines[i] = lines[i].lower()
17+
counts = [0] * 26
18+
countLetters(lines, counts)
19+
20+
cnvs.delete("data")
21+
cnvs.create_line(10, BASE, 595, BASE, tag="data")
22+
x = 12
23+
for i in range(26):
24+
cnvs.create_text(x+2, BASE+8, text=chr(i + ord('a')),tag="data")
25+
cnvs.create_rectangle(x-9, BASE-counts[i], x + LINE_WIDTH-9, BASE,tag="data")
26+
x += LINE_WIDTH
27+
28+
29+
def countLetters(lines, counts):
30+
for line in lines:
31+
for ch in line:
32+
if ch.isalpha():
33+
counts[ord(ch) - ord('a')] += 1
34+
35+
36+
def openFile():
37+
filenameforReading = askopenfilename()
38+
filename.set(filenameforReading)
39+
40+
41+
window = Tk() # Create a window
42+
window.title("Occurrence of Letters") # Set title
43+
44+
frame1 = Frame(window) # Hold four labels for displaying cards
45+
frame1.pack()
46+
47+
cnvs = Canvas(frame1, width=600,height=400)
48+
cnvs.pack()
49+
50+
frame2 = Frame(window) # Hold four labels for displaying cards
51+
frame2.pack()
52+
53+
Label(frame2, text="Enter a filename: ").pack(side=LEFT)
54+
filename = StringVar()
55+
Entry(frame2, width=20, textvariable=filename).pack(side=LEFT)
56+
Button(frame2, text="Browse", command=openFile).pack(side=LEFT)
57+
Button(frame2, text="Show Result", command=showResult).pack(side=LEFT)
58+
59+
window.mainloop() # Create an event loop

0 commit comments

Comments
 (0)