forked from gdiboulder/gdi-intro-python
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclass2.html
560 lines (505 loc) · 26.7 KB
/
class2.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Python | Girl Develop It Ann Arbor</title>
<meta name="description" content="This is the official Girl Develop It Core Intro to Python course. The course is meant to be taught in four two-hour sessions. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/gdiaa.css" id="theme">
<link rel="stylesheet" href="lib/css/rainbow.css">
<link rel="stylesheet" href="css/print/pdf.css" media="print">
<script src="lib/js/head.min.js"></script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- INTRO -->
<section>
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Python</span><br/><small><span class="blue">Class 2</span></small></h3>
<p><small>@gdiannarbor | #GDIA2 | #IntroToPython</small></p>
</div>
</section>
<!-- Block 1 30 minutes -->
<section>
<h3>Review</h3>
<ul class="list--xtall box--small">
<li>Arithmetic and variables</li>
<li>Data types</li>
<li>Text editor, command line, and python shell</li>
</ul>
</section>
<section>
<h3>What we will cover today</h3>
<ul span class="box--small list--xtall">
<li class ="fragment">Boolean Expressions and Conditionals</li>
<li class ="fragment">Loops</li>
<li class ="fragment">Functions</li>
</ul>
</section>
</section>
<!-- BOOLEANS & CONDITIONALS -->
<section>
<section>
<h3>Boolean Expressions</h3>
<p class="box copy--small">We can tell the computer to compare values and return True or False. These are called <strong>Boolean expressions</strong></p>
<ul class="list--tall copy--xsmall">
<li class="fragment">Test for equality by using <code>==</code>. We can't use <code>=</code> because that is used for assignment</li>
<li class="fragment">Test for greater than and less than using <code>></code> and <code><</code></li>
</ul>
<pre class="fragment"><code contenteditable class="python" style="min-height: 50px;">
a = 5
b = 5
print(a == b)
# Combine comparison and assignment
c = a == b
print(c)
print(3 < 5)
</code></pre>
</section>
<section>
<h3>Boolean Expressions continued</h3>
<p class="box--small copy--small">The following chart shows the various Boolean operators</p>
<table class="copy--xsmall" style="width: auto; margin: 0 auto;">
<tr>
<td style="width: 200px;"><code>a == b</code></td>
<td>a is equal to b</td>
</tr>
<tr>
<td style="width: 200px;"><code>a != b</code></td>
<td>a does not equal b</td>
</tr>
<tr>
<td style="width: 200px;"><code>a < b</code></td>
<td>a is less than b</td>
</tr>
<tr>
<td style="width: 200px;"><code>a > b</code></td>
<td>a is greater than b</td>
</tr>
<tr>
<td style="width: 200px;"><code>a <= b</code></td>
<td>a is less than or equal to b</td>
</tr>
<tr>
<td style="width: 200px;"><code>a >= b</code></td>
<td>a is greater than or equal to b</td>
</tr>
</table>
<pre class=""><code contenteditable class="python" style="min-height: 50px;">
a = 3
b = 4
print(a != b)
print(a <= 3)
print(a >= 4)
</code></pre>
<p class="copy--xsmall">Remember: Equals does not equal "equals equals"</p>
</section>
<section>
<h3>Conditionals</h3>
<p class="copy--small box--small">When we want different code to execute dependending on certain criteria, we use a <strong>conditional</strong></p>
<div class="fragment box--small">
<p class="copy--xsmall">We achieve this using <strong>if</strong> statements</p>
<pre><code contenteditable class="small python">
if x == 5:
print('x is equal to 5')
</code></pre></div>
<div class="fragment box--small">
<p class="copy--xsmall">We often want a different block to execute if the statement is false.<br/>This can be accomplished using <strong>else</strong></p>
<pre><code contenteditable class="small python">
if x == 5:
print('x is equal to 5')
else:
print('x is not equal to 5')
</code></pre>
</div>
</section>
<section>
<h3>Indentation</h3>
<p class="copy--small box--small">In Python, <strong>blocks</strong> <span class="green">begin when text is indented</span> and<br/><span class="red">ends when it returns to the previous indentation</span></p>
<div class="fragment">
<p class="copy--small box--small ">Let's look at the previous example again with a few minor changes and examine the meaning of its indentation</p>
<pre><code contenteditable class=" python">
if x == 5:
print('x is equal to 5')
x_is_5 = True
print('Still in the x == 5 block')
else:
print('x is not equal to 5')
x_is_5 = False
print('Still in the else block of x == 5')
print('Outside of the if or else blocks.')
print('x_is_5:')
print(x_is_5)
</code></pre></div>
</section>
<section>
<h3>Chained conditionals</h3>
<p class="box--small copy--small">Conditionals can also be <strong>chained</strong></p>
<div class="fragment"><p class="box--small copy--small ">Chained conditionals use <code>elif</code> as an additonal check after the preceeding <code>if</code> predicate was False. For example</p>
<pre><code contenteditable class=" python small">
if x > 5:
print('x is greater than 5')
elif x < 5:
print('x is less than 5')
else:
print('x is equal to 5')
</code></pre></div>
</section>
<section>
<h3>Nested conditionals</h3>
<p class="box--small copy--small">Conditionals can also be <strong>nested</strong></p>
<div class="fragment"><p class="box--small copy--small">Nested conditionals occur inside of other conditionals and are indented over once more.</p><p class="box--small copy--small">When the code block is complete, they are unindented</p>
<pre><code contenteditable class="small python">
if x > 5:
print('x is greater than 5')
if x > 10:
print('...it is also greater than 10')
print('Done evaluating the x > 10 block')
print('Done evaluating the x > 5 block')
</code></pre></div>
</section>
<!-- Let's develop it: 10 minutes -->
<section>
<h3>Let's Develop It</h3>
<p class="box--small copy--small">Write a program that uses if statements to determine<br/>what to do given some user input</p>
<p class="box--small copy--small">The code below is an example:</p>
<pre><code contenteditable class=" python">
health = 100
print("A vicious warg is chasing you.")
print("Options:")
print("1 - Hide in the cave.")
print("2 - Climb a tree.")
input_value = input("Enter choice:")
if input_value == '1':
print('You hide in a cave.')
print('The warg finds you and injures your leg with its claws')
health = health - 10
elif input_value == '2':
print('You climb a tree.')
print('The warg eventually looses interest and wanders off')
</code></pre>
</section>
</section>
<!-- LOOPS -->
<section>
<!-- Block 2 25 minutes -->
<section>
<h3>Iteration</h3>
<p class="box--small copy--small">It is often useful to perform a task and to repeat the process until a certain point is reached.</p>
<p class="box--small copy--small fragment">The repeated execution of a set of statements is called <strong>iteration</strong></p>
<div class="fragment"><p class="box--small copy--small">One way to acheive this, is with the <strong>while</strong> loop.</p>
<pre><code contenteditable class=" small python">
x = 10
while x > 0:
print(x)
x = x - 1
print('Done')
</code></pre></div>
<p class="box--small copy--small fragment">The while statement takes a predicate, and as long as it evaluates to True, the code block beneath it is repeated.</p>
<p class="box--small copy--small fragment">This creates a <strong>loop</strong>. Without the <code>x = x - 1</code> statement, <br/>this would be an <strong>infinite loop</strong></p>
</section>
<section>
<h3>While loops</h3>
<p class="box--small copy--small">Consider the following example that uses iteration to derive a factorial</p>
<p class="box--small copy--xsmall fragment">A factorial of a number is equal to that number * every positive integer less than that number. E.g. The factorial of 4 is 4 * 3 * 2 * 1, which equals 24</p>
<div class="fragment"><pre><code contenteditable class="small python">
input_value = input('Enter a positive integer:') # input('')
n = int(input_value)
result = 1
while n > 1:
result = result * n
n = n - 1
print("The factorial of " + input_value + " is:")
print(result)
</code></pre></div>
<p class="copy--small box--small fragment">This implementation does not work for negative numbers. Why?</p>
</section>
<section>
<h3>For loops</h3>
<p class="copy--small box--small">It is also useful to loop through a collection of elements, visiting each one to do some work, then stopping once all elements are processed.</p>
<p class="copy--small box--small fragment">This can be accomplished with a <strong>for</strong> loop</p>
<div class="fragment"><p class="copy--small box--small ">First, we need a collection. We create a <strong>list</strong> of numbers to loop over. This is called <code>numbers</code> in the following example</p>
<pre><code contenteditable class="small python">
numbers = [1, 3, 8]
for number in numbers:
print("The current number is:")
print(number)
</code></pre></div>
</section>
<section>
<h3>For loops continued</h3>
<p class="copy--small box--small">Let's examine the example carefully</p>
<pre><code contenteditable class="small python">
numbers = [1, 3, 8]
for number in numbers:
print("The current number is:")
print(number)
</code></pre>
<p class="fragment copy--small box--small">The for loop has three parts:</p>
<ul class="list--tall copy--xsmall">
<li class="fragment">The collection to loop over - numbers</li>
<li class="fragment">The name to give each element when the loop begins again - number</li>
<li class="fragment">The block of statements to execute with the element - The two print statements</li>
</ul>
</section>
<!-- Let's Develop It - 10 minutes -->
<section>
<h3>Let's Develop It</h3>
<ul class="list--xtall copy--small box">
<li class="fragment">Write a program that obtains user input like the last program</li>
<li class="fragment">However, this program should not exit until the user types "quit".</li>
<li class="fragment">Hint: A loop should help you</li>
</ul>
</section>
</section>
<!-- FUNCTIONS -->
<section>
<!-- Block 3 30 minutes -->
<section>
<h3>Functions</h3>
<p class="copy--small box--small">A named section of code that performs a specific task</p>
<p class="copy--small box--small fragment">When one uses a function, one makes a function <strong>call</strong></p>
<div class="fragment"><p class="copy--small box--small ">We have already made a function call<br/>when using the <code>type</code>, <code>int</code>, or <code>float</code> functions</p>
<pre><code contenteditable class=" python small">
a = '3'
print(type(a))
a = float(a)
</code></pre></div>
</section>
<section>
<h3>Function calls</h3>
<pre><code contenteditable class="small python">
a = 3
print(type(a))
</code></pre>
<p class="copy--small box--small">A function can take <strong>arguments</strong></p>
<p class="copy--small box--small">In the example above, the variable <code>a</code> is passed<br/>as an argument to the function <code>type</code></p>
<p class="copy--small box--small fragment">Arguments can also be called <strong>parameters</strong></p>
<div class="fragment "><pre><code contenteditable class="python" style="min-height: 50px;">
# Some more function call examples
int('32')
str(32)
</code></pre></div>
</section>
<section>
<h3>Function definition</h3>
<p class="copy--small box--small">The following example is a <strong>function definition</strong>.<br/>This allows us to create our own functions</p>
<pre><code contenteditable class="small python">
def print_plus_5(x):
print(x + 5)
</code></pre>
<p class="copy--small box--small fragment">The function definition has the following parts</p>
<ul class="copy--xsmall box--small list--tall">
<li class="fragment">The <code>def</code> keyword signifies we are defining a function</li>
<li class="fragment">The name of the function being defined - <code>print_plus_5</code></li>
<li class="fragment">The arguments in parentheses - <code>x</code></li>
<li class="fragment">The function <strong>body</strong>, which is a block of indented code that executes when the function is called. - <code>print x + 5</code></li>
</ul>
</section>
<section>
<h3>Function returns</h3>
<p class="copy--small box--small">A function can also <strong>return</strong> a value</p>
<div class="copy--small box--small fragment"><p>To do this, one uses the <strong>return</strong> keyword</p>
<pre><code contenteditable class=" small python">
def plus_5(x):
return x + 5
y = plus_5(4)
</code></pre></div>
<ul class="list--tall copy--xsmall">
<li class="fragment">This allows us to call a function to obtain a value for later use.<br/>(Not the same as printing the value)</li>
<li class="fragment">In this example, the function call <code>plus_5(4)</code> evaluates to 9, and <code>y</code> is set to this value</li>
<li class="fragment">To determine what a function will return, use the <strong>substitution method</strong>.</li>
<li class="fragment">If return is not used, the function returns <strong>None</strong></li>
</ul>
</section>
<section>
<h3>Functions with no arguments</h3>
<p class="copy--small box--small">A function does not have to take arguments,<br/>as in the following example:
<pre><code contenteditable class="small python">
def newline():
print('')
newline()
# prints an empty line. Nothing is returned
</code></pre>
<p class="copy--xsmall box--small ">This is useful when the function does some work but doesn't need any parameters.<br/>i.e. The function is intended to always do the same thing</p>
</section>
<section>
<h4>Functions with more than one argument</h4>
<p class="copy--small box--small">A function can also take more than one argument separated by commas. For example:</p>
<pre><code contenteditable class="small python">
def find_rectangle_area(width, height):
return width * height
area = find_rectangle_area(3, 4)
# area is set to the value 12
</code></pre>
</section>
<section>
<h3>Scope</h3>
<p class="copy--small box--small">The <strong>scope</strong> of a variable is the area of code in which a variable is still valid and can be used.</p>
<p class="copy--small box--small fragment">Variables defined within a function can not be used elsewhere.</p>
<div class="fragment "><pre><code contenteditable class="small python">
def get_triangle_area(base, height):
rect_area = base * height
return rect_area / 2.0
triangle_area = get_triangle_area(10, 20)
print(height)
# NameError
print(rect_area)
# NameError
</code></pre></div>
</section>
<section>
<h3>Import statements</h3>
<p class="copy--small box--small">The <strong>import</strong> statement allows us to use Python code that is defined in other files</p>
<p class="copy--small box--small fragment">Import statements can be used to import various specialized modules - such as scikit or nympy - into our code</p>
<p class="copy--small box--small fragment"><strong>module:</strong> A published collection of useful functions for doing a specific type of coding. There are math, statistics, data science, and graphics modules, to name a few types.</p>
<p class="copy--small box--small fragment">The <strong>from</strong> keyword allows us to only import parts of a Python file</p>
<div class="fragment "><pre><code contenteditable class="python" style="min-height: 50px;">
from math import sqrt
sqrt(2)
</code></pre></div>
</section>
<!-- Let's develop it: 15 minutes -->
<section>
<h3>Let's Develop It</h3>
<ul class="list--xtall copy--small box--small">
<li>Write a program with a function called pigLatin that takes in a word, and prints that word in Pig Latin (that is, move the first letter to the end of the word and add "-ay"). Call the function 3 times with 3 different words.</li>
<li>Write a program that adds 5 to a number, and returns the result. Call the function at least three times with various numbers, and print the result of each function call.</li>
</section>
<section>
<h3>Let's Develop It</h3>
<ul class="list--tall copy--small box--small">
<li>Write a program that asks the user to guess a number between a given range, such as 1 to 10</li>
<li>The program should give the user hints such as "too high" or "too low". Alternatively, the hints might be "warm" or "cold" depending on how close they are to the number</li>
<li>The computer will need to have a random number for the user to guess:</li>
<pre><code contenteditable class="python" style="min-height: 50px;">
#At the top of the file
from random import randint
# Use this line where you need to have a random number.
# (Hint, this is probably used before the user input loop)
random_number = randint(1, 10)
</code></pre>
</ul>
</section>
</section>
<!-- OUTRO -->
<section>
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Homework</h3>
<ul>
<li><p>Write a function that a bar could use to check to see if a customer is of drinking age. <pre>isOfDrinkingAge(age)</pre></p></li>
<li><p>If it is a saturday in spring and it is above 65 degrees, Kermit plants flowers. Write a function to check whether he will plant flowers today. <pre>willKermitPlantFlowers(dayOfWeek, season, currentTemperature)</pre></p></li>
<li><p>Zach knows something is wrong with his basement if the temperature is below 50 or the humidity is above 70%. Write a function that can check whether his basement has a problem. <pre>basementIsOk(temperature, humidity)</pre></p></li>
</ul>
</section>
<section>
<h3>Homework</h3>
<p>Given any list of any sort of mixed types,write a function that prints the type each item in the list. </p>
<p>You can try this list: <pre>[1, 'hello', 12.33, 'data', [1,2]]</pre></p>
<br>
<pre>typeIdentifier(list)</pre>
<pre><code contenteditable class="small python">
#should output something like this:
1 is a <class int>
hello is a <class string>
12.33 is a <class float>
</code></pre>
</section>
<section>
<h3>Homework</h3>
<p>Write a function that asks the user to guess a number between 1 and 100</p>
<ul>
<li class="future"><p>If their guess is within 20, tell them they are 'warm'</p></li>
<li class="future"><p>If their guess is within 10, tell them they are 'hot'</p></li>
<li class="future"><p>If their guess is within 5, tell them they are 'scalding'</p></li>
<li class="future"><p>if their guess is within 50, tell them they are 'cool'</p></li>
<li class="future"><p>otherwise tell them they are 'cold'</p></li>
<li class="future"><p>If they guess the number, congratulate them and ask if they'd like to play again.</p></li>
</ul>
<h6>Optional Extra Challenge: Let them know if they are 'warmer' or 'colder' than their last guess.</h6>
</section>
<section>
<h3>Optional Function Practice</h3>
<pre><code>
# write a function to...
# ...add 10 to any number
# ...divide any number by 100
# ...add any 2 numbers
# ...subtract any 2 numbers
# ...convert celsius to farenheight
# ...convert farenheight to celsius
# ...convert inches to centimeters
# ...convert centimeters to inches
# ...calculate sales tax for any given total amount
# ...calculate the area of a triangle (1/2 base * height)
# ...calculate the volume of a rectangular prism (length * width * height)
</code></pre>
</section>
<section>
<h3>Boolean Practice</h3>
<p>Do this excercise from Learn Python The Hard Way</p>
<i>Make sure to do these problems by hand, not with python! </i>
<h6><a href="http://learnpythonthehardway.org/book/ex28.html">http://learnpythonthehardway.org/book/ex28.html</a></h6>
</section>
<section>
<h3>Want help?</h3>
<div class="box">
<p class="box"><a href="https://gdiaa.slack.com/messages/0516-intro-python/details/" target="_blank" class="roll"><span>#0516-intro-python</span></a></p>
<p><small>Not on Slack? <a target="_blank" href="http://bit.ly/gdiaa-slack" class="roll"><span data-title="bit.ly/gdiaa-slack">bit.ly/gdiaa-slack</span></a></small></p>
</div>
</section>
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Python</span><br/><small><a href="class3.html">Class 3 »</a></small></h3>
<p><small>@gdiannarbor | #GDIA2 | #IntroToPython</small></p>
</div>
</section>
</section>
</div>
<footer>
<div class="copyright">
@gdiannarbor | #GDIA2 | #IntroToPython
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
rollingLinks: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>