forked from calebsmith/gdi-intro-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class4.html
427 lines (365 loc) · 18.8 KB
/
class4.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Python ~ Girl Develop It</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="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/gdicool.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor<link rel="stylesheet" href="lib/css/light.css">-->
<!-- dark editor--><link rel="stylesheet" href="reveal/lib/css/dark.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[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">
<h3>Intro to Python</h3>
<h4>Class 4</h4>
</section>
<section>
<h3>Review</h3>
<ul>
<li>Method calls</li>
<li>Combining lists and dictionaries</li>
<li>Builtins for collections</li>
</ul>
</section>
<!-- Block 1 - Intro to Classes and OOP - 20 minutes -->
<section>
<h3>Functions on Dictionaries</h3>
<p></p>
<pre><code contenteditable class="fragment python">
character = {
'x': 10,
'y': 20,
'health': 100,
}
def injure(character, damage):
character['health'] = character['health'] - damage
if character['health'] < 0:
character['health'] = 0
def heal(character, amount):
character['health'] = character['health'] + amount
if character['health'] > 100:
character['health'] = 100
</code></pre>
</section>
<section>
<h3>Classes</h3>
<p>A <strong>class</strong> creates a new type of object.</p>
<p class="fragment">A class defines the attributes and methods of objects of that type</p>
<p class="fragment">Classes are used to create new objects of that type</p>
<pre><code contenteditable class="fragment python">
class Character():
def __init__(self, x, y, health):
self.x = x
self.y = y
self.health = health
character = Character(10, 20, 100)
</code></pre>
</section>
<section>
<h3>A Sense of Self</h3>
<p>The first argument to every method is <strong>self</strong>.</p>
<p class="fragment">self contains the attributes and methods for the current object</p>
<pre><code contenteditable class="fragment python">
class Character():
def __init__(self, x, y, health):
self.x = x
self.y = y
self.health = health
character = Character(10, 20, 100)
</code></pre>
</section>
<section>
<h3>The __init__ Method</h3>
<p>This method defines what the class should do when creating a new object.</p>
<pre><code contenteditable class="fragment python">
class Character():
def __init__(self, x, y, health):
self.x = x
self.y = y
self.health = health
character_a = Character(10, 20, 100)
character_b = Character(10, 20, 100)
</code></pre>
<p class="fragment">To create a new Character, the syntax looks like a function call. These arguments are passed to the __init__ method</p>
</section>
<section>
<h3>Class Methods</h3>
<p>A class also defines <strong>methods</strong>, which are functions that operate on objects of that type</p>
<p class="fragment">Assigning values to an attribute on self is how we <strong>mutate</strong> the object's state.</p>
<pre><code contenteditable class="fragment python">
# inside the character class
def heal(self, amount):
self.health = self.health + amount
if self.health > 100:
self.health = 100
def injure(self, amount):
self.health = self.health - amount
if self.health < 0:
self.health = 0
character = Character(10, 20, 100)
character.injure(10)
</code></pre>
</section>
<!-- Let's develop it: 10 minutes -->
<section>
<h3>Let's Develop It</h3>
<ul>
<li>In your text editor, create your own class with an __init__ method, and at least one other method.</li>
<li>Open a Python shell and import the class. Create one or more objects from the class</li>
<li>If time allows, create a function that creates objects from your class, calls a method, and prints one of its attributes</li>
<li>Use the next slide as an example</li>
</ul>
</section>
<section>
<pre><code contenteditable class="python">
# in character.py
class Character():
def __init__(self, x, y, health):
self.x = x
self.y = y
self.health = health
def heal(self, amount):
self.health = self.health + amount
if self.health > 100:
self.health = 100
</code></pre>
<pre><code contenteditable class="python">
# in Python shell
from character import Character
character_a = Character(10, 20, 100)
character_a.injure(10)
print("character health is: " + character_a.health)
</code></pre>
</section>
<!-- Block 2 - Inheritance and Composition 20 Minutes -->
<section>
<h3>Inheritance</h3>
<p>A class can <strong>inherit</strong> from another class.</p>
<p class="fragment">A class that inherits from another is called the "child class" and obtains the methods and attributes of its "parent"</p>
<pre><code contenteditable class="fragment python">
class Mobile(object):
"""
An object with an x, y position, and methods for moving
"""
def __init__(self, x, y):
self.x = x
self.y = y
def move_up():
self.y = self.y - 1
# ... methods for move_down, move_left, and move_right
</code></pre>
</section>
<section>
<h3>Inheritance Continued</h3>
<p>The move_up method is <strong>overridden</strong> in the child class below:</p>
<pre><code contenteditable class="fragment python">
class BoundedMobile(Mobile):
"""
An object with an x, y position, and methods for moving
The x, y position must be within bounds
"""
def move_up():
self.y = self.y - 1
if self.y < 0:
self.y = 0
</code></pre>
<p class="fragment">See <a href="http://nicklang.com/gdi-intro-python/examples/mobile.py">mobile.py</a> for a more complete example.</p>
</section>
<section>
<h3>What's Super about Super</h3>
<p><strong>super</strong> is often helpful when writing methods that override the method of the parent class</p>
<pre><code contenteditable class="fragment python">
class BoundedMobile(Mobile):
"""
An object with an x, y position, and methods for moving
The x, y position must be within bounds
"""
def move_up():
super(BoundedMobile, self).move_up()
if self.y < 0:
self.y = 0
</code></pre>
<p class="fragment">The call to super() takes the name of the child class, followed by self. This is followed by the method call and any arguments to pass to it</p>
</section>
<section>
<h3>Composition</h3>
<p>Classes can also use the technique of <strong>composition</strong></p>
<p class="fragment">This simply means that a given object contains other objects within it.</p>
<p class="fragment">This often leads to a clearer and simpler design</p>
<pre><code contenteditable class="fragment python">
class Grid(object):
def __init__(self, x_limit, y_limit):
self.x_limit = x_limit
self.y_limit = y_limit
self.mobiles = []
def add_mobile(self, x, y):
mob = BoundedMobile(x, y, self.x_limit, self.y_limit)
mobs = self.mobiles.get((x, y), [])
mobs.append(mob)
self.mobiles[(x, y)] = mobs
</code></pre>
</section>
<section>
<h3>Composition Continued</h3>
<p>Given the class on the previous slide, the following code creates mobiles within the grid object. (Complete code is available in the aforementioned <a href="http://nicklang.com/gdi-intro-python/examples/mobile.py">mobile.py</a> file.)</p>
<pre><code contenteditable class="fragment python">
from mobile import Grid
grid = Grid(7, 7)
grid.add_mobile(1, 2)
grid.add_mobile(0, 1)
grid.add_mobile(0, 1)
grid.display_grid()
</code></pre>
</section>
<!-- Let's develop it: 10 minutes -->
<section>
<h3>Let's Develop It</h3>
<p>Create a class that uses inheritance, composition, or both.</p>
<p>To help you, use your work from the last exercise or the classes from <a href="http://nicklang.com/gdi-intro-python/examples/mobile.py">mobile.py</a></p>
</section>
<section>
<h3>Higher order functions</h3>
<p>A <strong>higher order function</strong> is a function that returns a function, takes a function as an argument, or both</p>
<p class="fragment">One commonly used higher order function that is a Python builtin is called <strong>map</strong></p>
<pre><code contenteditable class="fragment python">
# Define any function
def sqaure(number):
return number ** 2
# Pass the function to map along with an iterable
squares = map(square, range(10))
</code></pre>
<p class="fragment">N.B. - map has performance problems for large data sets and should only be used when the data set is well defined and somewhat small.</section>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Choose among any of these projects (Resources available on the next page):</p>
<ul>
<li>Search the Web - Write a program that searches the web using DuckDuckGo and displays results.
</li>
<li>Encryption - Write a program that encrypts a string from user input, or file and is able to decrypt it as well.
</li>
<li>Command Line Game - Create a simple game that runs inside the terminal.
</li>
</section>
<section>
<h3>Let's Develop It Resources</h3>
<table>
<tr>
<td>Search the Web</td>
<td><a href="https://github.com/crazedpsyc/python-duckduckgo/">python-duckduckgo</a> library to get started. Download duckduckgo.py and put it in the same directory as your code. Use the query() function it provides to begin. (HINT: Results are often empty, but 'related' list usually has a few hits.)</td>
</tr>
<tr>
<td>Encryption</td>
<td>Read about the <a href="https://en.wikipedia.org/wiki/Caesar_cipher">Caesar Cipher</a> or find a similarly simple encryption mechanism online. You should find the ord() and chr() functions helpful, as well as the modulus operator '%'</td>
</tr>
</table>
<p>continued on next page...</p>
</section>
<section>
<h3>Let's Develop It Resources Continued</h3>
<table>
<tr>
<td>Command Line Game</td>
<td>This might be a text adventure with paragraphs of text followed by a series of choices for the user. A choice maps to another node in the story (another paragraph with choices). You might try storing the paragraphs separately in a text file. The format might be something different, such as a series of "rooms", each with a description, for the user to explore by entering commands such as "go west". Examples of these kinds of games are <a href="https://en.wikipedia.org/wiki/Colossal_Cave_Adventure">Colossal Cave Adventure</a> and <a href="https://en.wikipedia.org/wiki/Zor">Zork</a></td>
</tr>
</table>
</section>
<section>
<h3>Future Resources</h3>
<p></p>
<table>
<tr>
<td><a href="http://docs.python.org/2/">Python.org Documentation</a></td>
<td>Official Python Documentation</td>
</tr>
<tr>
<td><a href="http://www.greenteapress.com/thinkpython/html/index.html">Think Python</a></td>
<td>Online and print book with exercises.</td>
</tr>
<tr>
<td><a href="http://learnpythonthehardway.org/book/">Learn Python the Hard Way</a></td>
<td>Online and print book with exercises</td>
</tr>
<tr>
<td><a href="https://developers.google.com/edu/python/">Google's Python Class</a></td>
<td>Video lectures coupled with exercises</td>
</tr>
<tr>
<td><a href="http://newcoder.io/tutorials/">New Coder</a></td>
<td>Ideas for slightly larger projects and resources to get you started. Projects include accessing API's, scraping pages, writing IRC bots, and others.</td>
</tr>
<tr>
<td><a href="http://girldevelopit.com/">Girl Develop It</a></td>
<td>Local workshops, events, and coding sessions</td>
</tr>
</table>
</section>
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Upcoming Events</h3>
<div class="left" style="width: 45%;font-size: 80%;">
<strong class="green">Core Team Planning session</strong>
<p>Feb. 6th, 6pm - 8pm</p>
<p style="color:#999">InspiringApps</p>
<strong class="blue">Intro to Servers</strong>
<p>Feb. 8th & 9th, 1pm - 5pm</p>
<p style="color:#999">Simple Energy</p>
</div>
<div class="right" style="width: 45%;font-size: 80%;">
<strong class="green">Code & Coffee - Boulder</strong>
<p>Feb. 15th, 10am - 1pm</p>
<p style="color:#999">The Cup</p>
<strong class="blue">Intermediate Git & GitHub</strong>
<p>Feb. 24th & 9th, 6:3pm - 9pm</p>
<p style="color:#999">Simple Energy</p>
</div>
</section>
</div>
<footer>
<div class="copyright">
Intro to Python ~ Girl Develop It ~
<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="reveal/lib/js/head.min.js"></script>
<script src="reveal/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,
theme: Reveal.getQueryHash().theme, // available themes are in /css/themes
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>