forked from durcana/gdi-intro-python
/
class3.html
425 lines (362 loc) · 18.3 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Python | Girl Develop It Class 3</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/light.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">
<!-- 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 Class 3</span></h3>
</div>
</section>
<!-- Block 1 25 minutes -->
<section>
<h3>Review</h3>
<ul class="list--xtall copy--small box">
<li>Lists - appending, indexing</li>
<li>Conditions - if, elif, else</li>
<li>Loops - while, for</li>
</ul>
</section>
<section>
<h3>What we will cover this morning</h3>
<ul class="list--xtall copy--small box">
<li class="fragment">Functions</li>
<li class="fragment">Method calls</li>
<li class="fragment">Dictionaries</li>
<li class="fragment">Python builtin functions</li>
</ul>
</section>
<!-- FUNCTIONS -->
<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">This is also referred to as <strong>calling</strong> a function</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>Creating a Function</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)
print y
</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 grass():
print 'The grass is green.'
grass()
</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)
print area
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Pick one of the following exercises</p>
<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>Function Composition</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>
<p class="copy--small box--small"><strong>Function composition</strong> is when the <span class="blue">output</span><br/>of one function <span class="green">acts as the input</span> of another</p>
<div class="fragment">
<pre><code contenteditable class=" python">from math import sqrt
def find_distance(p1, p2):
return abs(p1 - p2)
def get_hypotenuse(a, b):
return sqrt(a ** 2 + b ** 2)
def print_hypotenuse(x1, y1, x2, y2):
print 'The diagonal of the rectangle is:'
print get_hypotenuse(find_distance(x1, x2), find_distance(y1, y2))
print_hypotenuse(1,2,3,4)
# f(g(x))
# is the same as:
# y = g(x)
# f(y)
</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>
<!-- 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 that uses at least one function to solve a geometry problem</li>
<li>Hint: You might use a loop that obtains user input, does the calculation, then prints the answer. As before, the user should be able to quit by entering "quit"</li>
<li>Hint: You can import your function in the shell and call it with different parameters to test it out</li>
<li>Hint: Download <a href="http://nicklang.com/gdi-intro-python/examples/geometry.py">geometry.py</a> and use it as an example</li>
</ul>
</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>
<p class="copy--xsmall box fragment">We can use the <code>dir()</code> function to see the methods of an object<br/>and <code>help()</code> to see what they do.</p>
<div class="fragment"><pre><code contenteditable class=" small python">a = 4
print dir(a)
name = 'caleb'
sentence = 'the quick brown fox did the thing with the thing'
print dir(name)
print name.capitalize()
print sentence.count('the')
help(name.upper)
</code></pre></div>
</section>
<!-- Let's develop it: 5 minutes -->
<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>
<h3>Dictionaries</h3>
<p class="box--small copy--small">A <strong>dictionary</strong> (sometimes called a "hashmap") is a data type 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 literally 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>The in operator</h3>
<p class="box--small copy--small">The <code>in</code> operator is used to determine if<br/>an element is in a given data type</p>
<div class="fragment"><p class="box--small copy--small ">For dictionaries, the keys are searched for the element.</p>
<pre><code contenteditable class="small python">
color = [255, 255, 0]
if 0 in color:
print '0 is in the color'
menu = {'tofu': 4}
print 'tofu' in menu
names = ['Mary', 'Martha', 'George']
george_present = 'George' in names
</code></pre></div>
</section>
<section>
<h3>Builtins for data types</h3>
<p class="copy--small box--small">Python provides several functions that<br/>help us work with these data types.</p>
<table class="copy--xsmall">
<tr>
<td style="width: 200px;"><code>len()</code></td>
<td>Given a data type, 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 data type, returns a sorted copy of that data type</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])
print len("Hello")
# range() - Quickly creates a list of integers
print range(5)
print range(5, 10)
print range(0, 10, 2)
# sorted() - Sort a given list
grades = [93, 100, 60]
grades = sorted(grades)
print grades
</code></pre>
</section>
<section>
<h3>String Formatting Operators</h3>
<pre><code contenteditable class="python">
print 'To Do:'
to_dos = ['work', 'sleep', 'work']
for item in to_dos:
print '%s' %(item)
# zip()
name = 'Sally'
print 'Her name is {}'.format(name)
</code></pre>
</section>
<section>
<h3>Function Practice</h3>
<p class="copy--small box--small">Write a function to...</p>
<pre><code>
# ...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>Questions?</h3>
</section>
</div>
<footer>
<div class="copyright">
<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>