Skip to content

Commit 96d3764

Browse files
committed
publish Ch. 24
1 parent 33643f4 commit 96d3764

File tree

3 files changed

+301
-12
lines changed

3 files changed

+301
-12
lines changed
-46.9 KB
Binary file not shown.
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 24. Inventory at the English bookstore \n",
8+
"\n",
9+
"\n",
10+
"## Dictionary\n",
11+
"\n",
12+
"\n",
13+
"[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n",
14+
"Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/). Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) \n",
15+
"\n",
16+
"---\n",
17+
"\n"
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"- You are the owner of an English bookstore, and these are some classics you sell:"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"classics = {\"Austen\":\"Pride and Prejudice\", \n",
34+
" \"Shelley\":\"Frankenstein\", \n",
35+
" \"Joyce\":\"Ulyssessss\"}\n",
36+
"print (classics)"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"- You are conducting an inventory, and you need to print authors and titles: "
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"# as dict_items\n",
53+
"print (classics.items())\n",
54+
"# as a list of tuples\n",
55+
"print (list(classics.items()))"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"- Then, you need to print authors and titles separately:"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"# authors as dict_items\n",
72+
"print (classics.keys())\n",
73+
"# authors as a list \n",
74+
"print (list(classics.keys()))\n",
75+
"\n",
76+
"# titles as dict_values\n",
77+
"print (classics.values())\n",
78+
"# titles as a list\n",
79+
"print (list(classics.values()))"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"metadata": {},
85+
"source": [
86+
"- You notice that the title of the last book is wrong, so you correct it:"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"print (\"Wrong title: \" + classics[\"Joyce\"])\n",
96+
"classics[\"Joyce\"] = \"Ulysses\"\n",
97+
"print (\"Corrected title: \" + classics[\"Joyce\"])"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"- Then you add two new books that have just arrived: *Gulliver's travels* by Swift and *Jane Eyre* by Bronte:"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"# adding the first book (syntax 1)\n",
114+
"classics[\"Swift\"] = \"Gulliver's travels\"\n",
115+
"print (classics)\n",
116+
"\n",
117+
"# adding the second book (syntax 2)\n",
118+
"classics.update({\"Bronte\":\"Jane Eyre\"})\n",
119+
"print (classics)"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"- Finally you remove the books by Austen and Joyce because you have just sold them: "
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"# deleting the first book (syntax 1)\n",
136+
"del classics[\"Austen\"]\n",
137+
"print (classics)\n",
138+
"\n",
139+
"# deleting the second book (syntax 2)\n",
140+
"classics.pop(\"Joyce\")\n",
141+
"print (classics)"
142+
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"metadata": {},
147+
"source": [
148+
"---\n",
149+
"## List of dictionaries (*In more depth* section)\n"
150+
]
151+
},
152+
{
153+
"cell_type": "markdown",
154+
"metadata": {},
155+
"source": [
156+
"- Given the following list of dictionaries:"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": null,
162+
"metadata": {
163+
"tags": []
164+
},
165+
"outputs": [],
166+
"source": [
167+
"countries = [{\"name\": \"China\", \"capital\": \"Bejing\"},\n",
168+
" {\"name\": \"France\", \"capital\": \"Paris\"},]\n",
169+
"print (countries)"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"- Add a list element:"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {
183+
"tags": []
184+
},
185+
"outputs": [],
186+
"source": [
187+
"countries.append({\"name\": \"Brazil\", \"capital\": \"Brasilia\"})\n",
188+
"print (countries)"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {},
194+
"source": [
195+
"- Slice the second element:"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {
202+
"tags": []
203+
},
204+
"outputs": [],
205+
"source": [
206+
"print (countries [1])"
207+
]
208+
},
209+
{
210+
"cell_type": "markdown",
211+
"metadata": {},
212+
"source": [
213+
"- Print list elements using a for loop through elements and a for loop through indices:"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": null,
219+
"metadata": {
220+
"tags": []
221+
},
222+
"outputs": [],
223+
"source": [
224+
"# for loop though elements\n",
225+
"print (\"-> for loop though elements\")\n",
226+
"for country in countries:\n",
227+
" print (country)\n",
228+
"\n",
229+
"# for loop though indices\n",
230+
"print (\"-> for loop though indices\")\n",
231+
"for i in range (len(countries)):\n",
232+
" print (countries[i])"
233+
]
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"metadata": {},
238+
"source": [
239+
"- Print the country names using a for loop through elements and a for loop through indices:"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": null,
245+
"metadata": {
246+
"tags": []
247+
},
248+
"outputs": [],
249+
"source": [
250+
"# for loop though elements\n",
251+
"print (\"-> for loop though elements\") \n",
252+
"for country in countries:\n",
253+
" print (country[\"name\"])\n",
254+
" \n",
255+
"# for loop though indices\n",
256+
"print (\"-> for loop though indices\")\n",
257+
"for i in range (len(countries)):\n",
258+
" print (countries[i][\"name\"])"
259+
]
260+
}
261+
],
262+
"metadata": {
263+
"kernelspec": {
264+
"display_name": "Python 3 (ipykernel)",
265+
"language": "python",
266+
"name": "python3"
267+
},
268+
"language_info": {
269+
"codemirror_mode": {
270+
"name": "ipython",
271+
"version": 3
272+
},
273+
"file_extension": ".py",
274+
"mimetype": "text/x-python",
275+
"name": "python",
276+
"nbconvert_exporter": "python",
277+
"pygments_lexer": "ipython3",
278+
"version": "3.11.4"
279+
},
280+
"widgets": {
281+
"application/vnd.jupyter.widget-state+json": {
282+
"state": {},
283+
"version_major": 2,
284+
"version_minor": 0
285+
}
286+
}
287+
},
288+
"nbformat": 4,
289+
"nbformat_minor": 4
290+
}

index.html

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
<head>
55

6-
<!-- Global site tag (gtag.js) - Google Analytics -->
76
<!-- Google tag (gtag.js) -->
8-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-SMB9YNHT28"></script>
9-
<script>
10-
window.dataLayer = window.dataLayer || [];
11-
function gtag(){dataLayer.push(arguments);}
12-
gtag('js', new Date());
7+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-SMB9YNHT28"></script>
8+
<script>
9+
window.dataLayer = window.dataLayer || [];
10+
function gtag(){dataLayer.push(arguments);}
11+
gtag('js', new Date());
1312

14-
gtag('config', 'G-SMB9YNHT28');
15-
</script>
13+
gtag('config', 'G-SMB9YNHT28');
14+
</script>
1615

1716
<meta charset="utf-8">
1817
<meta content="width=device-width, initial-scale=1.0" name="viewport">
@@ -236,12 +235,12 @@ <h2>About the Book</h2>
236235
<div class="col-lg-12">
237236

238237
<div class="progress">
239-
<span class="skill">BOOK COMPLETION <i class="val">63%</i></span>
238+
<span class="skill">BOOK COMPLETION <i class="val">65%</i></span>
240239
<div class="progress-bar-wrap">
241-
<div class="progress-bar" role="progressbar" aria-valuenow="63" aria-valuemin="0" aria-valuemax="100"></div>
240+
<div class="progress-bar" role="progressbar" aria-valuenow="65" aria-valuemin="0" aria-valuemax="100"></div>
242241
</div>
243242
</div>
244-
<center>Next upload: <b>Chapter 24</b> on <b>January 28</b> </center>
243+
<center>Next upload: <b>Chapter 24</b> on <b>March 10</b> </center>
245244
</div>
246245

247246
</div>
@@ -338,7 +337,7 @@ <h4>PART 6: Focus on lists and for loop </h4>
338337
<div class="resume-item pb-0">
339338
<h4>PART 7: Dictionaries and overview of strings</h4>
340339
<ol start="24">
341-
<li>Inventory at the bookstore<br> <i>Dictionary</i></li>
340+
<a href="assets/notebooks/24_dictionaries.ipynb" onclick="download" ></a><li>Inventory at the English bookstore<br> <i>Dictionaries</i></li></a>
342341
<li>Trip to Switzerland<br> <i>Dictionaries with lists as values</i></li>
343342
<li>Counting, compressing, and sorting<br> <i>Dictionary use cases</i></li>
344343
<li>Overview of strings<br> <i>Operations, methods, and tricks</i></li>

0 commit comments

Comments
 (0)