-
Notifications
You must be signed in to change notification settings - Fork 3
/
lesson2.html
executable file
·329 lines (286 loc) · 10.4 KB
/
lesson2.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!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 2 - Booleans and While Loops</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 2:<br>Booleans and While Loops</h3>
<p>
<small>Expedia Code Academy</small>
<br>
<img src="static/img/code_academy_logo.png" border="0px">
</p>
</section>
<section>
<h3>Data types so far</h3>
<p>
<br>We have seen two types of data already:<br>
</p>
<ul>
<li class="fragment">Numbers:
<span id="code">
<ul>
<li>0</li>
<li>-124.5</li>
</ul>
</span>
</li>
<li class="fragment">Strings:
<span id="code">
<ul>
<li>'hello'</li>
<li>'I am 99 years old!'</li>
</ul>
</span>
</li>
</ul>
</section>
<section>
<h3>Booleans</h3>
<p>
<br>
A really useful data type is a <b>Boolean</b>
<br>
It can represent two values - <b>True</b> or <b>False</b>
<br><br>
</p>
<div class="fragment">
<pre><code class="python" data-trim>
my_boolean = 5 > 2
print(my_boolean)
another_boolean = 3.14 < -3
print(another_boolean)
</code></pre>
</div>
</section>
<section>
<h3>Comparisons</h3>
<div class="fragment">
<p>We can also test for equality using ==</p>
<pre><code class="python" data-trim>
first_number = 12 / 3
second_number = 2 * 2
are_equal = first_number == second_number
print(are_equal)
</code></pre>
</div>
<br>
<div class="fragment">
<p>We can also do "less-than-or-equal" and "greater-than-or-equal" comparisons:</p>
<pre><code class="python" data-trim>
2 <= 5 # but not 2 =< 5, order matters!
242 >= 242 # but not 242 => 2
</code></pre>
</div>
</section>
<section>
<h3>Why is this useful?</h3>
<p>
<br>
Think of the program powering Hotels.com. A user can only get exclusive deals if they're a Silver Member...
<br>
<img src="./static/img/silver.png" width="75%" style="padding-top: 20px">
</p>
<p class="fragment">
This means we need to change the price depending on whether this condition is <b>True</b> or not!
<br>
</p>
</section>
<section>
<h3>What if...?</h3>
<div>
<br>
The simplest way to do this is the <b>if</b> statement:
<pre><code class="python" data-trim>
if x > 0:
print('x is positive')
</code></pre>
</div>
<div>
<p class="fragment">
<br>
Syntax is important! You'll need these:
<br>
</p>
<ul>
<li class="fragment">4 spaces before any line <u>under the if</u></li>
<li class="fragment">A colon after your boolean</li>
</ul>
</div>
</section>
<section>
<h3>Indentation</h3>
<br>
The indentation (4 spaces) before each statement determine whether it belongs to the <b>if</b> or not.
<pre><code class="python" data-trim>
if x > 0:
print('x is positive')
print('a message that always gets printed')
</code></pre>
<pre><code class="python" data-trim>
if x > 0:
print('x is positive')
print('a message that gets printed only when x is positive')
</code></pre>
</section>
<section>
<h3>If/else</h3>
<p>
<br>
<pre><code class="python" data-trim>
if x > 0:
print('x is positive')
else:
print('x is negative')
</code></pre>
</p>
<p class="fragment">
<br>
The code under <span id="code">else</span> gets executed when the if boolean is False.
<br><br>
They are mutually exclusive!
</p>
</section>
<section>
<h3>(⭐) 2a. ID check</h3>
<p>
<small>
<ul>
<li>Create a variable called <b>age</b>.</li>
<li>Write an if statement which checks if someone of that age is allowed to drink. They are only if they're 18 and over.</li>
<li>If they are allowed - print "Can drink!".</li>
<li>If they are not - print "You have to wait!"</li>
</ul>
</small>
</p>
</section>
<section>
<h3>If/elif/else</h3>
<p>
Sometimes there are more than two possibilities, so you need more than True/False or if/else.
<br>
</p>
<pre class="fragment"><code class="python" data-trim>
if x > 0:
print('x is positive')
elif x == 0:
print('x is equal to 0')
else:
print('x is negative')
</code></pre>
<p class="fragment">
<span id="code">elif</span> is an abbreviation of “else if”
<br><br>
Please note that <b>only one</b> branch will be executed, and priority starts from the top!
</p>
</section>
<section>
<h3>(⭐⭐) 2b. GradeBot</h3>
<br>
<small>
<ul>
<li>Create a variable called grade.</li>
<li>Write an if statement, which will decide the mark a student will get based on their grade.</li>
<li>If they get a grade higher than or equal to 60 - print 'Pass'.</li>
<li>However, if they get a grade higher than 80 - print 'Distinction'.</li>
<li>If their grade is lower than 60 - print 'Fail'.</li>
</ul>
</small>
</section>
<section>
<h3>Getting our code to do more</h3>
<p>How would we write code to print all numbers between 10 and 0?<br></p>
<p class="fragment">What about 100? Or 1000?</p>
<div class="fragment">
By using loops, we can make python evaluate code more than once<br>
<pre><code class="python" data-trim>
number = 10
while number > 0:
print(number)
number = number - 1
print("Blast Off!")
</code></pre>
</div>
</section>
<section>
<h3>While loops</h3>
While loops are like <span id="code">if</span> statements,<br>
except they keep looping through their block of code until
the Boolean is <span id="code">False</span>
<br><br>
<div class="fragment">
What will this code do?
<pre><code class="python" data-trim>
number = 10
while number > 0:
print(number)
# number = number - 1
print("Blast Off!")
</code></pre>
(⭐️) 2c. print numbers <em>up</em> to 10 (including 0 and 10)
</section>
<section>
<h3>Exercise</h3>
<div>
<p>
(⭐️⭐️) 2d. Program Bob! See <a href="https://github.com/python101ldn/exercises/blob/master/Session_2/2d_bob.py" target="_blank">here</a> for instructions.
</p>
<br>
Hints you may (or may not) need:
<pre><code class="python" data-trim>
# Use `and` to use multiple True conditions at once
# You need to use the `number` variable in each boolean statement
if number > 5 and number < 10:
print('This number is between 6 and 9')
# Use `or` to only require one True condition
if there_is_free_food or i_am_hungry:
print('Time to eat!')
</code></pre>
</div>
</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',
slideNumber: true
});
</script>
</body>
</html>