-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythonathon.html
3166 lines (2584 loc) · 117 KB
/
pythonathon.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
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2017-05-16 Tue 20:59 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Ross Donaldson" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2017 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgcf4d2f5" class="outline-2">
<h2 id="orgcf4d2f5"><span class="todo TODO">TODO</span> In progress</h2>
<div class="outline-text-2" id="text-orgcf4d2f5">
</div><div id="outline-container-orge2f5972" class="outline-3">
<h3 id="orge2f5972"><span class="done DONE">DONE</span> List comprehensions</h3>
</div>
<div id="outline-container-orgf9750e0" class="outline-3">
<h3 id="orgf9750e0"><span class="done DONE">DONE</span> Namespaces and Imports</h3>
</div>
<div id="outline-container-orgccbd307" class="outline-3">
<h3 id="orgccbd307"><span class="todo TODO">TODO</span> Decorators</h3>
</div>
<div id="outline-container-org1a370a7" class="outline-3">
<h3 id="org1a370a7"><span class="todo TODO">TODO</span> Project Structure</h3>
</div>
</div>
<div id="outline-container-org14eb555" class="outline-2">
<h2 id="org14eb555">What Even</h2>
<div class="outline-text-2" id="text-org14eb555">
<p>
Some of us are born to python, some rise to python, and others have python
thrust upon 'em. Let's learn you a python.
</p>
<p>
This document assumes you've seen computer programming before, but tries to be
kind in how it is paced. Everything here is for Python 3.
</p>
</div>
<div id="outline-container-orgb8a57f2" class="outline-3">
<h3 id="orgb8a57f2">A note about Abstraction</h3>
<div class="outline-text-3" id="text-orgb8a57f2">
<p>
In some sense, the two core actions of computer programming are <i>abstraction</i> and
<i>naming</i>. That is: we're going to try and make code that expresses an idea; we
have to give that code and its constituents clear and meaningful names. This is,
I think, a tricky idea to get your head all the way around without thorough
exposure. My intention is that this document will point out some places where we
abstract ourselves from something so the ideas sink in well.
</p>
</div>
</div>
</div>
<div id="outline-container-orgc8a6614" class="outline-2">
<h2 id="orgc8a6614">Types</h2>
<div class="outline-text-2" id="text-orgc8a6614">
<p>
We've briefly covered the notion of a type. Python offers us some foundational
types to work with:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Type</th>
<th scope="col" class="org-left">Specification</th>
<th scope="col" class="org-left">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">int</td>
<td class="org-left">Integer; effectively unlimited size</td>
<td class="org-left">1, 5, 12,487,129,420</td>
</tr>
<tr>
<td class="org-left">float</td>
<td class="org-left">Double-precision floating point number</td>
<td class="org-left">0.219, 50.6</td>
</tr>
<tr>
<td class="org-left">complex</td>
<td class="org-left">Complex numbers</td>
<td class="org-left">2i</td>
</tr>
<tr>
<td class="org-left">bool</td>
<td class="org-left">Boolean</td>
<td class="org-left">True/False</td>
</tr>
<tr>
<td class="org-left">str</td>
<td class="org-left">String</td>
<td class="org-left">'cat', 'house boat'</td>
</tr>
</tbody>
</table>
<p>
Python is a <i>strongly, dynamically typed</i> language. This means we almost never
have to care about what the type of a thing is when we declare or receive it,
but we <i>cannot</i> use types interchangeably in some contexts. For instance:
</p>
<pre class="example">
>>> 2 + 2
4
>>> 'cat' + 'dog'
'catdog'
>>> 2 + 'dog'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
</pre>
<p>
Between ints, <code>+</code> means "addition"; between strings, it means "concatenation". But
between an int and a string, python cannot and will not guess what <code>+</code> means, and
throws a type error. We must "cast", changing the type of one operand to match
the other, so that python knows how to <code>+</code> everything together correctly:
</p>
<pre class="example">
>>> str(2) + 'dog'
'2dog'
</pre>
</div>
</div>
<div id="outline-container-org04dfeeb" class="outline-2">
<h2 id="org04dfeeb">Identifiers</h2>
<div class="outline-text-2" id="text-org04dfeeb">
<p>
Ok, so we're going to name things. In python, we name things by giving them an
<i>identifier</i>. A valid identifier in python follows these rules:
</p>
<ol class="org-ol">
<li>It can be any combination of upper and lowercase letters, numbers, and the <code>_</code> character.</li>
<li>It must start with a letter.</li>
</ol>
<p>
So, <code>my_swe3t_l33t_IdEnTiFi3r</code> is valid (but don't ever do that); <code>3rd_item</code> is not.
</p>
<p>
Python usually follows these conventions:
</p>
<ol class="org-ol">
<li>Identifiers used for variables are in <i>snake case</i>: all lower-case letters with
words separated by the underscore character.
E.G. <code>a_variable_named_foo</code></li>
<li>Identifiers for classes are in <i>title case</i>: each word with its first letter
capitalized, no spaces or underscores.
E.G. <code>MyFooClass</code></li>
</ol>
</div>
</div>
<div id="outline-container-orgcaa57e1" class="outline-2">
<h2 id="orgcaa57e1">Statements</h2>
<div class="outline-text-2" id="text-orgcaa57e1">
<p>
Python code is made up of <i>statements</i>. A statement can be a whoooole lot of
things. A statement might be variable assignment, or creating a function. Simply
listing a type or an object <i>isn't</i> a statement.
</p>
<p>
So, in an interpreter:
</p>
<pre class="example">
>>> 5 # not a statement
5
>>> x = 5 # statement
</pre>
</div>
</div>
<div id="outline-container-orgf394436" class="outline-2">
<h2 id="orgf394436">Variables</h2>
<div class="outline-text-2" id="text-orgf394436">
<p>
Variable assignment is one of the most standard features a programming language
can have. In python, variable assignment is as simple as can be:
</p>
<div class="org-src-container">
<pre class="src src-python">x = 5
print(x)
</pre>
</div>
<pre class="example">
5
</pre>
<p>
Put another way: we're binding the <i>value</i> 5 to the <i>identifier</i> x. We can bind any
value we want to any valid identifier this way.
</p>
<p>
Now: it's important that you understand that there is a thing called <i>scope</i>,
which affects when and how variables can be accessed. We're going to get to
<a href="#orgbfa0668">scope</a> soon, but we need a few more ideas before we can fully explain it.
</p>
</div>
</div>
<div id="outline-container-org4580670" class="outline-2">
<h2 id="org4580670">Reserved Words</h2>
<div class="outline-text-2" id="text-org4580670">
<p>
Before we get too far, there's a thing about Python you should know – which is
a thing that's true of many programming languages, so it's useful to be clear
on. This is the notion of <i>reserved words</i>. It goes like this:
</p>
<p>
When we write code, we express to a computer what we want it to do. The language
we use to express ourselves is our programming language. That language has some
syntax, made of words and symbols, that allows us to get our ideas and
intentions written down. Certain words and symbols are baked in to the language,
very deeply – their meaning cannot be changed by us, and we have to respect and
use these words only in very specific ways.
</p>
<p>
(<i>Nota bene</i>: in python, "reserved words" are typically referred to as "keywords."
Same idea, slightly different name.)
</p>
<p>
What this means in practice is that we <i>cannot use a reserved word as an
identifier</i>. For instance:
</p>
<div class="org-src-container">
<pre class="src src-python">False = 5 # NOPE
import = 7 # SUPER NOPE
</pre>
</div>
<p>
The python keywords are:
<code>False</code>, <code>class</code>, <code>finally</code>, <code>is</code>, <code>return</code>, <code>None</code>, <code>continue</code>, <code>for</code>, <code>lambda</code>, <code>try</code>, <code>True</code>, <code>def</code>,
<code>from</code>, <code>nonlocal</code>, <code>while</code>, <code>and</code>, <code>del</code>, <code>global</code>, <code>not</code>, <code>with</code>, <code>as</code>, <code>elif</code>, <code>if</code>, <code>or</code>, <code>yield</code>,
<code>assert</code>, <code>else</code>, <code>import</code>, <code>pass</code>, <code>break</code>, <code>except</code>, <code>in</code>, <code>raise</code>
</p>
<p>
We will get in to what most of these do as we work through this document! Hang
in there.
</p>
</div>
</div>
<div id="outline-container-orgcf5ebf8" class="outline-2">
<h2 id="orgcf5ebf8">Boolean comparisons</h2>
<div class="outline-text-2" id="text-orgcf5ebf8">
<p>
Let's say we want to make a logical statement about the comparison of two
values. If we're dealing with numbers, python provides a set of built-in
operators to help us do precisely this. We can explore this in the python
interpreter:
</p>
<pre class="example">
>>> 5 < 6
True
>>> 1 > 100
False
</pre>
<p>
Note our first two keywords: <code>True</code> and <code>False</code>.
</p>
<p>
Python also supports greater-than-or-equal to, so:
</p>
<pre class="example">
>>> 5 >= 9
False
>>> 9 >= 9
True
</pre>
<p>
Or we can test equality:
</p>
<pre class="example">
>>> 10 == 10
True
</pre>
<p>
Common in many languages, exclamation point captures the idea of negation in a
symbol. So, "not equal" is written:
</p>
<pre class="example">
>>> 4 != 5
True
>>> 4 != 4
False
</pre>
<p>
Python also provides the keyword <code>not</code>, which, as with <code>!</code>, negates any Boolean
expression following it:
</p>
<pre class="example">
>>> not True
False
>>> not 4 == 5
True
</pre>
<p>
Note that python also has nice English keywords for Boolean operators: <code>and</code> and
<code>or</code>:
</p>
<pre class="example">
>>> False or True
True
>>> False and False
False
>>> False and True
False
>>> True and True
True
</pre>
</div>
<div id="outline-container-org05b82b8" class="outline-3">
<h3 id="org05b82b8">Equality versus Identity</h3>
<div class="outline-text-3" id="text-org05b82b8">
<p>
Along with equality operators (e.g. <code>==</code>), python provides an <i>identity</i> operator.
While extremely useful, the identity operator can also lead to some very subtle
bugs. This is in part because the identity operator is <code>is</code>, and thus has a much
more natural language syntax than <code>==</code>. However, observe:
</p>
<pre class="example">
>>> a = 19998989890
>>> b = 19998989889 + 1
>>> a == b
True
>>> a is b
False
</pre>
<p>
<i>Equality</i> compares the <i>value</i> of two things; <i>identity</i> checks to see if two things
are literally the same object in memory.
</p>
<p>
As a general rule, <code>is</code> can always be used to compare with <code>True</code>, <code>False</code>, and <code>None</code>.
This is because these three values (all keywords, notice) are <i>singleton objects</i>
– there is only one <code>True</code> object, ever, period, so equality and identity are
effectively interchangeable. For more complex kinds of values, it's often better
to stick to <code>==</code>. Thus:
</p>
<pre class="example">
>>> x = True
>>> x is True
True
>>> x is not False
True
>>> y = 10
>>> y == 10
True
</pre>
</div>
</div>
</div>
<div id="outline-container-org1c4ddd1" class="outline-2">
<h2 id="org1c4ddd1">Control Flow</h2>
<div class="outline-text-2" id="text-org1c4ddd1">
<p>
If we have a notion of Boolean values and truthiness, we can now decide to
change the way our program works based on some Boolean condition. This is called
<code>control flow</code>, and it is very nice.
</p>
<p>
The single most common control flow structure is the <code>if / else</code> block. Python
elides the common <code>else if</code> phrase in to <code>elif</code>, for no reason in particular.
</p>
<div class="org-src-container">
<pre class="src src-python">x = 5
if x > 10:
print('X is greater than 10!')
elif x == 10:
print('X is exactly 10')
else:
print('X must be less than ten')
</pre>
</div>
<pre class="example">
X must be less than ten
</pre>
<p>
These checks can get quite complex:
</p>
<div class="org-src-container">
<pre class="src src-python">if x < 5 or y is 'cow':
print('woah')
elif (x is 5 and y is 5 and z is 5) or skip_the_fives:
print('okay double woah')
else:
print('whew')
</pre>
</div>
<p>
A thing to notice: instead of doing an explicit comparison, we can use the
<a href="#org79ffe11">Truthiness</a> of a term directly:
</p>
<div class="org-src-container">
<pre class="src src-python">if 5:
print('it must be 5')
</pre>
</div>
<pre class="example">
it must be 5
</pre>
<p>
Seen slightly less frequently, but still fairly common, is the <code>while</code> construct,
which loops "while" some term is truthy:
</p>
<div class="org-src-container">
<pre class="src src-python">x = 0
while x < 10:
print(x)
x = x + 1
</pre>
</div>
<pre class="example">
0
1
2
3
4
5
6
7
8
9
</pre>
<p>
Note two things:
</p>
<ol class="org-ol">
<li>If <code>x</code> weren't mutated, the loop would loop forever.</li>
<li>You can use a <code>while</code> loop to loop forever, on purpose.</li>
</ol>
<p>
#2 is not uncommonly seen for the "main loop" of a program. That is: if we
#consider a computer "program" to be a thing that sits idle until some action
#occurs, then goes back to being idle, we could express that idea like so:
</p>
<pre class="example">
while True:
if check_for_user_input():
respond_appropriately()
</pre>
</div>
<div id="outline-container-org79ffe11" class="outline-3">
<h3 id="org79ffe11">Truthiness</h3>
<div class="outline-text-3" id="text-org79ffe11">
<p>
Python has a broad notion of what we often call "truthiness". That is: certain
values are <i>implicitly</i> considered to be roughly equivalent to <code>True</code> or <code>False</code> when
used in control flow expressions.
</p>
<p>
So:
</p>
<dl class="org-dl">
<dt>Truthy Values are</dt><dd><ul class="org-ul">
<li><code>True</code></li>
<li>Any string with length greater than 0</li>
<li>All numbers</li>
<li>All non-empty collections</li>
<li>Most object instances (we'll get in to what this is in a little bit)</li>
</ul></dd>
<dt>Falsy Values are</dt><dd><ul class="org-ul">
<li><code>False</code></li>
<li>Empty string</li>
<li>Empty collections</li>
<li><code>None</code></li>
</ul></dd>
</dl>
<p>
We use them like:
</p>
<div class="org-src-container">
<pre class="src src-python">a_list = []
if not a_list:
print('it is empty!')
else:
print('it is full')
</pre>
</div>
<pre class="example">
it is empty!
</pre>
<p>
Or:
</p>
<div class="org-src-container">
<pre class="src src-python">full_string = 'this is a string'
empty_string = ''
if full_string:
print('there was some string!')
if empty_string:
print('you should be surprised if this prints')
</pre>
</div>
<pre class="example">
there was some string!
</pre>
</div>
</div>
</div>
<div id="outline-container-org9e01adf" class="outline-2">
<h2 id="org9e01adf">Collections</h2>
<div class="outline-text-2" id="text-org9e01adf">
<p>
A "collection" is, as the name implies, a kind of container or group of Things.
Python comes with four main collection types built-in; in practice, we use two
of them vastly more than the others. For every collection, python provides a
<i>literal</i> syntax, which is a shorthand way of creating a new collection.
</p>
<p>
<b>Note</b>: all collections in python are <i>zero indexed</i>. This means that the very first
element in a collection is the 0 element, the second is the 1 element, etc.
This takes a little getting used to, but is also very common.
</p>
<p>
Also note: all python collections are <i>heterogeneous</i> – they can contain Things
of any combination of types, including other collections.
</p>
</div>
<div id="outline-container-org9dba229" class="outline-3">
<h3 id="org9dba229">Tuples</h3>
<div class="outline-text-3" id="text-org9dba229">
<p>
A tuple is an immutable, and usually small, collection. It is used to group
together a small number of things we implicitly assert are related to one
another. The tuple literal is a set of parens <code>()</code>. We access the elements of a
tuple by their index.
</p>
<div class="org-src-container">
<pre class="src src-python">x = ('cat', 'dog', 'phone')
print(x[0])
print(x[1])
print(x[2])
</pre>
</div>
<pre class="example">
cat
dog
phone
</pre>
<p>
Note a python oddity: to make a single-element tuple, a comma is needed after
the first element – e.g. <code>('cat',)</code>.
</p>
</div>
</div>
<div id="outline-container-orgf87c0ea" class="outline-3">
<h3 id="orgf87c0ea">Lists</h3>
<div class="outline-text-3" id="text-orgf87c0ea">
<p>
A <code>list</code> is one of the data structures we interact with alllllll the time in
python. We can make a list with the <code>list</code> function, but it's more common to do it
with the list literal, which is a set of square braces <code>[]</code>.
</p>
<p>
Lists are ordered and mutable. We access the elements of a list by their index.
</p>
<div class="org-src-container">
<pre class="src src-python">a_list = [5, False, 'gazpacho']
print(a_list[2])
</pre>
</div>
<pre class="example">
gazpacho
</pre>
</div>
</div>
<div id="outline-container-orgf968600" class="outline-3">
<h3 id="orgf968600">Dicts</h3>
<div class="outline-text-3" id="text-orgf968600">
<p>
A <code>dict</code> captures the notion of key-value pairs in python; the name is short for
<i>dictionary</i>, which gives us a very good hit about its use. <code>Dicts</code> offer us <i>very
fast</i> lookup of elements. There is a <code>dict</code> function, but we more commonly use the
curly-brace literal, <code>{}</code>, with the internal format keyname, colon, space, value
of key (E.G. <code>{name_of_key: value}</code>.)
</p>
<p>
The key of a <code>dict</code> is typically a string, but sometimes, tuples or integers are
used.<sup><a id="fnr.1" class="footref" href="#fn.1">1</a></sup>
</p>
<p>
We access a list of the keys in a <code>dict</code> using an instance<sup><a id="fnr.2" class="footref" href="#fn.2">2</a></sup> method called
<code>keys()</code>. We access values by the name of their key. Like so:
</p>
<div class="org-src-container">
<pre class="src src-python">the_dict = {'googoo': 'cachoo',
'hocus': 'pocus',
'Marlon': 'Brando'}
print(the_dict.keys())
print(the_dict['hocus'])
</pre>
</div>
<pre class="example">
['googoo', 'Marlon', 'hocus']
pocus
</pre>
</div>
</div>
<div id="outline-container-org9169887" class="outline-3">
<h3 id="org9169887">Sets</h3>
<div class="outline-text-3" id="text-org9169887">
<p>
A set is a very handy data type with a special property: <i>every element of the
set is guaranteed unique</i>. Sets are, thus, used for uniquing, and for
maintaining collections of unique elements. You can use the <code>set</code> function, or
you can use the set literal, which is, slightly confusingly, also curly braces
<code>{}</code>. (If there are no colons inside the braces, python knows it's a <code>set</code>, not a
<code>dict.</code>)
</p>
<p>
When you create a set, all of the elements will be uniqued correctly. This is
done by… wait for it… hashing each element, which means each element in a
set must be hashable.
</p>
<div class="org-src-container">
<pre class="src src-python">list_with_duplicates = [1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5]
the_set = set(list_with_duplicates)
print(the_set)
</pre>
</div>
<pre class="example">
set([1, 2, 3, 4, 5])
</pre>
<p>
For those of you with a math bent, you might be thinking, "I wonder if we can
take the union, difference, and intersection of Python's sets?" Good news! You
absolutely can. The interface is exposed as instance methods on a given set.
</p>
<div class="org-src-container">
<pre class="src src-python">first_set = {1, 2, 3}
second_set = {3, 4, 5}
# The union of two sets is all the unique elements of both sets together in one
print(first_set.union(second_set))
# The intersection is only those elements found in both sets
print(first_set.intersection(second_set))
# The difference is all the elements from the calling set not found in the
# argument set -- in this case, all the elements in first_set not found in
# second_set
print(first_set.difference(second_set))
</pre>
</div>
<pre class="example">
set([1, 2, 3, 4, 5])
set([3])
set([1, 2])
</pre>
</div>
</div>
</div>
<div id="outline-container-org0b22fa7" class="outline-2">
<h2 id="org0b22fa7">Iteration and Comprehension</h2>
<div class="outline-text-2" id="text-org0b22fa7">
<p>