-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.html
445 lines (341 loc) · 9.09 KB
/
part2.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Python</title>
<meta name="description" content="Intro to Python Workshop for Iowa Tech Chicks">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/white.css" id="theme">
<!-- ITC theme -->
<link rel="stylesheet" href="css/itc.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="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 ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
</section>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<div class="slides">
<section>
<h1>Intro to Python Workshop</h1>
<p>
<small>
Presented by: <br><br>
Jennifer Reiber Kyle / <a href="http://twitter.com/jreiberkyl" target="blank">@jreiberkyle</a>
<br>and <br>
<a href="http://iowatechchicks.com" target="blank">Iowa Tech Chicks</a> / <a href="http://twitter.com/iowatechchicks" target="blank">@iowatechchicks</a>
</small>
</p>
<br>
<p><small>
<b>Slides Source:</b>
<a href="https://github.com/jreiberkyle/intro-to-python" target="blank">
https://github.com/jreiberkyle/intro-to-python
</a>
</small></p>
</section>
<!-- Slides are separated by newline + three dashes + newline, vertical slides identical but two dashes -->
<section data-markdown data-separator="^\n---\n$" data-separator-vertical="^\n--\n$">
<script type="text/template">
### Part 2: Advanced Control
- Loops
- Data Structures
- Hangman Game!
- Functions
- Modules
---
## Loops
- So far our code has run from top to bottom
- Programs usually need to do something over and over agin
- Computers don’t mind doing repetitive tasks
---
## While Loop
```python
#"while" something is true
#perform an action
while True:
print 'yep'
ret = raw_input('continue? (y/n)')
if 'y' == ret:
break
```
Try it out!
---
Code within the loop may invalidate the condition
```python
loop = True
while loop:
print 'Are we there yet?'
loop = False
print 'Done the loop'
```
Try it out!
---
To run a certain number of times we test a
variable and increment within the loop
```python
x = 1
while x < 10:
print x
x += 1
```
Try it out!
---
## Exercise
Print from 10 to 1
---
```python
x = 10
while x > 0:
print x
x -= 1
```
---
## Exercise
Guess a Number
- We’re going to write a game for the computer
- The computer will pick a number and we have to guess it
```
from random import rand
int x = randint(1,10)
```
- The user should know if they are too high or too low
- We know how to ask for input
- We know how to compare guesses
- We want to give the user multiple chances
---
```python
from random import randint
x = randint(1,10)
print "I'm thinking of a number between 1 and 10"
correct = False
while not correct:
guess = raw_input("Guess the number\n")
guess = int(guess)
if guess > x:
print 'Too high'
elif guess < x:
print 'Too low'
else:
print "You're right!"
correct = True
```
---
## Data Structures
- [Lists](https://docs.python.org/2/tutorial/introduction.html#lists)
- An ordered set of objects
- Lists can hold strings, integers, floats, booleans, and
anything else you can create
---
## Creating Lists
```python
mylist = [1,2,3,4]
print mylist #[1, 2, 3, 4]
companies = ['My Elephant Brain', 're:3D', 'Hacker You']
print companies
#['My Elephant Brain', 're:3D', 'Hacker You']
kitchen_sink = [1 , 'a', True]
print kitchen_sink
#[1, 'a', True]
```
---
## Accessing Items in a List
```
>>> companies = ['My Elephant Brain', 're:3D', 'Hacker You']
['My Elephant Brain', 're:3D', 'Hacker You']
>>> companies[1]
're:3D'
>>> companies[-1]
'Hacker You'
>>> companies[10]
Traceback (most recent call last): File "<stdin>",
line 1, in <module>
IndexError: list index out of range
```
---
## Exercise
- Using a list and a while loop, print the days of the week
- ‘break’ the loop on Wednesday
---
### Modifying a List
```python
l = []
#add an item
l.append(5)
print l
#remove an item
l.remove(3)
print l
# remove throws an error if the item does not exist
l.remove(9)
```
---
## A Quick jump Back to Loops
```python
# for loops make it easy to loop over a list
for company in companies:
print company
# they are also useful for looping
# a known number of times
for i in range(1,10):
print i
```
---
## Exercise
- Use a for loop to print the days of the week
- Use a for loop to count from 10 to 1
- Hint: by default `range()` increases by 1 each time, but you can give it a third
argument to tell it the number to increment by each time
- e.g. `range(0, 10, 2)`
---
## More data structures...
- Sets
- Like lists, but (1) unordered and (2) only store unique elements
- Tuples
- Like lists, but cannot change after creation
- Dictionaries
- Unordered list indexed by keys
```python
my_dict = {
'name': 'Jennifer Reiber Kyle',
'group': 'Iowa Tech Chicks'}
```
---
# Hangman Game!
(credit: Python Programming for the Absolute Beginner)
**Keep in Mind:**
- Before coding, think about what you’re trying to do.
- Computers need specific instructions, so you need to
break it down.
- You can start by playing hangman on paper with each other.
---
### Hint:
- Create a list of hangman 'states'
```python
HANGMAN = [
"""
------
| |
|
|
|
|
|
|
|
------
""",
...
]
```
---
### Hint:
- Create a list of words
- Pick the secret word:
- `random.choice(WORDS)`
- Track the number of wrong guesses
- Track the letters used
- Create a string that tracks the correct guesses
- One dash for each letter in the secret word:
- `so_far = "-" * len(word)`
- For each correct guess, place the letter in its correct spot
---
### Solution
[https://github.com/jreiberkyle/intro-to-python/blob/master/examples/hangman_soln.py](./examples/hangman_soln.py)
---
# Functions
We put reusable code inside 'functions'
```
def add_two(a, b):
return a + b
ans = add_two(1, 2)
print ans
```
---
## Exercise
Write functions to do the following
- Multiply two numbers
- Combine an int and a string
- Print the sequence of a given number, down to zero
- 5, 4, 3, 2, 1, 0
---
## Modules (local)
Python code is reusable by other code
functions.py
```
def add_two(a, b):
return a + b
```
start python in the same folder as functions.py
```
from functions import add_two
print add_two(1,2)
```
---
## Python Libraries
One of the greatest aspects of Python is the large number of libraries
Some useful libraries:
- numpy (used at Planet Labs to process images as arrays)
- scipy
- scikit-learn
---
### You’re programmers now!
- Sure there’s more to know
- There’s always more to know
- The trick is using what you know
- More Resources
- http://www.pyschools.com/
- http://www.djangobook.com/en/2.0/index.html
- http://learnpythonthehardway.org/
</script>
</section>
<section>
<h1>Thank You!</h1>
<br>
<p>Support Iowa Tech Chicks at <a href="http://www.iowatechchicks.com/support/">iowatechchicks.com/support</a></p>
<br>
<p>Connect with me on Twitter (@jreiberkyle) or Github (github/jreiberkyle)</p>
</section>
<footer>
<br>
</footer>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.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, condition: function() { return true; }, callback: function() { hljs.initHighlightingOnLoad(); } },
// { src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>