-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
593 lines (479 loc) · 19.3 KB
/
index.html
File metadata and controls
593 lines (479 loc) · 19.3 KB
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=1024, user-scalable=no">
<title>Python 101 — Gather Workshops</title>
<!-- Required stylesheet -->
<link rel="stylesheet" href="core/deck.core.css">
<!-- Extension CSS files go here. Remove or add as needed. -->
<link rel="stylesheet" href="extensions/goto/deck.goto.css">
<link rel="stylesheet" href="extensions/menu/deck.menu.css">
<link rel="stylesheet" href="extensions/navigation/deck.navigation.css">
<link rel="stylesheet" href="extensions/status/deck.status.css">
<link rel="stylesheet" href="extensions/hash/deck.hash.css">
<link rel="stylesheet" href="extensions/scale/deck.scale.css">
<!-- Style theme. More available in /themes/style/ or create your own. -->
<link rel="stylesheet" href="themes/style/gather.css">
<!-- Transition theme. More available in /themes/transition/ or create your own. -->
<link rel="stylesheet" href="themes/transition/horizontal-slide.css">
<script src="modernizr.custom.js"></script>
</head>
<body class="deck-container">
<!-- Begin slides. Just make elements with a class of slide. -->
<article class='slide title-slide'>
<h1 class="n-flow">Python 101</h1>
<h4>Presenting</h4>
<p><a href="http://twitter.com/amatix" target="_blank">@amatix</a> (Rob Coup)</p>
<!--
<h4>Mentors</h4>
<p><a href="http://twitter.com/tanya" target="_blank">@tanya</a> (Tanya Gray)</p>
-->
</article>
<article class='slide title-slide'>
<h1><img src="assets/python-logo.png" alt="Python" /></h1>
</article>
<article class='slide'>
<h2>Goals!</h2>
<ol>
<li>Set up Python on your computer</li>
<li>Self-directed tutorial (with help!)</li>
<li>Review</li>
<li>Learn some more</li>
<li>Practise</li>
</ol>
</article>
<article class='slide title-slide'>
<h1>Set up Python on your computer</h1>
</article>
<article class='slide'>
<h2>1. Set up Python</h2>
<p>We're going to use Python 2.7 for this workshop.</p>
<p>Python is available for:</p>
<ul>
<li>Windows <a href="https://openhatch.org/wiki/Boston_Python_Workshop_6/Friday/Windows_set_up_Python">bit.ly/1boQfxe</a></li>
<li>Mac OS X <a href="https://openhatch.org/wiki/Boston_Python_Workshop_6/Friday/OSX_set_up_Python">bit.ly/1eZgdfc</a></li>
<li>Linux <a href="https://openhatch.org/wiki/Boston_Python_Workshop_6/Friday/Linux_set_up_Python">bit.ly/1dcCxR2</a></li>
</ul>
<p>Get from <a href="http://www.python.org">python.org</a>.</p>
<div class="protip"><strong>PRO TIP:</strong> Python 2 and Python 3 are <em>not</em> the same - pick one and stick to it!</div>
</article>
<article class='slide'>
<h2>2. Prepare a Text Editor</h2>
<p>Install a good text editor for writing and saving Python code during the workshop.</p>
<p>Recommendations: </p>
<ul>
<li>Windows: Notepad++ <a href="https://openhatch.org/wiki/Boston_Python_Workshop_6/Friday/Windows_text_editor">bit.ly/1gPohQF</a></li>
<li>Mac OS X: Smultron <a href="https://openhatch.org/wiki/Boston_Python_Workshop_6/Friday/OSX_text_editor">bit.ly/1boQvwi</a></li>
<li>Linux: Gedit <a href="https://openhatch.org/wiki/Boston_Python_Workshop_6/Friday/Linux_text_editor">bit.ly/J2EVkz</a></li>
</ul>
<div class="protip"><strong>PRO TIP:</strong> You need to set your editor to use <em>spaces</em> to indent.</div>
</article>
<article class='slide'>
<h2>3. Starting and exiting Python</h2>
<p>Open a command prompt:
<ul>
<li>Windows: Start... Search... <code>cmd</code></li>
<li>Mac OS X: Start the 'Terminal' application</li>
</ul>
<p>To start Python, type <code>python</code>. You should see something like:
<pre>Python 2.7.1 (r271:86832, ...) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>></pre>
<p>Exit the Python prompt by typing <code>exit()</code>. Now you're back at the command prompt.</p>
</article>
<article class='slide'>
<h2>4. Running Python code from a file</h2>
<p>Start your text editor.</p>
<p>Add the following to a new, blank text file:</p>
<pre>print "Hello World!"</pre>
<p>Save the file as <code>hello.py</code> in your Desktop directory. The <code>.py</code> extension indicates the file contains Python code.</p>
<p>Start a command prompt and <code>cd Desktop</code> to move to the directory where we saved your file.</p>
<p><code>python hello.py</code> should show:</p>
<pre>Hello World!</pre>
</article>
<article class='slide slide-subhead title-slide'>
<div class="vcenter">
<h1>Self-directed Tutorial</h1>
<h3>With help! 60-90 minutes</h3>
<h3><a href="https://github.com/nzgather/python-101-workshop/blob/master/resources/tutorial.md">bit.ly/IKqqAS</a></h3>
</div>
</article>
<article class='slide title-slide'>
<h1>Review</h1>
</article>
<article class='slide'>
<h2>Maths</h2>
<ul>
<li>Addition <code>+</code></li>
<li>Subtraction <code>-</code></li>
<li>Multiplication <code>*</code></li>
<li>Division <code>/</code>
<pre>1 / 2
1.0 / 2
3/4 + 1/4
3.0/4 + 1.0/4</pre></li>
<li>Order of operations <code>2 * (3 + 1)</code></li>
</ul>
</article>
<article class='slide'>
<h2>Types and Functions</h2>
<p>Find what data type something is using <code>type()</code></p>
<p>Functions make work re-usable</p>
<ul>
<li>Function name</li>
<li>Input "arguments"</li>
<li>Output "return value"</li>
</ul>
</article>
<article class='slide'>
<h2>Variables</h2>
<p>Just like maths, except any data type.</p>
<pre>type(4)
x = 4
x
type(x)
2 * x</pre>
<p>Use descriptive variable names.</p>
<div class="protip"><strong>PRO TIP:</strong> Pick <em>one</em> of <code>snake_case_naming</code> or <code>camelCaseNaming</code>.</div>
</article>
<article class='slide'>
<h2>Strings</h2>
<p>Are text, surrounded by balanced quotes (<code>"</code> or <code>'</code>)</p>
<pre>type("Hello")
type(1)
type("1")</pre>
<p>Concatenate strings with <code>"Hello" + "World"</code></p>
<p>Print strings with <code>print "Hello"</code></p>
<p>Convert other types to strings using <code>str("Hello")</code></p>
<p>Find out how long a string is with <code>len("Hello")</code></p>
<p>Multiply strings with <code>"Hello" * 5</code></p>
</article>
<article class='slide'>
<h2>Strings</h2>
<p>Escape quotes with <code>'I\'m a happy camper'</code></p>
<p><code>""</code> is an empty string.</p>
<p>To get a new line use <code>print "One\nTwo"</code>
<p>For multi-line strings you can use:
<pre>"""I am
a long
string"""</pre></p>
</article>
<article class='slide'>
<h2>Booleans</h2>
<p><code>True</code> and <code>False</code></p>
<p>Test equality using <code>==</code></p>
<div class="protip"><strong>IMPORTANT:</strong> <code>=</code> is assignment, <code>==</code> is comparison</div>
<p>Other comparisions: <code>< <= != >= ></code></p>
<p>Test for containment:
<pre>"H" in "Hello"
"W" not in "World"</pre>
</article>
<article class='slide'>
<h2>Flow control</h2>
<p>Use <code>if</code> statements to make decisions and conditionally execute code.</p>
<pre>colour = "orange"
if colour == "green" or colour == "red":
print "Christmas colour!"
elif colour == "black" or colour == "orange":
print "Halloween colour!"
elif colour == "pink":
print "Valentine's Day colour!"
else:
print "Some other colour!"
</pre>
<div class="protip"><strong>PRO TIP:</strong> Use the <code>tab</code> key to indent code blocks <strong>4</strong> spaces.</div>
</article>
<article class='slide'>
<h2>Flow control</h2>
<p>Logically combine two expressions</p>
<p>With <code>and</code>, <em>both</em> must be True</p>
<p>With <code>or</code>, <em>either</em> can be True</p>
<pre>temperature = 29
if temperature > 15 and temperature < 25:
print "It's nice and cozy in here!"
else:
print "Too extreme for me."
</pre>
</article>
<article class='slide'>
<h2>Writing functions</h2>
<pre>>>> def add(x, y):
... result = x + y
... return result
...
print add(1234, 5678)</pre>
<p>Remember to indent the code block inside your function.</p>
<p>Functions don't need to have arguments, or need to return a value.</p>
</article>
<article class='slide title-slide'>
<h1>New Stuff</h1>
</article>
<article class='slide title-slide'>
<h1>Lists <code>[]</code></h1>
</article>
<article class='slide'>
<h2>Lists</h2>
<p>Store data where order matters.</p>
<p>Indexed starting with <em>0</em>.</p>
<pre>>>> my_list = []
>>> my_list
[]
>>> your_list = ["a", "b", "c", 1, 2, 3]
>>> your_list
['a', 'b', 'c', 1, 2, 3]</pre>
</article>
<article class='slide'>
<h2>Access and adding elements</h2>
<p><code>my_list.append('new')</code> to add to the end.</p>
<p><code>my_list.insert(0, 'new')</code> to insert at the start.</p>
<p><code>my_list[0] = 'new'</code> to change the first element.</p>
<p><code>len(my_list)</code> to get the length.</p>
<p><code>'new' in my_list</code> to check containment.</p>
</article>
<article class='slide'>
<h2>Access and Slicing</h2>
<p><code>my_list[0]</code> to get the first element.</p>
<p><code>my_list[-1]</code> to get the last element.</p>
<p><code>my_list[:3]</code> to get the first 3 elements.</p>
<p><code>my_list[3:]</code> to get everything <em>after</em> the first 3 elements.</p>
</article>
<article class='slide'>
<h2>Strings are a lot like Lists</h2>
<pre>>>> my_string = "Hello World"
>>> my_string[0]
'H'
>>> my_string[:5]
'Hello'
>>> my_string[6:]
'World'
>>> my_string = my_string[:6] + "Jessica"
>>> my_string
'Hello Jessica'</pre>
<p>Big difference is you can't <em>change</em> strings.</p>
</article>
<article class='slide title-slide'>
<h1>Loops and more flow control</h1>
</article>
<article class='slide'>
<h2>For loops</h2>
<p>Use a <code>for</code> loop to do something to every element in a list.</p>
<pre>>>> names = ["Jessica", "Adam", "Liz"]
>>> for name in names:
... print name
...
Jessica
Adam
Liz</pre>
</article>
<article class='slide'>
<h2><code>if</code> statements inside <code>for</code> loops</h2>
<p>Use to build up a list.</p>
<pre>>>> vowel_names = []
>>> for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]:
... if name[0] in "AEIOU":
... vowel_names.append(name)
...
>>> print vowel_names
['Alice', 'Ellen']</pre>
</article>
<article class='slide'>
<h2><code>for</code> loops inside <code>for</code> loops</h2>
<pre>>>> letters = ["a", "b", "c"]
>>> numbers = [1, 2, 3]
>>> for letter in letters:
... for number in numbers:
... print letter * number
...
a
aa
aaa
b
bb
bbb
c
cc
ccc</pre>
<p>Order of your loops matters!</p>
</article>
<article class='slide'>
<h2>Useful list and loop functions</h2>
<p><code>my_list.sort()</code> to sort a list.</p>
<p><code>min(my_list)</code> and <code>max(my_list)</code> to get extremes.</p>
<p><code>range()</code> to generate a range of numbers:
<pre>>>> range(5)
[0, 1, 2, 3, 4]</pre></p>
</article>
<article class='slide'>
<h2><code>while</code> loops</h2>
<p>Keep looping as long as a condition is <code>True</code>.</p>
<pre>>>> i = 0
>>> while i < 5:
... print i
... i = i + 1
...
0
1
2
3
4</pre>
</article>
<article class='slide'>
<h2><code>break</code></h2>
<p>Use <code>break</code> to break out of a <code>for</code> or <code>while</code> loop early.</p>
<pre>>>> i = 0
>>> while True:
... print i
... i = i + 1
... if i > 3:
... break
0
1
2
3</pre>
</article>
<article class='slide'>
<h2>Get user input</h2>
<p>Use <code>raw_input()</code> to prompt the user for a string.</p>
<pre>>>> input = raw_input("Please type something? ")
>>> print "You said: " + input
Please type something? Hello
You said: Hello
</pre>
</article>
<article class='slide title-slide'>
<h1>Dictionaries <code>{}</code></h1>
</article>
<article class='slide'>
<h2>Dictionaries</h2>
<p>Use dictionaries to store key/value pairs.</p>
<p>Dictionaries do not guarantee ordering.</p>
<p>A given key can only have one value, but multiple keys can have the same value.</p>
<pre>>>> my_dict = {}
>>> my_dict
{}
>>> your_dict = {"Alice" : "chocolate", "Bob" : "strawberry", "Cara" : "mint chip"}
>>> your_dict
{'Bob': 'strawberry', 'Cara': 'mint chip', 'Alice': 'chocolate'}</pre>
</article>
<article class='slide'>
<h2>Access and adding elements</h2>
<p><code>your_dict["Alice"]</code> to get the value for a key.</p>
<p><code>your_dict["Dora"] = "vanilla"</code> to set or replace a key & value.</p>
<p><code>'Alice' in your_dict</code> to check containment.</p>
<p><code>len(your_dict)</code> to get the number of items.</p>
<p><code>your_dict.get("Eve")</code> to get the value for a key, without throwing an error if it doesn't exist.</p>
</article>
<article class='slide title-slide'>
<h1>Modules</h1>
</article>
<article class='slide'>
<h2>Modules</h2>
<p>Don't write it twice! Modules allow us to organise and re-use code.</p>
<p>Don't write it at all! Python has 'batteries included', and there are thousands of modules for every conceivable purpose.</p>
<p>Each <code>.py</code> file is a module.</p>
</article>
<article class='slide'>
<h2>Imports</h2>
<p>Use <code>import</code> to access a module.</p>
<pre>>>> import random
>>> random.randint(1, 10)
5
>>> random.choice(["red", "green", "blue", "yellow"])
'green'</pre>
<p><code>len()</code>, <code>type()</code>, <code>min()</code>, <code>max()</code>, <code>range()</code>, <code>str()</code> are all "builtins" and don't need importing.</p>
</article>
<article class='slide slide-subhead title-slide'>
<div class="vcenter">
<h1>NZ Regions</h1>
<h3><a href="https://github.com/nzgather/python-101-workshop/blob/master/resources/nz_regions.py">nz_regions.py</a></h3>
</div>
</article>
<article class='slide title-slide'>
<h1>Practise</h1>
</article>
<article class='slide'>
<h2>Practise</h2>
<p>CodingBat is a little different from using Python at the command line or in a text editor like we've been doing. When you use CodingBat, you type your code into a web page and click 'Go' when you want that code to run. You'll still need to make sure you indent all your code to the same level.
For each CodingBat question, you will write a function. CodingBat will run your function with a few different inputs, and will compare the output of the function you wrote to what it knows is the correct answer. If all the outputs are correct for all the inputs, you've written the function correctly!
We like CodingBat because it gives you immediate feedback on how close your function is to being correct.
<p>CodingBat is a useful site exercises: <a href="http://codingbat.com/home/bostonpythonworkshop@gmail.com/Saturday">bit.ly/Iz7zJA</a></h5>
<p>Data Types cheat sheet: <a href="https://github.com/nzgather/python-101-workshop/blob/master/resources/data_types.md">bit.ly/1gQb6z2</a></p>
<p>Python Loops cheat sheet: <a href="https://github.com/nzgather/python-101-workshop/blob/master/resources/loops.md">bit.ly/1iN6XQL</a></p>
</article>
<article class='slide title-slide'>
<h1>What next?</h1>
</article>
<article class='slide'>
<h2>Keep Learning</h2>
<p>Think Python: <a href="http://openbookproject.net/thinkcs/python/english2e/">openbookproject.net/thinkcs/python/english2e/</a></p>
<p>Learn Python the Hard Way: <a href="http://learnpythonthehardway.org">learnpythonthehardway.org</a></p>
<p>Learn Python with Games: <a href="http://inventwithpython.com/chapters/">inventwithpython.com/chapters/</a></p>
<p>The official Python tutorial: <a href="http://docs.python.org/tutorial/">docs.python.org/tutorial/</a></p>
</article>
<article class='slide'>
<h2>Using Python</h2>
<ul>
<li>do data analysis and plotting</li>
<li>make a website</li>
<li>write games</li>
<li>social media</li>
<li>make and remix music</li>
<li>homework</li>
<li>participate in space exploration!</li>
<li>analyze census data!</li>
<li>help your community!</li>
<li>save the world!</li>
</ul>
</article>
<article class='slide'>
<h2>Local Pythonistas</h2>
<p>NZ Python Users Group has monthly meetups: <a href="http://nzpug.org/">nzpug.org</a></p>
<p>Kiwi PyCon is on every year: <a href="http://nz.pycon.org">nz.pycon.org</a></p>
</article>
<article class='slide'>
<h2>Thank you!</h2>
<p>The content for this workshop was adapted from the <a href="https://openhatch.org/wiki/Boston_Python_Workshop_6">Boston Python Workshop</a> (CC-BY) by <a href="https://github.com/organizations/nzgather">Gather</a> to be used with New Zealand students.</p>
<p>Visit <a href="http://gathergather.co.nz/community">Gather Community</a> for help from our mentors after today!</p>
<p>This workshop was made possible by <a href="http://internetnz.org.nz">InternetNZ</a></p>
</article>
<!-- End slides. -->
<!-- Begin extension snippets. Add or remove as needed. -->
<!-- deck.navigation snippet -->
<a href="#" class="deck-prev-link" title="Previous">←</a>
<a href="#" class="deck-next-link" title="Next">→</a>
<!-- deck.status snippet -->
<p class="deck-status">
<span class="deck-status-current"></span>
/
<span class="deck-status-total"></span>
</p>
<!-- deck.goto snippet -->
<form action="." method="get" class="goto-form">
<label for="goto-slide">Go to slide:</label>
<input type="text" name="slidenum" id="goto-slide" list="goto-datalist">
<datalist id="goto-datalist"></datalist>
<input type="submit" value="Go">
</form>
<!-- deck.hash snippet -->
<a href="." title="Permalink to this slide" class="deck-permalink">#</a>
<!-- End extension snippets. -->
<!-- Required JS files. -->
<script src="jquery-1.7.2.min.js"></script>
<script src="core/deck.core.js"></script>
<!-- Extension JS files. Add or remove as needed. -->
<script src="extensions/hash/deck.hash.js"></script>
<script src="extensions/menu/deck.menu.js"></script>
<script src="extensions/goto/deck.goto.js"></script>
<script src="extensions/status/deck.status.js"></script>
<script src="extensions/navigation/deck.navigation.js"></script>
<script src="extensions/scale/deck.scale.js"></script>
<!-- Initialize the deck. You can put this in an external file if desired. -->
<script>
$(function() {
$.deck('.slide');
});
</script>
</body>
</html>