-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
1237 lines (1097 loc) · 39 KB
/
index.html
File metadata and controls
1237 lines (1097 loc) · 39 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>Python</title>
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:200,300,400,500,600,700,900' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Merriweather:400,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
<style type="text/css">
/* Color Palette Based on Monokai
body : #191919, #fafafa
blockquote : #272822, #eeeeee
links : #66d9ef
pre : #272822, #75715e, #dddddd, #f92672,
#a6e22e, #66d9ef, #bf79db
*/
/* Theme styles */
body {
font-family: 'Open Sans', sans-serif;
color: #fafafa;
background-color: #191919;
font-size: 100%;
}
/* custom slide styles */
/* fade.jpg, wireballblack.png, space.png, snow2.jpg, pinstripe_rain.png, orion.jpg, nodes.jpg */
#slide-1 {
background: #222 url(wireball.png) center center no-repeat;
background-size: cover;
}
#slide-1 h1 {
text-shadow: 1px 2px 4px #191919;
}
#slide-1 p {
text-shadow: 1px 1px 1px #191919;
}
.center {
text-align: center;
}
img.badge {
height: 128px;
width: 128px !important;
border-radius: 50% !important;
}
/* presentation styles */
.slide {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
padding: 100px;
}
.slide-content {
width: 100%;
height: 100%;
overflow: hidden;
margin: 0 auto;
padding: 0;
font-weight: 200;
font-size: 200%;
line-height: 1.375;
}
.slide-content img {
border-radius: 3px;
width: 100%;
}
.controls {
position: absolute;
bottom: 10px;
left: 10px;
}
.arrow {
width: 0; height: 0;
border: 15px solid #272822;
float: left;
margin-right: 15px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.prev {
border-top-color: transparent;
border-bottom-color: transparent;
border-left-color: transparent;
border-left-width: 0;
border-right-width: 25px;
}
.next {
border-top-color: transparent;
border-bottom-color: transparent;
border-right-color: transparent;
border-left-width: 25px;
border-right-width: 0;
}
.prev:hover {
border-right-color: #eee;
cursor: pointer;
}
.next:hover {
border-left-color: #eee;
cursor: pointer;
}
h1, h2, h3 {
font-family: 'Merriweather', serif;
}
h1 {
font-size: 300%;
line-height: 1.2;
text-align: center;
margin: 170px 0 0;
}
h2 {
font-size: 100%;
line-height: 1.2;
margin: 5px 0;
text-align: center;
font-weight: 200;
}
h3 {
font-size: 140%;
line-height: 1.2;
border-bottom: 2px ridge #fafafa;
margin: 0;
padding-bottom: 15px;
}
ul {
padding: 20px 0 0 60px;
font-weight: 200;
line-height: 1.375;
}
blockquote {
background: #272822;
margin-left: 0;
padding-left: 20px;
border-left: 5px solid #eee;
border-radius: 0 3px 3px 0;
}
blockquote p {
color: #eee;
padding: 0.5em;
}
.left {
float: left;
width: 48%;
}
.right {
float: right;
width: 48%;
}
.author h1 {
font-size: 170%;
font-weight: 200;
text-align: center;
margin-bottom: 30px;
}
.author h3 {
font-weight: 100;
text-align: center;
font-size: 95%;
border: none;
}
.author h3.twitter::before {
content: 'Twitter: '
}
.author h3.github::before {
content: 'Github: '
}
.author h3.email::before {
content: 'Email: '
}
.author h3.url::before {
content: 'Web: '
}
a {
text-decoration: none;
color: #66D9EF;
}
a:hover {
color: #66D9EF;
border-bottom: 1px solid #66D9EF;
}
pre {
font-size: 75%;
line-height: 1.3;
}
table td, table th {
padding-right: 3em;
text-align: left;
}
.progress {
position: fixed;
top: 0; left: 0; right: 0;
height: 3px;
}
.progress-bar {
width: 0%;
height: 3px;
background-color: #A6E22E;
-webkit-transition: width 0.15s ease-out;
-moz-transition: width 0.15s ease-out;
-o-transition: width 0.15s ease-out;
transition: width 0.15s ease-out;
}
.hidden {
display: none;
}
/* code highlighting */
code {
background-color: #272822;
border-radius: 3px;
padding: 0.1em 0.35em;
}
pre code {
display: block;
font-family: 'Source Code Pro';
font-weight: 400;
padding: 0.5em;
background: #272822;
overflow-y: hidden;
}
pre .tag,
pre .tag .title,
pre .keyword,
pre .literal,
pre .change,
pre .winutils,
pre .flow,
pre .lisp .title,
pre .clojure .built_in,
pre .nginx .title,
pre .tex .special {
color: #F92672;
}
pre code {
color: #DDD;
}
pre code .constant {
color: #66D9EF;
}
pre .class .title {
color: white;
}
pre .attribute,
pre .symbol,
pre .symbol .string,
pre .value,
pre .regexp {
color: #BF79DB;
}
pre .tag .value,
pre .string,
pre .subst,
pre .title,
pre .haskell .type,
pre .preprocessor,
pre .ruby .class .parent,
pre .built_in,
pre .sql .aggregate,
pre .django .template_tag,
pre .django .variable,
pre .smalltalk .class,
pre .javadoc,
pre .django .filter .argument,
pre .smalltalk .localvars,
pre .smalltalk .array,
pre .attr_selector,
pre .pseudo,
pre .addition,
pre .stream,
pre .envvar,
pre .apache .tag,
pre .apache .cbracket,
pre .tex .command,
pre .prompt {
color: #A6E22E;
}
pre .comment,
pre .java .annotation,
pre .python .decorator,
pre .template_comment,
pre .pi,
pre .doctype,
pre .deletion,
pre .shebang,
pre .apache .sqbracket,
pre .tex .formula {
color: #75715E;
}
pre .keyword,
pre .literal,
pre .css .id,
pre .phpdoc,
pre .title,
pre .haskell .type,
pre .vbscript .built_in,
pre .sql .aggregate,
pre .rsl .built_in,
pre .smalltalk .class,
pre .diff .header,
pre .chunk,
pre .winutils,
pre .bash .variable,
pre .apache .tag,
pre .tex .special,
pre .request,
pre .status {
font-weight: 600;
}
pre .coffeescript .javascript,
pre .javascript .xml,
pre .tex .formula,
pre .xml .javascript,
pre .xml .vbscript,
pre .xml .css,
pre .xml .cdata {
opacity: 0.5;
}
/* responsiveness */
@media (max-width: 850px) {
body {
font-size: 70%;
}
.slide-content {
width: auto;
}
img {
border-radius: 3px;
width: 100%;
}
h1 {
margin-top: 120px;
}
.prev, .prev:hover {
border-right-color: rgba(135, 135, 135, 0.5);
}
.next, .next:hover {
border-left-color: rgba(135, 135, 135, 0.5);
}
}
@media (max-width: 480px) {
body {
font-size: 50%;
overflow: hidden;
}
.slide-content {
padding: 10px;
margin-top: 10px;
height: 340px;
}
h1 {
margin-top: 50px;
}
ul {
padding-left: 25px;
}
}
@media print {
* {
-webkit-print-color-adjust: exact;
}
@page {
size: letter;
}
.hidden {
display: inline;
}
html {
width: 100%;
height: 100%;
overflow: visible;
}
body {
margin: 0 auto !important;
border: 0;
padding: 0;
float: none !important;
overflow: visible;
background: none !important;
font-size: 52%;
}
.progress, .controls {
display: none;
}
.slide {
position: static;
}
.slide-content {
border: 1px solid #222;
margin-top: 0;
margin-bottom: 40px;
height: 3.5in;
overflow: visible;
}
.slide:nth-child(even) {
/* 2 slides per page */
page-break-before: always;
}
}
</style>
</head>
<body>
<div class="progress">
<div class="progress-bar"></div>
</div>
<div class="slide" id="slide-1">
<section class="slide-content"><h1 id="python-basics">Python Basics</h1>
<h2 id="a-quick-primer-on-the-scripting-language-">A quick primer on the scripting language.</h2>
<p><em>Generated using <a href="https://github.com/jdan/cleaver">cleaver</a></em></p>
</section>
</div>
<div class="slide hidden" id="slide-2">
<section class="slide-content"><ul>
<li>Introduction to Python</li>
<li>Control Structures</li>
<li>Basic Data Types</li>
<li>Advanced Data Types</li>
<li>Dictionaries</li>
<li>Functions</li>
<li>Exception Handling</li>
<li>Classes</li>
<li>Files</li>
<li>Larger Programs</li>
<li>Pattern Matching</li>
</ul>
</section>
</div>
<div class="slide hidden" id="slide-3">
<section class="slide-content"><h3 id="introduction-to-python">Introduction to Python</h3>
<ul>
<li>General purpose high level scripting language:</li>
<li>Emphasis placed on<ul>
<li>Human Readability.</li>
<li>Self describing code.</li>
</ul>
</li>
</ul>
<p><em>from PEP-20 - The Zen of Python</em>:</p>
<pre><code> Beautiful is better than ugly
Explicit is better than implicit
Simple is better than complex
Complex is better than complicated
Readability counts</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-4">
<section class="slide-content"><h3 id="introduction-to-python-hello-world-">Introduction to Python - <em>Hello World</em></h3>
<p>Python can be run in two ways, either in <strong>The Python Interpreter</strong>:</p>
<p><em>hello.py</em>:</p>
<pre><code class="lang-python">print(<span class="hljs-string">'Hello World!'</span>)</code></pre>
<pre><code class="lang-bash">> python hello.py
Hello World!</code></pre>
<p>or in an <em>Interactive shell</em>:</p>
<pre><code class="lang-bash">> python
Python 2.7.9 (default, May 1 2015, 19:04:44)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin
Type <span class="hljs-string">"help"</span>, <span class="hljs-string">"copyright"</span>, <span class="hljs-string">"credits"</span> or <span class="hljs-string">"license"</span> <span class="hljs-keyword">for</span> more information.
>>> <span class="hljs-built_in">print</span>(<span class="hljs-string">"Hello World!"</span>)
Hello World!
>>> <span class="hljs-built_in">exit</span>()</code></pre>
<p><strong> Try doing the following in an interactive shell</strong>:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> this</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-5">
<section class="slide-content"><h3 id="basic-data-types">Basic Data Types</h3>
<ul>
<li>Everything is an object.<ul>
<li>Interpreter can be left to decide what a variable is.</li>
<li>Explicit casts are available (int(), str() etc) but are not recommended.</li>
</ul>
</li>
</ul>
<p><em>variables.py</em></p>
<pre><code class="lang-python">i = <span class="hljs-number">2</span>
f = <span class="hljs-number">0.33</span>
s = <span class="hljs-string">"This is a string"</span>
print(s)
print(f * i)</code></pre>
<pre><code class="lang-bash">> python variables.py
This is a string
0.66</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-6">
<section class="slide-content"><h3 id="basic-data-types-string-ms-since-everything-s-an-object-strings-have-methods-for-manipiulation-anipulation-">Basic Data Types - <em>String Ms = 'Since everything\'s an object, strings have methods for manipiulation'anipulation</em></h3>
<p>Since everything is an object, strings have builtin methods for modification.</p>
<p><em>strings.py</em></p>
<pre><code class="lang-python">s = <span class="hljs-string">'Since everything\'s an object, strings have methods for manipiulation'</span>
print(s)
print(s.rstrip(<span class="hljs-string">'manipiulation'</span>))
print(s.upper())
print(s.lower())
print(s.swapcase())</code></pre>
<pre><code>> python strings.py
Since everything's an object, strings have methods for manipiulation
Since everything's an object, strings have methods for
SINCE EVERYTHING'S AN OBJECT, STRINGS HAVE METHODS FOR MANIPIULATION
since everything's an object, strings have methods for manipiulation
sINCE EVERYTHING'S AN OBJECT, STRINGS HAVE METHODS FOR MANIPIULATION</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-7">
<section class="slide-content"><h3 id="sequence-data-types-lists-">Sequence Data Types - <em>Lists</em></h3>
<ul>
<li>As described, lists are a collection of objects.</li>
<li>Behave in the same way as c arrays, but with the benifits of being an object in themselves!</li>
<li>Lists are <strong>mutable</strong>.</li>
<li>Can be added to with .append() and concanated with .extend()</li>
</ul>
<p><em>list.py</em></p>
<pre><code class="lang-python">list_1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
list_2 = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'d'</span>]
list_specials = [[<span class="hljs-string">'a'</span>], (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>), <span class="hljs-keyword">False</span>]
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(list_1, len(list_1)))
list_1.append(<span class="hljs-number">5</span>)
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(list_1, len(list_1)))
list_1.extend(list_2)
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(list_1, len(list_1)))
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(list_specials, len(list_specials)))</code></pre>
<pre><code>> python list.py
[1, 2, 3, 4] is of length 4
[1, 2, 3, 4, 5] is of length 5
[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd'] is of length 9
[['a'], (1, 2, 3), False] is of length 3</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-8">
<section class="slide-content"><h3 id="sequence-data-types-tuples-">Sequence Data Types - <em>Tuples</em></h3>
<ul>
<li>Similar to lists except that they are <strong>immutable</strong>!</li>
<li>Cannot be modified once created.</li>
<li>Useful when returning more than one thing from <em>functions</em> (see later).</li>
</ul>
<p><em>tuple.py</em></p>
<pre><code class="lang-python">x1 = ()
x2 = (<span class="hljs-number">1</span>,)
x3 = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
x4 = (<span class="hljs-number">1</span>, <span class="hljs-string">"mixed"</span>, <span class="hljs-number">2</span>, <span class="hljs-string">"tuple"</span>)
x5 = ((<span class="hljs-number">1</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>))
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(x1, len(x1)))
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(x2, len(x2)))
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(x3, len(x3)))
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(x4, len(x4)))
print(<span class="hljs-string">'{0} is of length {1}'</span>.format(x5, len(x5)))</code></pre>
<pre><code>> python tuple.py
() is of length 0
(1,) is of length 1
(1, 2, 3) is of length 3
(1, 'mixed', 2, 'tuple') is of length 4
((1, 2), (3, 4)) is of length 2</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-9">
<section class="slide-content"><h3 id="sequence-data-types-dictionaries-">Sequence Data Types - <strong>Dictionaries</strong></h3>
<ul>
<li>Used to store <strong>key value pairs</strong>.</li>
<li>Not stored in any particular order, left up to interpreter to order when inserting/accessing.</li>
<li>Values can be accessed/updated by refering to dictionary's key.</li>
</ul>
<p><em>dict.py</em></p>
<pre><code class="lang-python">d = {}
d[<span class="hljs-string">'name'</span>] = <span class="hljs-string">'Matt'</span>
d[<span class="hljs-string">'job'</span>] = <span class="hljs-string">'something or other'</span>
print(d)
print(d.keys())
print(<span class="hljs-string">'my name is{0}'</span>.format(d[<span class="hljs-string">'name'</span>]))
print(<span class="hljs-string">'and i do {0}'</span>.format(d[<span class="hljs-string">'job'</span>]))</code></pre>
<pre><code>> python dict.py
{'job': 'something or other', 'name': 'Matt'}
['job', 'name']
my name is Matt
and i do something or other</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-10">
<section class="slide-content"><h3 id="control-structures">Control Structures</h3>
<h4 id="if">IF</h4>
<p>Only execute statement(s) if condtion is met.</p>
<pre><code class="lang-python">x = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-keyword">if</span> len(x) > <span class="hljs-number">0</span>:
print(<span class="hljs-string">'IF:{0}'</span>.format(x))</code></pre>
<h4 id="for">FOR</h4>
<p>Iterates through items in collection (set, list, tuple etc...)</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> x:
print(<span class="hljs-string">'FOR:{0}'</span>.format(item))</code></pre>
<h4 id="while">WHILE</h4>
<p>Keep executing statement(s) until condition is not met.</p>
<pre><code class="lang-python">y = []
i = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> len(y) < len(x):
i += <span class="hljs-number">1</span>
y.append(i)
print(<span class="hljs-string">'WHILE:{0}'</span>.format(y))</code></pre>
<pre><code>IF:[1, 2, 3]
FOR:1
FOR:2
FOR:3
WHILE:[1]
WHILE:[1, 2]
WHILE:[1, 2, 3]</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-11">
<section class="slide-content"><h3 id="functions">Functions</h3>
<ul>
<li>Used to logically seperate off common code.</li>
<li>Can return value(s) to calling function.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_up</span><span class="hljs-params">(num_list)</span>:</span>
x = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> num_list:
x += i
<span class="hljs-keyword">return</span> x
<span class="hljs-comment"># our script starts running from here.</span>
nums = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]
print(<span class="hljs-string">'The sum of {0} is {1}'</span>.format(nums, add_up(nums)))</code></pre>
<pre><code>The sum of [1, 2, 3, 4, 5, 6] is 21</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-12">
<section class="slide-content"><h3 id="exception-handling">Exception Handling</h3>
<ul>
<li>Gracefully handle problems occuring in execution.</li>
<li>exceptions are signals sent from interpreter at time of problem.</li>
<li>Unless handled, they will cause a program to crash, displaying the exception.</li>
</ul>
<pre><code class="lang-python">x = <span class="hljs-number">3</span>
print(x / <span class="hljs-number">0</span>)</code></pre>
<pre><code>> python exception.py
Traceback (most recent call last):
File "exception.py", line 2, in <module>
print(x / 0)
ZeroDivisionError: integer division or modulo by zero</code></pre>
<ul>
<li>Exceptions can be used by wrapping code in try / except statements.</li>
</ul>
<pre><code class="lang-python">x = <span class="hljs-number">3</span>
<span class="hljs-keyword">try</span>:
print(x / <span class="hljs-number">0</span>)
<span class="hljs-keyword">except</span> ZeroDivisionError:
print(<span class="hljs-string">"You're trying to print by zero, this isnt allowed!"</span>)</code></pre>
<pre><code>> python exception.py
You're trying to print by zero, this isnt allowed!</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-13">
<section class="slide-content"><h3 id="classes">Classes</h3>
<ul>
<li>Used to group together variables/classes logically (usually into a model of something).</li>
<li>All the types we have been using are classes!</li>
</ul>
<p><em>class.py</em></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TrainingGroup</span>:</span>
<span class="hljs-comment"># class members</span>
date = <span class="hljs-string">''</span>
participants = []
<span class="hljs-comment"># class methods</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, date, participants)</span>:</span>
self.date = date
self.participants = participants
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'{0} participants on {1}'</span>.format(len(self.participants), self.date)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">kick_member</span><span class="hljs-params">(self, idiot)</span>:</span>
new_members = []
<span class="hljs-keyword">for</span> member <span class="hljs-keyword">in</span> self.participants:
<span class="hljs-keyword">if</span> idiot != member:
new_members.append(member)
<span class="hljs-keyword">else</span>:
print(<span class="hljs-string">'kicking {0}'</span>.format(idiot))
self.participants = new_members
group = TrainingGroup(<span class="hljs-string">'2015-08-10'</span>, [<span class="hljs-string">'Tom'</span>, <span class="hljs-string">'Dick'</span>, <span class="hljs-string">'Harry'</span>])
print(group)
print(group.participants)
group.kick_member(<span class="hljs-string">'Dick'</span>)
print(group)
print(group.participants)</code></pre>
<pre><code>> python class.py
3 participants on 2015-08-10
['Tom', 'Dick', 'Harry']
kicking Dick
2 participants on 2015-08-10
['Tom', 'Harry']</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-14">
<section class="slide-content"><h3 id="files">Files</h3>
<p><em>file.py</em></p>
<pre><code class="lang-python">infile = open(<span class="hljs-string">'testfile.txt'</span>, <span class="hljs-string">'r'</span>) <span class="hljs-comment"># opened for reading only</span>
outfile = open(<span class="hljs-string">'file.output.txt'</span>, <span class="hljs-string">'w'</span>) <span class="hljs-comment"># opened for writing</span>
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> infile.readlines():
outfile.write(line)</code></pre>
<pre><code>> python file.py
> cat file.output.txt
this
is
is
a
test
file</code></pre>
<p>Files can be opened in different <em>modes</em>:</p>
<table>
<thead>
<tr>
<th style="text-align:left"></th>
<th style="text-align:left"></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">r</td>
<td style="text-align:left">Open text file for reading. The stream is positioned at the beginning of the file.</td>
</tr>
<tr>
<td style="text-align:left">r+</td>
<td style="text-align:left">Open for reading and writing. The stream is positioned at the beginning of the file.</td>
</tr>
<tr>
<td style="text-align:left">w</td>
<td style="text-align:left">Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.</td>
</tr>
<tr>
<td style="text-align:left">w+</td>
<td style="text-align:left">Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is</td>
</tr>
<tr>
<td style="text-align:left"></td>
<td style="text-align:left">positioned at the beginning of the file.</td>
</tr>
<tr>
<td style="text-align:left">a</td>
<td style="text-align:left">Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file.</td>
</tr>
<tr>
<td style="text-align:left"></td>
<td style="text-align:left">Subsequent writes to the file will always end up at the then current end of file.</td>
</tr>
<tr>
<td style="text-align:left">a+</td>
<td style="text-align:left">Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the</td>
</tr>
<tr>
<td style="text-align:left"></td>
<td style="text-align:left">file. Subsequent writes to the file will always end up at the then current end of file.</td>
</tr>
</tbody>
</table>
</section>
</div>
<div class="slide hidden" id="slide-15">
<section class="slide-content"><h3 id="larger-programs-using-modules-">Larger Programs - <em>Using Modules</em></h3>
<ul>
<li>Large codebases can be broken up into seperate files.</li>
<li><em>Calling</em> functions calling functions from another file need to have <strong>imported</strong> them before use.</li>
<li>Good practice to cut down on <em>code replication</em>.</li>
<li>Importing a file causes <strong>all code in the file to be executed</strong>.<ul>
<li>Need to be wary of code not within a function</li>
<li>Can <em>guard</em> against this code by wrapping it in <code>if __name__ == '__main__':</code></li>
</ul>
</li>
</ul>
<p><em>mymodule.py</em></p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sum_numeric_list</span><span class="hljs-params">(nums)</span>:</span>
<span class="hljs-string">"""
Accepts a list of numeric values.
Returns the sum of the elements.
"""</span>
sum = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> nums:
sum += item
<span class="hljs-keyword">return</span> sum
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">prune_dict</span><span class="hljs-params">(dict, keys_to_remove)</span>:</span>
<span class="hljs-string">"""
Accepts a dict to priune and a list of keys to remove.
Matching keys are deleted and rmainders returned.
"""</span>
pruned_dict = {}
<span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> dict.keys():
<span class="hljs-keyword">if</span> key <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> keys_to_remove:
pruned_dict[key] = dict[key]
<span class="hljs-keyword">return</span> pruned_dict
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
test_ints = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
print(<span class="hljs-string">'The sum of {0} is {1}'</span>.format(test_ints, sum_numeric_list(test_ints)))
test_dict = {<span class="hljs-string">'Tom'</span>: <span class="hljs-string">'The First'</span>, <span class="hljs-string">'Dick'</span>: <span class="hljs-string">'The Second'</span>, <span class="hljs-string">'Harry'</span>: <span class="hljs-string">'The Third'</span>}
pruned_dict = prune_dict(test_dict, <span class="hljs-string">'Tom'</span>)
print(<span class="hljs-string">'Stripping \'Tom\' from {0} gives us {1}'</span>.format(test_dict, pruned_dict))</code></pre>
<pre><code>> python mymodule.py
The sum of [1, 2, 3, 4, 5] is 15
Stripping 'Tom' from {'Harry': 'The Third', 'Dick': 'The Second', 'Tom': 'The First'} gives us {'Dick': 'The Second', 'Harry': 'The Third'}</code></pre>
<p>Notice how just importing the module doesn't execute the test code:</p>
<pre><code>> python
Python 2.7.9 (default, May 1 2015, 19:04:44)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymodule
>>> dir(mymodule)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'prune_dict', 'sum_numeric_list']</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-16">
<section class="slide-content"><h3 id="larger-programs-using-modules-">Larger Programs - <em>Using Modules</em></h3>
<p>After importing the module, you can now use it's functions without the test code being called:</p>
<p><em>import py:</em></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> mymodule
<span class="hljs-keyword">from</span> mymodule <span class="hljs-keyword">import</span> prune_dict
nums = [<span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]
print(<span class="hljs-string">'SUM:{0}'</span>.format(mymodule.sum_numeric_list(nums)))
dict = {<span class="hljs-string">'a'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'b'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'c'</span>: <span class="hljs-number">3</span>}
new_dict = prune_dict(dict, <span class="hljs-string">'b'</span>)
print(<span class="hljs-string">'dict: {0}'</span>.format(dict))
print(<span class="hljs-string">'new_dict: {0}'</span>.format(new_dict))</code></pre>
<pre><code>> python import.py
SUM:30
dict:{'a': 1, 'c': 3, 'b': 2}
new_dict:{'a': 1, 'c': 3}</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-17">
<section class="slide-content"><h3 id="pattern-matching">Pattern Matching</h3>
<ul>
<li>Regex matching is done via built in <code>re</code> module.</li>
<li>re.search - <em>look for pattern anywhere within string</em></li>
<li>re.match - <em>look for exact pattern match in string</em></li>
<li>regex's can be pre-compiled for optimisation <em>see example</em></li>
</ul>
<p><em>regex cheat sheet</em>:</p>
<table>
<thead>
<tr>
<th style="text-align:right"></th>
<th style="text-align:left"></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right">^</td>
<td style="text-align:left">start of string</td>
</tr>
<tr>
<td style="text-align:right">$</td>
<td style="text-align:left">end of string</td>
</tr>
<tr>
<td style="text-align:right">[abc]</td>
<td style="text-align:left">Any one character from list (a OR b OR c)</td>
</tr>
<tr>
<td style="text-align:right">[a-m]</td>
<td style="text-align:left">Any one character from range (a OR b OR ... OR m)</td>
</tr>
<tr>
<td style="text-align:right">[^abc]</td>
<td style="text-align:left">Any one character not in list (NOT a NOR b NOR c)</td>
</tr>
<tr>
<td style="text-align:right">.</td>
<td style="text-align:left">Any one character</td>
</tr>
<tr>
<td style="text-align:right">\s</td>
<td style="text-align:left">Any white space character</td>
</tr>
<tr>
<td style="text-align:right">\S</td>
<td style="text-align:left">Any NON white space character</td>
</tr>
<tr>
<td style="text-align:right">\d</td>
<td style="text-align:left">Any digit</td>
</tr>
<tr>
<td style="text-align:right">\D</td>
<td style="text-align:left">Any NON digit</td>
</tr>
<tr>
<td style="text-align:right">\w</td>
<td style="text-align:left">Any alphanumeric</td>
</tr>
<tr>
<td style="text-align:right">\W</td>
<td style="text-align:left">Any NON alphanumeric</td>
</tr>
<tr>
<td style="text-align:right">()</td>
<td style="text-align:left">Group together matches within parentheses (use with re.search().group()).</td>
</tr>
</tbody>
</table>
</section>
</div>
<div class="slide hidden" id="slide-18">