-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlesson6.html
More file actions
executable file
·273 lines (239 loc) · 9.04 KB
/
lesson6.html
File metadata and controls
executable file
·273 lines (239 loc) · 9.04 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Week 6 - Dictionaries</title>
<link rel="stylesheet" href="reveal/css/reveal.css">
<!-- Theme set to Simple Serif with custom overrides below -->
<link rel="stylesheet" href="reveal/css/theme/simple.css">
<link rel="stylesheet" href="static/css/custom.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="reveal/lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'reveal/css/print/pdf.css' : 'reveal/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- TITLE SLIDE -->
<section>
<br>
<h2>Python 101</h2>
<h3>Week 6:<br>Dictionaries</h3>
<p>
<small>Expedia Code Academy</small>
<br>
<img src="static/img/code_academy_logo.png" border="0px">
</p>
</section>
<section>
<h3>Dictionaries</h3>
<p>
I have a dictionary, and I want to look up the meaning of the word <i>hotel</i>.<br>
</p>
<p class="fragment">
You can describe this data as having a key (hotel) and a value (the definition). This is a data structure in
Python!<br>
</p>
<p class="fragment">
Here's a dictionary representing people (keys) and their favourite animals (values):
<img src="static/img/dict.png">
</p>
</section>
<section>
<h3>Dictionaries</h3>
<p>
So far we have seen lists, where we grab elements using their numbered position, or index.<br><br>
Dictionaries are similar to lists, except you access the elements using <b>unordered keys</b>
<div class="fragment">
<pre><code class="python" data-trim>
eng_dict = {} # let's create an English dictionary
# then add words to it
eng_dict['hotel'] = 'An establishment providing accommodation.'
eng_dict['flight'] = 'The process of flying through the air.'
print(eng_dict)
</code></pre>
</div>
</p>
</section>
<section>
<h3>Dictionaries</h3>
<p>
You can also create a dictionary with data in it:
<div>
<pre><code class="python" data-trim>
# We can define a dictionary in one line too
eng_dict = {
'hotel': 'An establishment providing accommodation',
'flight': 'The process of flying through the air'
}
print(eng_dict['hotel']) # to print a value from the dictionary
</code></pre>
</div>
</p>
<p class="fragment">
(⭐️) 6a. Create a dictionary of the class' favourite animals
</p>
</section>
<section>
<h3>Dictionary operations</h3>
We can also modify existing dictionaries - the syntax is the same as adding new values.
<p>
<div>
<pre><code class="python" data-trim>
eng_dict = {
'hotel': 'An establishment providing accommodation',
'flight': 'The process of flying through the air'
}
eng_dict['hotel'] = 'An establishment providing accommodation, buy now at Hotels.com!'
# We can find the number of key-value pairs in the dictionary
len(inventory) # just like for lists
</code></pre>
</div>
</p>
<p class="fragment">
(⭐️⭐️) 6b. Use <span id="code">input()</span> to add someone's name and their favourite animal dynamically.
</p>
</section>
<section>
<h3>Dictionary exercise</h3>
(⭐️⭐️) 6c. Let's access data in a dictionary!
<pre><code class="python" data-trim>
hotel = {
'name': 'Corinthia Hotel',
'amenities': ['Indoor pool', 'Fitness centre', 'Spa'],
'top spots': [
{'name': 'Trafalgar Square', 'proximity': 300},
{'name': 'London Eye', 'proximity': 450},
{'name': 'Buckingham Palace', 'proximity': 1200}
]
}
</code></pre>
<small>
How can you:<br>
<ul>
<li>Access the hotel name?</li>
<li>Find out how many amenities it has?</li>
<li>Find out the names of the top spots near it?</li>
<li>Find out all the top spots within 500 metres reach?</li>
</ul>
</small>
</section>
<section>
<h3>Appendix: Dictionary methods</h3>
We can get lists of all keys or all values.
<p>
<div class="fragment">
<pre><code class="python" data-trim>
inventory = {'apples': 320, 'pears': 228, 'bananas': 420}
print(inventory.keys())
# will print ['apples', 'pears', 'bananas']
print(inventory.values())
# will print [320, 228, 420]
</code></pre>
</div>
</p>
<span class="fragment">
Or we can check if a dictionary contains a given key, just like in lists. <br>
</span>
<p>
<div class="fragment">
<pre><code class="python" data-trim>
inventory = {'apples': 320, 'pears': 228, 'bananas': 420}
'apples' in inventory # will return True
'oranges' in inventory # will return False
</code></pre>
</div>
</p>
</section>
<section>
<h3>Appendix: Dictionary exercises</h3>
<pre><code class="python" data-trim>
inventory = {'apples': 3, 'pears': 2, 'bananas': 0}
</code></pre>
<small>
<ul>
<li>
(⭐️⭐️) 6d. Let's go shopping! For this exercise use the following dictionary:
</li>
<ul>
<li>
The program should handle the following scenario:
</li>
<li>
A shopper would like to buy 1 item. Take user input of what that item is.
<br>
Assume that the user will type 'apples', 'pears' or 'bananas'!
</li>
<li>
Update the inventory to reflect that purchase - i.e. if someone buys apples, their value would go from 3
to 2.
</li>
</ul>
</li>
<li>
(⭐️⭐️⭐️) 6e. Add the following functionality to your shopping program:
<ul>
<li>
i. If that item doesn't exist, let the user know.
</li>
<li>
ii. If that item is sold out, let the user know.
</li>
<li>
iii. (🔥) Also ask the user for how many items they'd like to buy. Hint: you'll need
<pre><code class="python" data-trim>
how_many = input('How many items would you like to buy?')
how_many = int(how_many) # convert string to number (int = integer)
</code></pre>
</li>
</ul>
</li>
<br>
</ul>
</small>
</section>
<section>
<h3>Appendix: Dictionary creation exercises</h3>
<ul>
<li>(⭐️️️⭐️️️⭐️) 6f. Write a program that tracks the count of the letters from a string.
<br><br>
Given the string <span id="code">'expedia'</span> the expected output is:
<br>
<pre><code class="python" data-trim>
{'e': 2, 'x': 1, 'p': 1, 'd': 1, 'i': 1, 'a': 1}
</code></pre>
</li>
<br>
</ul>
</section>
</div>
</div>
<script src="reveal/lib/js/head.min.js"></script>
<script src="reveal/js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'reveal/plugin/markdown/marked.js' },
{ src: 'reveal/plugin/markdown/markdown.js' },
{ src: 'reveal/plugin/notes/notes.js', async: true },
{ src: 'reveal/plugin/highlight/highlight.js', async: true, callback: function () { hljs.initHighlightingOnLoad(); } }
],
history: true,
progress: false,
center: false, // disable vertical centering of text
transition: 'none'
});
</script>
</body>
</html>