-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlesson3.html
executable file
·229 lines (196 loc) · 7.82 KB
/
lesson3.html
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
<!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 3 - Lists</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 3:<br>Lists</h3>
<p>
<small>Expedia Code Academy</small>
<br>
<img src="static/img/code_academy_logo.png" border="0px">
</p>
</section>
<section>
<h3>Lists</h3>
<p>
A list acts as a single variable to store lots of data.
<br><br>
Each of those values is called an element of the list and can be accessed by its position, or <b>index</b>.
<br><br>
<img src="static/img/list.png">
</p>
</section>
<section>
<h3>Lists</h3>
<p>
In Python, you can define lists like this:
<br><br>
<pre><code class="python" data-trim>
# a list of strings
expedia_brands = ['Hotels.com', 'EPS', 'Expedia']
# a list of numbers
exam_grades = [40, 55, 99, 73]
# a list containing mixed types
a_bit_of_everything = ['hello!', 2, True, 0.535]
# a list of lists
words = [['hello', 'yes', 'no'], ['hola', 'si', 'no']]
</code></pre>
</p>
</section>
<section>
<h3>Lists</h3>
You can pick out items from a list by using their index which starts from 0, and also ends in -1.
<img src="static/img/list_backwards.png" height=180px>
<pre class="fragment"><code class="python" data-trim>
# a list of strings
expedia_brands = ['Hotels.com', 'EPS', 'Expedia', 'Egencia']
print(expedia_brands[0]) # 'Hotels.com'
print(expedia_brands[2]) # 'Expedia'
print(expedia_brands[-1]) # 'Egencia'
print(expedia_brands[-2]) # 'Expedia'
</code></pre><br>
</section>
<section>
<h3>Checkpoint</h3>
<br>
What error do you see if you try accessing an element that doesn't exist?
<br><br>
(⭐) 3a. How would you check if the first and last elements of a list are the same?
</section>
<section>
<h3>Lists exercise</h3>
It is useful to know the number of items in a list too:
<pre><code class="python" data-trim>
# Create a list
random_numbers = [31, 5.36, 0, 25, 535, 42.3]
length = len(sentence)
print(length) # 6
</code></pre>
<br>
<div class="fragment">
(⭐⭐️) 3b. Find the average of <span id="code">random_numbers</span><br><br>Hint: add all the numbers together then divide by how many numbers there are
<br><br>
</div>
</section>
<section>
<h3>Building lists</h3>
<p><br>
<pre><code class="python" data-trim>
# Start with a list
sentence = ['Programming', 'is']
# Add an item to the end
sentence.append('fun')
print(sentence)
# To create an empty list
empty_list = []
</code></pre><br>
(⭐⭐️️️) 3c. Build a list of 1-100 yourself using <span id="code">append</span> and a <span id="code">while</span> loop<br>
</p>
</section>
<section>
<h3>Building lists</h3>
<br>
We can concatenate two lists together.
<pre><code class="python" data-trim>
# Create two lists
sentence_start = ['Programming', 'is']
sentence_end = ['really', 'fun']
# Join the two lists together using the + operator
sentence = sentence_start + sentence_end
print(sentence)
</code></pre>
</section>
<section>
<h3>List Membership</h3>
Sometimes it's useful to find if an element exists in a given list.
<pre><code class="python" data-trim>
expedia_brands = ['Hotels.com', 'EAN', 'Expedia']
if 'Booking.com' in expedia_brands:
print('Something must be wrong!')
if 'Hotels.com' in expedia_brands:
print('Yay!')
</code></pre>
(⭐️⭐️) 3d. Program Bob (version 2)!
<br>
Get Bob to say more! See
<a href="https://github.com/python101ldn/exercises/blob/master/Session_3/3d_bob_v2.py" target="_blank">
here
</a> for the full conversation, and
<a href="https://github.com/python101ldn/exercises/blob/master/Session_2/2d_bob_solution.py" target="_blank">
here
</a>
for the solution to the original Bob exercise.
</section>
<section>
<h3>Appendix: Sorting & Slicing</h3>
We can sort lists:
<pre><code class="python" data-trim>
random_numbers = [5, 3, 4, 6, 1, 2]
sorted_numbers = sorted(random_numbers)
print(sorted_numbers) # displays [1, 2, 3, 4, 5, 6]
</code></pre>
<div class="fragment">
<br>
Or take sections (called slices) of a list:
<pre><code class="python" data-trim>
letters = ['a', 'b', 'c', 'd', 'e', 'f']
letters[1:3] # results in ['b', 'c']
letters[2:3] # results in ['c']
</code></pre>
</div>
</section>
<section>
<h3>Appendix: Exercises</h3>
<br>
<pre><code class="python" data-trim>
numbers = [65, 4, 9, 23, 15, 875, 3.69, 33, 12, 79, 6]
</code></pre><br>
(⭐️⭐️) 3e. Write a program to print the lowest and highest numbers<br>
(⭐️⭐️️️️⭐️️️) 3f. Write a program that splits a list into two halves - if there's an odd number of elements the second list will be longer
</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>