forked from gdiboulder/gdi-intro-python
-
Notifications
You must be signed in to change notification settings - Fork 10
/
class3.html
498 lines (433 loc) · 22.4 KB
/
class3.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
<!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 3</span></small></h3>
<p><small>@gdiannarbor | #GDIA2 | #IntroToPython</small></p>
</div>
</section>
<!-- Block 1 25 minutes -->
<section>
<h3>Review</h3>
<ul class="list--xtall copy--small box">
<li>Functions - calls, definitions, returns, arguments</li>
<li>Conditions - if, elif, else</li>
<li>Loops - while, for</li>
</ul>
</section>
<section>
<h3>What we will cover today</h3>
<ul class="list--xtall copy--small box">
<li class="fragment">More Functions</li>
<li class="fragment">Method calls</li>
<li class="fragment">Lists and dictionaries</li>
<li class="fragment">Python builtin functions</li>
</ul>
</section>
</section>
<!-- FUNCTIONS -->
<section>
<section>
<h3>More with Functions</h3>
<p class="copy--small box--small">Functions can also call other functions.</p>
<p class="copy--small box--small fragment">You can use this to break up tasks<br/>into small pieces that rely on others to do their work.</p>
<div class="fragment">
<pre><code contenteditable class=" python">from math import sqrt
def absolute_difference(value_a, value_b):
return abs(value_a - value_b)
def get_hypotenuse(a, b):
return sqrt(a ** 2 + b ** 2)
def get_area_rectangle(width, height):
return width * height
def print_area_and_hypotenuse(x1, y1, x2, y2):
width = absolute_difference(x1, x2)
height = absolute_difference(y1, y2)
area = get_area_rectangle(width, height)
hypotenuse = get_hypotenuse(width, height)
print('Area of the rectangle is:')
print(area)
print('The diagonal of the rectangle is:')
print(hypotenuse)
</code></pre>
</div>
</section>
<section>
<h3>Remember</h3>
<p class="copy--small box"><strong>Functions</strong> are called by their name - <code>my_function()</code></p>
<p class="copy--small box">They can be passed data in the form of parameters - <code>my_function(my_parameter)</code></p>
<p class="copy--small box">They usually return some sort of value. This all happens explicitly.</p>
</section>
</section>
<section>
<section>
<h3>Method Calls</h3>
<p class="copy--small box"><strong>Methods</strong> are also called by name but are associated with an object.<br/><small>Really, they are identical to functions except that the data passed into it is passed implicitly.</small></p>
<p class="copy--xsmall box fragment">For example, the integers and strings we've been using have methods attached to them.</p>
<div class="fragment"><pre><code contenteditable class=" small python">
name = 'caleb'
sentence = 'the quick brown fox did the thing with the thing'
print(name.capitalize())
print(sentence.count('the'))
</code></pre></div>
</section>
<section>
<h3>String Indexing</h3>
<p class="copy--xsmall box fragment">Python strings include methods for accessing specific characters, or substrings.</p>
<p class="copy--xsmall box fragment">To index from the back of the string, use negative numbers.</p>
<div class="fragment"><pre><code contenteditable class=" small python">
sentence = 'Hello World'
print(sentence[0]) # prints 'H'
print(sentence[0:5]) # prints 'Hello'
print(sentence[-5]) # prints 'W'
print(sentence[-5:]) # prints 'World'
</code></pre></div>
</section>
<section>
<h3>String Manipulation</h3>
<p class="copy--xsmall box fragment">There are also methods available for manipulating and changing the values of a string</p>
<div class="fragment"><pre><code contenteditable class=" small python">
sentence = 'Hello World'
print(sentence.split()) # prints a list: ['Hello', 'World']
shopping_list = ['apples', 'bananas', 'grapes']
print(', '.join(shopping_list))
</code></pre></div>
</section>
<!-- Let's develop it: 5 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>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Open a Python shell and define a string varaible</p>
<p class="copy--small box--small">Use <code>dir(string_variable)</code> and the <code>help()</code> function<br/>to explore the various methods
<p class="copy--small box--small"><strong>Hint:</strong> Like functions, some methods take arguments and others don't</p>
<p class="copy--small box--small"><strong>Hint:</strong> Use <code>help()</code> on a method.<br/><small>It will tell you the arguments to use and the expected behavior</small></p>
<p class="copy--small box--small"><strong>Hint:</strong> Don't be afraid of errors.<br/><small>They seem to be in a foreign language but they are there to help you.<br/><i>Read them carefully.</i></small></p>
</section>
</section>
<!-- LISTS : 30 minutes -->
<section>
<section>
<h3>Lists</h3>
<p class="copy--small box--small">A list is an ordered collection of elements</p>
<div class="fragment"><p class="copy--small box--small">In Python, a list is defined using <code>[ ]</code> with elements separated by commas, as in the following example</p>
<pre><code contenteditable class="small python">
words = ['list', 'of', 'strings']
</code></pre>
</div>
</section>
<section>
<h3>Multi-type Lists</h3>
<div class="fragment">
<p class="copy--small box--small">A list can, but doesn't have to be of all one type.<br/><small>A list of one type is <strong>homogenous</strong> as opposed to a list of multiple types, which is <strong>heterogeneous</strong>.</small></p>
<pre><code contenteditable class="small python">
# heterogenous list
words = [0, 'list', 'of', 3, 'strings', 'and', 'numbers']
</code></pre></div>
</section>
<section>
<h3>List Methods</h3>
<p class="copy--small box--small">Lists have several methods, the most useful of which is <code>append</code></p>
<div class="copy--small box--small">
<p class="copy--small box--small ">A list can be created as an empty list and have values added to it with <code>append</code></p>
<pre><code contenteditable class="small python">
to_dos = []
to_dos.append('buy soy milk')
to_dos.append('install git')
print(to_dos)
</code></pre></div>
</section>
<section>
<h3>Iteration</h3>
<p class="copy--small box--small">Lists and many other collections are <strong>iterable</strong>.</p>
<div class="fragment"><p class="copy--small box--small">Once defined, we can iterate on them,<br/>performing an action with each element</p>
<pre><code contenteditable class="small python">
shipping_cost = 2.5
prices = [3, 4, 5.25]
costs = []
for price in prices:
costs.append(price + shipping_cost)
for cost in costs:
print(cost)
</code></pre></div>
</section>
<section>
<h3>Indexing</h3>
<p class="copy--small box--small">An element can also be obtained from a list through <strong>indexing</strong></p>
<p class="copy--small box--small fragment">This allows us to obtain an element without iterating through the entire collection if we just want one value.</p>
<div class="fragment"><p class="copy--small box--small">To index on a collection, follow it immediately with <code>[index]</code>. (index here is a number, variable or expression)</p>
<pre><code contenteditable class="small python">
numbers = [10, 20, 30]
print(numbers[0])
</code></pre></div>
</section>
<section>
<h3>Indexing continued</h3>
<p class="box--small copy--small">Lists and other collections in Python are <strong>zero indexed</strong>.<br/><small>This means that the number 0 refers to first element in the list.</small></p>
<pre><code contenteditable class="small python">
to_dos = [
'install git', 'read email', 'make lunch',
]
print(to_dos[0])
print(to_dos[1])
print(to_dos[-1])
</code></pre>
<p class="box--smal copy--xsmall fragment">Python can index from the end of a string by using negative numbers</p>
<p class="box--small copy--xsmall fragment">An IndexError results if an index is larger than the list size</p>
</section>
</section>
<section>
<section>
<h3>Dictionaries</h3>
<p class="box--small copy--small">A <strong>dictionary</strong> is a collection of key/value pairs, defined with <code>{}</code></p>
<pre><code contenteditable class="small python">
menu_categories = {
'food': 'stuff you eat',
'beverage': 'stuff you drink',
}
</code></pre>
<p class="copy--small box--small fragment">Think of words in a dictionary.<br/><small>The words are keys and the definitions are values.</small></p>
<p class="copy--small box--small fragment">This dictionary would be indexed with strings such as 'food' and 'beverage' instead of integers like in a list</p>
</section>
<section>
<h3>Indexing on Dictionaries</h3>
<p class="box--small copy--small">Dictionaries aren't just for definitions. They represent a group of mappings. A mapping might be: menu items -> costs.</p>
<p class="box--small copy--small fragment">We can also index on dictionaries.</p>
<div class="fragment">
<p class="box--small copy--small">The most common indexes are strings,<br/>but they can be whatever type the keys are.</p>
<pre><code contenteditable class="small python">
menu = {
'tofu': 4,
}
tofu_cost = menu['tofu']
</code></pre></div>
<p class="box--small copy--small fragment">Indexing on a key that doesn't exist results in a KeyError</p>
<p class="box--small copy--small fragment">If you aren't certain a key is present, you can use the <code>get</code> method</p>
</section>
<section>
<h3>Dictionary Methods</h3>
<p class="box--small copy--small">Some of the most essential methods are <code>keys</code>, <code>values</code> and <code>items</code></p>
<pre><code contenteditable class="small python">
menu = {
'tofu': 4,
'pizza': 8,
'baguette': 3,
}
print(menu.keys())
print(menu.values())
print(menu.items())
print(menu.get('pizza'))
print(menu.get('water'))
print(menu.get('juice', 5))
</code></pre>
<p class=" box--small copy--small"><code>get</code> will return None if the key isn't present<br/>or a default value if provided.</p>
</section>
<section>
<h3>Let's Develop It- Example Code</h3>
<pre><code contenteditable class="small python">
from helpers import generate_cleaned_lines
def is_word_in_file(word, filename):
for line in generate_cleaned_lines(filename):
# line will be a string of each line of the file in order
# Your code goes here.
# Your code should do something with the word and line variables and assign the value to a variable for returning
input_word = input("Enter a word to search for:")
answer = is_word_in_file(input_word, 'pride.txt')
# Display the answer in some meaningful way
</code></pre>
<p class="copy--xsmall box--small">I have used <a href="http://www.gdiannarbor.com/events/intro-python/examples/pride.txt">Pride and Prejudice</a> from Project Gutenburg with my example code.</p>
<p class="copy--xsmall">You can click this link and copy/paste the text into a new text file<br/>called 'pride.txt' and save it in the same folder as your code</p>
</section>
</section>
<!-- Block 3 20 minutes -->
<section>
<section>
<h3>Builtins for collections</h3>
<p class="copy--small box--small">Python provides several functions that<br/>help us work with these collections.</p>
<table class="copy--xsmall">
<tr>
<td style="width: 200px;"><code>len()</code></td>
<td>Given a collection, return its length</td>
</tr>
<tr>
<td><code>range()</code></td>
<td>Create a list of integers in the range provided.</td>
</tr>
<tr>
<td><code>sorted()</code></td>
<td>Given a collection, returns a sorted copy of that collection</td>
</tr>
<tr>
<td><code>enumerate()</code></td>
<td>Returns a list of (index, element) from the list</td>
</tr>
<tr>
<td><code>zip()</code></td>
<td style="line-height: 1.25">Given one or more iterables, returns a list of tuples with an element from each iterable</td>
</tr>
</table>
</section>
<section>
<h3>Examples of using Builtins</h3>
<pre><code contenteditable class="python">
# Using len() - Determines length
print(len([1, 2]))
# range() - Quickly creates a list of integers
print(range(5))
print(range(5, 10))
print(range(0, 10, 2))
print(range(9, -1, -1))
# sorted() - Sort a given list
grades = [93, 100, 60]
grades = sorted(grades)
print(grades)
</code></pre>
</section>
<section>
<h3>Builtins Examples continued</h3>
<pre><code contenteditable class="python">
# enumerate() - Obtain the index of the element in the loop
print('To Do:')
to_dos = ['work', 'sleep', 'work']
for index, item in enumerate(to_dos):
print('{0}. {1}'.format(index + 1, item))
print(list(enumerate(to_dos)))
# zip()
widths = [10, 15, 20]
heights = [5, 8, 12]
for width, height in zip(widths, heights):
print('Area is {0}'.format(width * height))
</code></pre>
</section>
<!-- Let's develop it: 25 minutes -->
<!-- <section>
<h3>Let's Develop It</h3>
<p class="copy--small box">Write a program that expands on your previous one.<br/>If it is unfinished, feel free to finish the original exercise first.<br/>To expand on it, choose one of the following:</p>
<ul class="list--xtall copy--xsmall">
<li>Determine how many times the user provided word appears in the file and/or what lines it appears on</li>
<li>Change the program so that it counts the number of times each word occurs. E.g. A dictionary of all words in the file, whose values are a count of their occurrences</li>
<li>Use <a href="http://www.gdiannarbor.com/events/intro-python/examples/boilerplate.py">boilerplate.py</a> to help you improve the reusability of the program. (The comments in that file should explain the how and why)</li>
</ul>
<p class="copy--small box">Resources for this and the previous exercise<br/>are provided on the next slide for convenience</p>
</section> -->
</section>
<section>
<section>
<h3>Prep for Flask</h3>
<p class="box copy--small"><a href="http://flask.pocoo.org/">Flask</a></p>
</section>
</section>
<section>
<section>
<h3>Questions?</h3>
</section>
<!-- Let's develop it: 15 minutes -->
<section>
<h3>Let's Develop It</h3>
<p class="box copy--small">Write a program that opens a text file and does some processing.</p>
<ul class="list--xtall copy--xsmall">
<li>The program should take a word as input and determine if the word appears in the file</li>
<li>The program should use at least one function to do its work and you should be able to import this function in a Python shell and call it with a word and filename</li>
<li>Use the functions from <a href="http://www.gdiannarbor.com/events/intro-python/examples/helpers.py">helpers.py</a> to help with reading in the lines and/or words of the file</li>
<li>Download a book in plain text from <a href="http://www.gutenberg.org/wiki/Main_Page">Project Gutenburg</a> and put it into the same directory as your python file.</li>
</ul>
<p class="box copy--small">The next slide has some code and other<br/>resources that should help you get started</p>
</section>
<section>
<h3>Resources</h3>
<ul class="list--tall copy--xsmall box">
<li>Helper functions are in <a href="http://www.gdiannarbor.com/events/intro-python/examples/helpers.py">helpers.py</a></li>
<li>Download a book in plain text from <a href="http://www.gutenberg.org/wiki/Main_Page">Project Gutenburg</a> and put it into the same directory as your python file.</li>
<li>You can use this link for <a href="http://www.gdiannarbor.com/events/intro-python/examples/pride.txt">Pride and Prejudice</a>. Click this link and copy/paste the text into a new text file called 'pride.txt' and save it in the same folder as your code</li>
</ul>
<pre><code contenteditable class="python">
from helpers import generate_cleaned_lines
def is_word_in_file(word, filename):
for line in generate_cleaned_lines(filename):
# line will be a string of each line of the file in order
# Your code goes here. Do something with the word and line variables
return result
input_word = input("Enter a word to search for:")
answer = is_word_in_file(input_word, 'pride.txt')
# Display the answer in some meaningful way
</code></pre>
</section>
<section>
<h3>Homework</h3>
<p>Expand on the searching for word in text problem.</p>
<p>Can you display the context (3 words before and after) of each occurance of that word?</p>
</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="class4.html">Class 4 »</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>