forked from UWPCE-PythonCert/IntroToPython-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession01.html
More file actions
1691 lines (1547 loc) · 97.2 KB
/
session01.html
File metadata and controls
1691 lines (1547 loc) · 97.2 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>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Session One: Introductions — Introduction To Python 1.3 documentation</title>
<link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="top" title="Introduction To Python 1.3 documentation" href="index.html"/>
<link rel="next" title="Session Two: Functions, Booleans and Modules" href="session02.html"/>
<link rel="prev" title="In This Course" href="index.html"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-nav-search">
<a href="index.html" class="fa fa-home"> Introduction To Python</a>
<div role="search">
<form id ="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="">Session One: Introductions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#introductions">Introductions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#introduction-to-this-class">Introduction to This Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="#introduction-to-your-environment">Introduction to Your Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="#setting-up-your-environment">Setting Up Your Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="#introduction-to-ipython">Introduction to iPython</a></li>
<li class="toctree-l2"><a class="reference internal" href="#basic-python-syntax">Basic Python Syntax</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id2">Homework</a></li>
<li class="toctree-l2"><a class="reference internal" href="#next-class">Next Class</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session02.html">Session Two: Functions, Booleans and Modules</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session02.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#git-work">Git Work</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#quick-intro-to-basics">Quick Intro to Basics</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#functions">Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#in-class-lab">In-Class Lab:</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#boolean-expressions">Boolean Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#id1">In-Class Lab:</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#code-structure-modules-and-namespaces">Code Structure, Modules, and Namespaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#id4">In-Class Lab</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session03.html">Session Three: Sequences, Iteration and String Formatting</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session03.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#sequences">Sequences</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#lists-tuples">Lists, Tuples...</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#mutability">Mutability</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#mutable-sequence-methods">Mutable Sequence Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#id1">Iteration</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#string-features">String Features</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#one-last-trick">One Last Trick</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session04.html">Session Four: Dictionaries, Sets, Exceptions, and Files</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session04.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#a-little-warm-up">A little warm up</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#dictionaries-and-sets">Dictionaries and Sets</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#exceptions">Exceptions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#file-reading-and-writing">File Reading and Writing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#paths-and-directories">Paths and Directories</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session05.html">Session Five: Advanced Argument passing, List and Dict Comprehensions, Lambda and Functional programming</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session05.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#advanced-argument-passing">Advanced Argument Passing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#a-bit-more-on-mutability-and-copies">A bit more on mutability (and copies)</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#list-and-dict-comprehensions">List and Dict Comprehensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#anonymous-functions">Anonymous functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#functional-programming">Functional Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session06.html">Session Six: Object oriented programming: Classes, instances, attributes, and subclassing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session06.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session06.html#object-oriented-programming">Object Oriented Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="session06.html#python-classes">Python Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="session06.html#subclassing-inheritance">Subclassing/Inheritance</a></li>
<li class="toctree-l2"><a class="reference internal" href="session06.html#more-on-subclassing">More on Subclassing</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session07.html">Session Seven: Testing, More OO</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session07.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#testing">Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#more-on-subclassing">More on Subclassing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#properties">Properties</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#static-and-class-methods">Static and Class Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#special-methods">Special Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session08.html">Session Eight: Generators, Iterators, Decorators, and Context Managers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session08.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#decorators">Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#iterators-and-generators">Iterators and Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#context-managers">Context Managers</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#homework">Homework</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="homework/index.html">Homework Materials</a><ul>
<li class="toctree-l2"><a class="reference internal" href="homework/kata_fourteen.html">Kata Fourteen: Tom Swift Under Milk Wood</a></li>
<li class="toctree-l2"><a class="reference internal" href="homework/html_builder.html">HTML Renderer Homework Assignment</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="supplements/index.html">Supplemental Materials</a><ul>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_learning_resources.html">Useful Python Learning Resources</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_for_mac.html">Setting up your Mac for Python and this class</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_for_windows.html">Setting up Windows for Python and this class</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_for_linux.html">Setting up Linux for Python and this class</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/virtualenv.html">Working with Virtualenv</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/sublime_as_ide.html">Turning Sublime Text Into a Lightweight Python IDE</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/shell.html">Shell Customizations for Python Development</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/unicode.html">Unicode in Python 2</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Introduction To Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Session One: Introductions</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/session01.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main">
<div class="section" id="session-one-introductions">
<h1>Session One: Introductions<a class="headerlink" href="#session-one-introductions" title="Permalink to this headline">¶</a></h1>
<div class="line-block">
<div class="line">In which you are introduced to this class, your instructors, your environment</div>
<div class="line">and your new best friend, Python.</div>
</div>
<a class="reference internal image-reference" href="_images/python.png"><img alt="_images/python.png" class="align-center" src="_images/python.png" style="width: 38%;" /></a>
<p class="credit"><a class="reference external" href="http://xkcd.com/353">xkcd.com/353</a></p>
<div class="section" id="introductions">
<h2>Introductions<a class="headerlink" href="#introductions" title="Permalink to this headline">¶</a></h2>
<p class="center large">In which we meet each-other</p>
<div class="section" id="your-instructors">
<h3>Your instructors<a class="headerlink" href="#your-instructors" title="Permalink to this headline">¶</a></h3>
<div class="center large line-block">
<div class="line">Christopher Barker</div>
<div class="line">(PythonCHB at gmail dot com)</div>
<div class="line"><br /></div>
</div>
<div class="center large line-block">
<div class="line">Nathan Savage</div>
<div class="line">(nathansavagemail at gmail dot com)</div>
<div class="line"><br /></div>
</div>
</div>
<div class="section" id="who-are-you">
<h3>Who are you?<a class="headerlink" href="#who-are-you" title="Permalink to this headline">¶</a></h3>
<p class="center large">Tell us a tiny bit about yourself:</p>
<ul class="simple">
<li>name</li>
<li>programming background: what languages have you used?</li>
<li>what do you hope to get from this class</li>
</ul>
</div>
</div>
<div class="section" id="introduction-to-this-class">
<h2>Introduction to This Class<a class="headerlink" href="#introduction-to-this-class" title="Permalink to this headline">¶</a></h2>
<p class="center large">Intro to Python</p>
<div class="section" id="course-materials-online">
<h3>Course Materials Online<a class="headerlink" href="#course-materials-online" title="Permalink to this headline">¶</a></h3>
<p>A rendered HTML copy of the slides for this course may be found online at:</p>
<p><a class="reference external" href="http://uwpce-pythoncert.github.io/IntroToPython">http://uwpce-pythoncert.github.io/IntroToPython</a></p>
<p>Also there are some homework descriptions and supplemental materials.</p>
<p>The source of these materials are in the class gitHub repo:</p>
<p><a class="reference external" href="https://github.com/UWPCE-PythonCert/IntroToPython">https://github.com/UWPCE-PythonCert/IntroToPython</a></p>
<p>Class email list: We will be using this list to communicate for this class:</p>
<p><a class="reference external" href="mailto:programming-in-python%40googlegroups.com">programming-in-python<span>@</span>googlegroups<span>.</span>com</a></p>
<p>You should have (or will soon) received and email invitation to join
the mailing list.</p>
</div>
<div class="section" id="class-structure">
<h3>Class Structure<a class="headerlink" href="#class-structure" title="Permalink to this headline">¶</a></h3>
<p>Class Time:</p>
<blockquote>
<div><ul class="simple">
<li>Some lecture, lots of demos</li>
<li>Lab time: lots of hand-on practice
- Take a break if you need one then...</li>
<li>Lather, Rinse, Repeat.....</li>
</ul>
</div></blockquote>
<p>Interrupt me with questions – please!</p>
<p>(Some of the best learning prompted by questions)</p>
</div>
<div class="section" id="homework">
<h3>Homework:<a class="headerlink" href="#homework" title="Permalink to this headline">¶</a></h3>
<ul>
<li><p class="first">Assigned at each class</p>
</li>
<li><p class="first">You are adults – it’s up to you to do it</p>
</li>
<li><p class="first">You can do a gitHub “pull request” if you want us to review it.</p>
<blockquote>
<div><ul class="simple">
<li>We’ll review how to do that later...</li>
</ul>
</div></blockquote>
</li>
</ul>
</div>
<div class="section" id="mailing-list-and-office-hours">
<h3>Mailing list and Office Hours<a class="headerlink" href="#mailing-list-and-office-hours" title="Permalink to this headline">¶</a></h3>
<p><strong>Mailing list:</strong></p>
<p>We’ve set up a google group – you will all be invited to join:</p>
<p><tt class="docutils literal"><span class="pre">programming-in-python@googlegroups.com</span></tt></p>
<p><strong>Office Hours:</strong></p>
<p>I generally will hold “office hours” at a coffee shop for a couple hours
each weekend.</p>
<p>Nathan can do some as well.</p>
<p>What are good times for you?</p>
</div>
<div class="section" id="lightning-talks">
<h3>Lightning Talks<a class="headerlink" href="#lightning-talks" title="Permalink to this headline">¶</a></h3>
<p><strong>Lightning Talks:</strong></p>
<blockquote>
<div><ul class="simple">
<li>5 minutes each (including setup) - no kidding!</li>
<li>Every student will give one</li>
<li>Purposes: introduce yourself, share interests, also show Python applications</li>
<li>Any topic you like, that is related to Python – according to you!</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="python-ecosystem">
<h3>Python Ecosystem<a class="headerlink" href="#python-ecosystem" title="Permalink to this headline">¶</a></h3>
<p>Python is Used for:</p>
<blockquote>
<div><ul class="simple">
<li>CS education (this course!)</li>
<li>Application scripting (GIS, GNU Radio, Blender...)</li>
<li>Systems administration and “glue”</li>
<li>Web applications (Django etc. etc. etc.)</li>
<li>Scientific/technical computing (a la MATLAB, R, .... )</li>
<li>Software tools (automated software testing, distributed version control, ...)</li>
<li>Research (natural language, graph theory, distributed computing, ...)</li>
</ul>
</div></blockquote>
<p>An unusually large number of niches – versatile</p>
<p>Used by:</p>
<ul class="simple">
<li>Beginners</li>
<li>Professional software developers, computer system administrators, ...</li>
<li>Professionals OTHER THAN computer specialists: biologists, urban planners, ....</li>
</ul>
<p>An unusually large number of types of users – versatile</p>
<p>You can be productive in Python WITHOUT full-time immersion!</p>
</div>
<div class="section" id="python-features">
<h3>Python Features<a class="headerlink" href="#python-features" title="Permalink to this headline">¶</a></h3>
<p>Gets many things right:</p>
<ul class="simple">
<li>Readable – looks nice, makes sense</li>
<li>No ideology about best way to program – object-oriented programming, functional, etc.</li>
<li>No platform preference – Windows, Mac, Linux, ...</li>
<li>Easy to connect to other languages – C, Fortran - essential for science/math</li>
<li>Large standard library</li>
<li>Even larger network of external packages</li>
<li>Countless conveniences, large and small, make it pleasant to work with</li>
</ul>
</div>
<div class="section" id="what-is-python">
<h3>What is Python?<a class="headerlink" href="#what-is-python" title="Permalink to this headline">¶</a></h3>
<ul class="build simple">
<li>Dynamic</li>
<li>Object oriented</li>
<li>Byte-compiled</li>
<li>Interpreted</li>
</ul>
<p class="center large">But what does that mean?</p>
</div>
<div class="section" id="id1">
<h3>Python Features<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<ul class="build simple">
<li>Unlike C, C++, C#, Java ... More like Ruby, Lisp, Perl, Javascript
...</li>
<li><strong>Dynamic</strong> – no type declarations<ul>
<li>Programs are shorter</li>
<li>Programs are more flexible</li>
<li>Less code means fewer bugs</li>
</ul>
</li>
<li><strong>Interpreted</strong> – no separate compile, build steps - programming process is
simpler</li>
</ul>
</div>
<div class="section" id="what-s-a-dynamic-language">
<h3>What’s a Dynamic language<a class="headerlink" href="#what-s-a-dynamic-language" title="Permalink to this headline">¶</a></h3>
<p><strong>Dynamic typing</strong>.</p>
<ul class="simple">
<li>Type checking and dispatch happen at run-time</li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="n">x</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</pre></div>
</div>
<ul class="build simple">
<li>What is <tt class="docutils literal"><span class="pre">a</span></tt>?</li>
<li>What is <tt class="docutils literal"><span class="pre">b</span></tt>?</li>
<li>What does it mean to add them?</li>
<li><tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> can change at any time before this process</li>
</ul>
<p><strong>Strong typing</strong>.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="n">a</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">In [2]: </span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gh">Out[2]: </span><span class="go">int</span>
<span class="gp">In [3]: </span><span class="n">b</span> <span class="o">=</span> <span class="s">'5'</span>
<span class="gp">In [4]: </span><span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="gh">Out[4]: </span><span class="go">str</span>
</pre></div>
</div>
<ul class="build simple">
<li><strong>everything</strong> has a type.</li>
<li>the <em>type</em> of a thing determines what it can do.</li>
</ul>
</div>
<div class="section" id="duck-typing">
<h3>Duck Typing<a class="headerlink" href="#duck-typing" title="Permalink to this headline">¶</a></h3>
<p class="center large">“If it looks like a duck, and quacks like a duck – it’s probably a duck”</p>
<p class="center large">If an object behaves as expected at run-time, it’s the right type.</p>
</div>
<div class="section" id="python-versions">
<h3>Python Versions<a class="headerlink" href="#python-versions" title="Permalink to this headline">¶</a></h3>
<p>Python 2.x</p>
<ul class="build simple">
<li>“Classic” Python</li>
<li>Evolved from original</li>
</ul>
<p>Python 3.x (“py3k”)</p>
<ul class="build simple">
<li>Updated version</li>
<li>Removed the “warts”</li>
<li>Allowed to break code</li>
</ul>
<p>This class uses Python 2.7 not Python 3.x</p>
<ul class="build simple">
<li>Adoption of Python 3 is growing fast<ul>
<li>A few key packages still not supported (<a class="reference external" href="https://python3wos.appspot.com/">https://python3wos.appspot.com/</a>)</li>
<li>Most code in the wild is still 2.x</li>
</ul>
</li>
<li>You <em>can</em> learn to write Python that is forward compatible from 2.x to 3.x</li>
<li>We will cover that more later in the program.</li>
<li>If you find yourself needing to work with Python 2 and 3, there are ways to write compatible code: <a class="reference external" href="https://wiki.python.org/moin/PortingPythonToPy3k">https://wiki.python.org/moin/PortingPythonToPy3k</a></li>
</ul>
</div>
</div>
<div class="section" id="introduction-to-your-environment">
<h2>Introduction to Your Environment<a class="headerlink" href="#introduction-to-your-environment" title="Permalink to this headline">¶</a></h2>
<p>There are three basic elements to your environment when working with Python:</p>
<ul class="left build simple">
<li>Your Command Line</li>
<li>Your Interpreter</li>
<li>Your Editor</li>
</ul>
<div class="section" id="your-command-line-cli">
<h3>Your Command Line (cli)<a class="headerlink" href="#your-command-line-cli" title="Permalink to this headline">¶</a></h3>
<p>Having some facility on the command line is important</p>
<p>We won’t cover this in class, so if you are not comfortable, please bone up at
home.</p>
<p>I suggest running through the <strong>cli</strong> tutorial at “learn code the hard way”:</p>
<p><a class="reference external" href="http://cli.learncodethehardway.org/book">http://cli.learncodethehardway.org/book</a></p>
</div>
<div class="section" id="your-interpreter">
<h3>Your Interpreter<a class="headerlink" href="#your-interpreter" title="Permalink to this headline">¶</a></h3>
<p>Python comes with a built-in interpreter.</p>
<p>You see it when you type <tt class="docutils literal"><span class="pre">python</span></tt> at the command line:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span class="go">$ python</span>
<span class="go">Python 2.7.5 (default, Aug 25 2013, 00:04:04)</span>
<span class="go">[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin</span>
<span class="go">Type "help", "copyright", "credits" or "license" for more information.</span>
<span class="go">>>></span>
</pre></div>
</div>
<p>That last thing you see, <tt class="docutils literal"><span class="pre">>>></span></tt> is the “Python prompt”.</p>
<p>This is where you type code.</p>
<p>Try it out:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="s">"hello world!"</span>
<span class="go">hello world!</span>
<span class="gp">>>> </span><span class="mi">4</span> <span class="o">+</span> <span class="mi">5</span>
<span class="go">9</span>
<span class="gp">>>> </span><span class="mi">2</span> <span class="o">**</span> <span class="mi">8</span> <span class="o">-</span> <span class="mi">1</span>
<span class="go">255</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="s">"one string"</span> <span class="o">+</span> <span class="s">" plus another"</span>
<span class="go">one string plus another</span>
<span class="go">>>></span>
</pre></div>
</div>
<p>When you are in an interpreter, there are a number of tools available to
you.</p>
<p>There is a help system:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">help</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="go">Help on class str in module __builtin__:</span>
<span class="go">class str(basestring)</span>
<span class="go"> | str(object='') -> string</span>
<span class="go"> |</span>
<span class="go"> | Return a nice string representation of the object.</span>
<span class="go"> | If the argument is a string, the return value is the same object.</span>
<span class="go"> ...</span>
</pre></div>
</div>
<p>You can type <tt class="docutils literal"><span class="pre">q</span></tt> to exit the help viewer.</p>
<p>You can also use the <tt class="docutils literal"><span class="pre">dir</span></tt> builtin to find out about the attributes of a
given object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">bob</span> <span class="o">=</span> <span class="s">"this is a string"</span>
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">bob</span><span class="p">)</span>
<span class="go">['__add__', '__class__', '__contains__', '__delattr__',</span>
<span class="go"> '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',</span>
<span class="go"> '__getitem__', '__getnewargs__', '__getslice__', '__gt__',</span>
<span class="go"> ...</span>
<span class="go"> 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',</span>
<span class="go"> 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',</span>
<span class="go"> 'zfill']</span>
<span class="gp">>>> </span><span class="n">help</span><span class="p">(</span><span class="n">bob</span><span class="o">.</span><span class="n">rpartition</span><span class="p">)</span>
</pre></div>
</div>
<p>This allows you quite a bit of latitude in exploring what Python is.</p>
<p>In addition to the built-in interpreter, there are several more advanced
interpreters available to you.</p>
<p>We’ll be using one in this course called <tt class="docutils literal"><span class="pre">iPython</span></tt></p>
<p>More on this soon.</p>
</div>
<div class="section" id="your-editor">
<h3>Your Editor<a class="headerlink" href="#your-editor" title="Permalink to this headline">¶</a></h3>
<p>Typing code in an interpreter is great for exploring.</p>
<p>But for anything “real”, you’ll want to save the work you are doing in a more permanent
fashion.</p>
<p>This is where an Editor fits in.</p>
<p>Any good text editor will do.</p>
<p>MS Word is <strong>not</strong> a text editor.</p>
<p>Nor is <em>TextEdit</em> on a Mac.</p>
<p><tt class="docutils literal"><span class="pre">Notepad</span></tt> is a text editor – but a crappy one.</p>
<p>You need a real “programmers text editor”</p>
<p>A text editor saves only what it shows you, with no special formatting
characters hidden behind the scenes.</p>
<p>At a minimum, your editor should have:</p>
<ul class="build simple">
<li>Syntax Colorization</li>
<li>Automatic Indentation</li>
</ul>
<p>In addition, great features to add include:</p>
<ul class="build simple">
<li>Tab completion</li>
<li>Code linting</li>
<li>Jump-to-definition</li>
</ul>
<p>Have an editor that does all this? Feel free to use it.</p>
<p>If not, I suggest <tt class="docutils literal"><span class="pre">SublimeText</span></tt>:</p>
<p><a class="reference external" href="http://www.sublimetext.com/">http://www.sublimetext.com/</a></p>
</div>
<div class="section" id="why-no-ide">
<h3>Why No IDE?<a class="headerlink" href="#why-no-ide" title="Permalink to this headline">¶</a></h3>
<p>I am often asked this question.</p>
<p>An IDE does not give you much that you can’t get with a good editor plus a good
interpreter.</p>
<p>An IDE often weighs a great deal</p>
<p>Setting up IDEs to work with different projects can be challenging and
time-consuming.</p>
<p>Particularly when you are first learning, you don’t want too much done for you.</p>
<p class="center large">YAGNI</p>
</div>
</div>
<div class="section" id="setting-up-your-environment">
<h2>Setting Up Your Environment<a class="headerlink" href="#setting-up-your-environment" title="Permalink to this headline">¶</a></h2>
<p class="centered large">Shared setup means reduced complications.</p>
<div class="section" id="our-class-environment">
<h3>Our Class Environment<a class="headerlink" href="#our-class-environment" title="Permalink to this headline">¶</a></h3>
<p>We are going to work from a common environment in this class.</p>
<p>We will take the time here in class to get this going.</p>
<p>This helps to ensure that you will be able to work.</p>
</div>
<div class="section" id="step-1-python-2-7">
<h3>Step 1: Python 2.7<a class="headerlink" href="#step-1-python-2-7" title="Permalink to this headline">¶</a></h3>
<p class="large">You have this already, RIGHT?</p>
<div class="highlight-bash"><div class="highlight"><pre>$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
$
</pre></div>
</div>
<p>If not:</p>
<blockquote>
<div><ul class="simple">
<li><a class="reference external" href="./supplements/python_for_mac.html">For the mac</a></li>
<li><a class="reference external" href="./supplements/python_for_linux.html">For linux</a></li>
<li><a class="reference external" href="./supplements/python_for_windows.html">For windows</a></li>
</ul>
</div></blockquote>
</div>
<div class="section" id="step-2-pip">
<h3>Step 2: Pip<a class="headerlink" href="#step-2-pip" title="Permalink to this headline">¶</a></h3>
<p>Python comes with quite a bit (“batteries included”).</p>
<p>Sometimes you need a bit more.</p>
<p>Pip allows you to install Python packages to expand your system.</p>
<p>You install it by downloading and then executing an installer script:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>curl -O https://bootstrap.pypa.io/get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1309k 100 1309k 0 0 449k 0 0:00:02 0:00:02 --:--:-- 449k
<span class="nv">$ </span>python get-pip.py
</pre></div>
</div>
<p>(or go to: <a class="reference external" href="http://pip.readthedocs.org/en/latest/installing.html">http://pip.readthedocs.org/en/latest/installing.html</a>)</p>
<p>(Windows users will need to do that....)</p>
<p>Once you’ve installed pip, you use it to install Python packages by name:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>pip install foobar
...
</pre></div>
</div>
<p>To find packages (and their proper names), you can search the python
package index (PyPI):</p>
<p><a class="reference external" href="https://pypi.python.org/pypi">https://pypi.python.org/pypi</a></p>
</div>
<div class="section" id="step-3-install-ipython">
<h3>Step 3: Install iPython<a class="headerlink" href="#step-3-install-ipython" title="Permalink to this headline">¶</a></h3>
<p>As this is an intro class, we are going to use almost entirely features
of standard library. But there are a couple things you may want:</p>
<p><strong>iPython</strong></p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$pip</span> install ipython
</pre></div>
</div>
<p>If you are using SublimeText, you may want:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>pip install PdbSublimeTextSupport
</pre></div>
</div>
</div>
<div class="section" id="step-4-clone-class-repository">
<h3>Step 4: Clone Class Repository<a class="headerlink" href="#step-4-clone-class-repository" title="Permalink to this headline">¶</a></h3>
<p><a class="reference external" href="www.github.com">gitHub</a> is an industry-standard system for
collaboration on software projects – particularly open source ones.</p>
<p>We will use it this class to manage submitting and reviewing your work, etc.</p>
<p><strong>Wait!</strong> Don’t have a gitHub account? Set one up now.</p>
<p>Next, you’ll make a copy of the class repository using <tt class="docutils literal"><span class="pre">git</span></tt>.</p>
<p>The canonical copy is in the UWPCE organization on GitHub:</p>
<p><a class="reference external" href="https://github.com/UWPCE-PythonCert/IntroToPython">https://github.com/UWPCE-PythonCert/IntroToPython</a></p>
<p>Open that URL, and click on the <em>Fork</em> button at the top right corner.</p>
<p>This will make a copy of this repository in <em>your</em> github account.</p>
<p>From here, you’ll want to make a clone of your copy on your local machine.</p>
<p>At your command line, run the following commands:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span><span class="nb">cd </span>your_working_directory_for_the_class
<span class="nv">$ </span>git clone https://github.com/<yourname>/IntroToPython.git
</pre></div>
</div>
<p>(you can copy and paste that link from the gitHub page)</p>
<p><strong>Remember</strong>, <yourname> should be replaced by your github account name.</p>
</div>
</div>
<div class="section" id="introduction-to-ipython">
<h2>Introduction to iPython<a class="headerlink" href="#introduction-to-ipython" title="Permalink to this headline">¶</a></h2>
<div class="section" id="ipython-overview">
<h3>iPython Overview<a class="headerlink" href="#ipython-overview" title="Permalink to this headline">¶</a></h3>
<p>You have installed <a class="reference external" href="http://ipython.org">iPython</a>.</p>
<p>iPython is an advanced Python interpreter that offers enhancements.</p>
<p>You can read more about it in the <a class="reference external" href="http://ipython.org/ipython-doc/stable/index.html">official documentation</a>.</p>
<p>Specifically, you’ll want to pay attention to the information about</p>
<p><a class="reference external" href="http://ipython.org/ipython-doc/stable/interactive/index.html">Using iPython for Interactive Work</a>.</p>
</div>
<div class="section" id="the-very-basics-of-ipython">
<h3>The very basics of iPython<a class="headerlink" href="#the-very-basics-of-ipython" title="Permalink to this headline">¶</a></h3>
<p>iPython can do a lot for you, but for starters, here are the key pieces
you’ll want to know:</p>
<p>Start it up</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ipython</span>
<span class="nv">$ </span>ipython
Python 2.7.6 <span class="o">(</span>v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54<span class="o">)</span>
Type <span class="s2">"copyright"</span>, <span class="s2">"credits"</span> or <span class="s2">"license"</span> <span class="k">for </span>more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython<span class="s1">'s features.</span>
<span class="s1">%quickref -> Quick reference.</span>
<span class="s1">help -> Python'</span>s own <span class="nb">help </span>system.
object? -> Details about <span class="s1">'object'</span>, use <span class="s1">'object??'</span> <span class="k">for </span>extra details.
</pre></div>
</div>
<p>This is the stuff I use every day:</p>
<ul class="simple">
<li>command line recall:<ul>
<li>hit the “up arrow” key</li>
<li>if you have typed a bit, it will find the last command that starts the same way.</li>
</ul>
</li>
<li>basic shell commands:<ul>
<li><tt class="docutils literal"><span class="pre">ls</span></tt>, <tt class="docutils literal"><span class="pre">cd</span></tt>, <tt class="docutils literal"><span class="pre">pwd</span></tt></li>
</ul>
</li>
<li>any shell command:</li>
</ul>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">!</span> <span class="pre">the_shell_command</span></tt></li>
</ul>
</div></blockquote>
<ul class="simple">
<li>pasting from the clipboard:<ul>
<li><tt class="docutils literal"><span class="pre">%paste</span></tt> (this keeps whitespace cleaner for you)</li>
</ul>
</li>
</ul>
<ul class="simple">
<li>getting help:<ul>
<li><tt class="docutils literal"><span class="pre">something?</span></tt></li>
</ul>
</li>
<li>tab completion:<ul>
<li><tt class="docutils literal"><span class="pre">something.<tab></span></tt></li>
</ul>
</li>
<li>running a python file:<ul>
<li><tt class="docutils literal"><span class="pre">run</span> <span class="pre">the_name_of_the_file.py</span></tt></li>
</ul>
</li>
</ul>
<p>That’s it – you can get a lot done with those.</p>
</div>
<div class="section" id="how-to-run-a-python-file">
<h3>How to run a python file<a class="headerlink" href="#how-to-run-a-python-file" title="Permalink to this headline">¶</a></h3>
<p>A file with python code in it is a ‘module’ or ‘script’</p>
<p>(more on the distinction later on...)</p>
<p>It should be named with the <tt class="docutils literal"><span class="pre">.py</span></tt> extension: <tt class="docutils literal"><span class="pre">some_name.py</span></tt></p>
<p>To run it, you have a couple options:</p>
<ol class="arabic simple">
<li>call python on the command line, and pass in your module name</li>
</ol>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>python the_name_of_the_script.py
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li>run <tt class="docutils literal"><span class="pre">iPython</span></tt>, and run it from within iPython with the <tt class="docutils literal"><span class="pre">run</span></tt> command</li>
</ol>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="n">run</span> <span class="n">the_file</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="basic-python-syntax">
<h2>Basic Python Syntax<a class="headerlink" href="#basic-python-syntax" title="Permalink to this headline">¶</a></h2>
<p>(Follow along in the iPython interpreter...)</p>
<div class="center mlarge line-block">
<div class="line">Values, Types, and Symbols</div>
<div class="line"><br /></div>
<div class="line">Expressions and Statements</div>
</div>
<div class="section" id="values">
<h3>Values<a class="headerlink" href="#values" title="Permalink to this headline">¶</a></h3>
<p>All of programming is really about manipulating values.</p>
<ul class="build simple">
<li>Values are pieces of unnamed data: <tt class="docutils literal"><span class="pre">42,</span> <span class="pre">'Hello,</span> <span class="pre">world',</span></tt></li>
<li>In Python, all values are objects<ul>
<li>Try <tt class="docutils literal"><span class="pre">dir(42)</span></tt> - lots going on behind the curtain!</li>
</ul>
</li>
<li>Every value belongs to a type<ul>
<li>Try <tt class="docutils literal"><span class="pre">type(42)</span></tt> - the type of a value determines what it can do</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="literals-for-the-basic-value-types">
<h3>Literals for the Basic Value types:<a class="headerlink" href="#literals-for-the-basic-value-types" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt>Numbers:</dt>
<dd><ul class="first last simple">
<li>floating point: <tt class="docutils literal"><span class="pre">3.4</span></tt></li>
<li>integers: <tt class="docutils literal"><span class="pre">456</span></tt></li>
</ul>
</dd>
<dt>Text:</dt>
<dd><ul class="first last simple">
<li><tt class="docutils literal"><span class="pre">"a</span> <span class="pre">bit</span> <span class="pre">of</span> <span class="pre">text"</span></tt></li>
<li><tt class="docutils literal"><span class="pre">'a</span> <span class="pre">bit</span> <span class="pre">of</span> <span class="pre">text'</span></tt></li>
<li>(either single or double quotes work – why?)</li>
</ul>
</dd>
<dt>Boolean values:</dt>
<dd><ul class="first last simple">
<li><tt class="docutils literal"><span class="pre">True</span></tt></li>
<li><tt class="docutils literal"><span class="pre">False</span></tt></li>
</ul>
</dd>
</dl>
<p>(There are intricacies to all of these that we’ll get into later)</p>
</div>
<div class="section" id="code-structure">
<h3>Code structure<a class="headerlink" href="#code-structure" title="Permalink to this headline">¶</a></h3>
<p>Each line is a piece of code.</p>
<p>Comments:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [3]: </span><span class="c"># everything after a '#' is a comment</span>
</pre></div>
</div>
<p>Expressions:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [4]: </span><span class="c"># evaluating an expression results in a value</span>
<span class="gp">In [5]: </span><span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span>
<span class="gh">Out[5]: </span><span class="go">7</span>
</pre></div>
</div>
<p>Statements:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [6]: </span><span class="c"># statements do not return a value, may contain an expression</span>
<span class="gp">In [7]: </span><span class="k">print</span> <span class="s">"this"</span>
<span class="go">this</span>
<span class="gp">In [8]: </span><span class="n">line_count</span> <span class="o">=</span> <span class="mi">42</span>
<span class="gp">In [9]:</span>
</pre></div>
</div>
<p>It’s kind of obvious, but handy when playing with code:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="k">print</span> <span class="s">"something"</span>
<span class="go">something</span>
</pre></div>
</div>
<p>You can print multiple things:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [2]: </span><span class="k">print</span> <span class="s">"the value is"</span><span class="p">,</span> <span class="mi">5</span>
<span class="go">the value is 5</span>
</pre></div>
</div>
<p>Python automatically adds a newline, which you can suppress with a comma:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [12]: </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
<span class="gp"> ....: </span> <span class="k">print</span> <span class="s">"the value is"</span><span class="p">,</span>
<span class="gp"> ....: </span> <span class="k">print</span> <span class="n">i</span>
<span class="gp"> ....:</span>
<span class="go">the value is 0</span>
<span class="go">the value is 1</span>
<span class="go">the value is 2</span>
<span class="go">the value is 3</span>
</pre></div>
</div>
<p>Any python object can be printed (though it might not be pretty...)</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="k">class</span> <span class="nc">bar</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp"> ...: </span> <span class="k">pass</span>
<span class="gp"> ...:</span>
<span class="gp">In [2]: </span><span class="k">print</span> <span class="n">bar</span>
<span class="go"><class '__main__.bar'></span>
</pre></div>
</div>
<p>Blocks of code are delimited by a colon and indentation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">a_function</span><span class="p">():</span>
<span class="n">a_new_code_block</span>
<span class="n">end_of_the_block</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span>
<span class="k">print</span> <span class="n">i</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
<span class="n">do_something_bad</span><span class="p">()</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">fix_the_problem</span><span class="p">()</span>
</pre></div>
</div>
<p>Python uses indentation to delineate structure.</p>
<p>This means that in Python, whitespace is <strong>significant</strong>.</p>
<p>(but <strong>ONLY</strong> for newlines and indentation)</p>
<p>The standard is to indent with <strong>4 spaces</strong>.</p>
<p><strong>SPACES ARE NOT TABS</strong></p>
<p><strong>TABS ARE NOT SPACES</strong></p>
<p>These two blocks look the same:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span>
<span class="k">print</span> <span class="n">i</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span>
<span class="k">print</span> <span class="n">i</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<p>But they are not:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span>
\<span class="n">s</span>\<span class="n">s</span>\<span class="n">s</span>\<span class="n">sprint</span> <span class="n">i</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span>
\<span class="n">tprint</span> <span class="n">i</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<p><strong>ALWAYS INDENT WITH 4 SPACES</strong></p>
<p class="center large">NEVER INDENT WITH TABS</p>
<p>Make sure your editor is set to use spaces only –</p>
<p>Even when you hit the <tab> key</p>
</div>
<div class="section" id="expressions">
<h3>Expressions<a class="headerlink" href="#expressions" title="Permalink to this headline">¶</a></h3>
<p>An <em>expression</em> is made up of values and operators.</p>
<ul class="build simple">
<li>An expression is evaluated to produce a new value: <tt class="docutils literal"><span class="pre">2</span> <span class="pre">+</span> <span class="pre">2</span></tt><ul>
<li>The Python interpreter can be used as a calculator to evaluate expressions</li>
</ul>
</li>
<li>Integer vs. float arithmetic<ul>
<li>(Python 3 smooths this out)</li>
<li>Always use <tt class="docutils literal"><span class="pre">/</span></tt> when you want float results, <tt class="docutils literal"><span class="pre">//</span></tt> when you want
floored (integer) results</li>
</ul>
</li>
<li>Type conversions<ul>
<li>This is the source of many errors, especially in handling text</li>
</ul>
</li>
<li>Type errors - checked at run time only</li>
</ul>
</div>
<div class="section" id="symbols">
<h3>Symbols<a class="headerlink" href="#symbols" title="Permalink to this headline">¶</a></h3>
<p>Symbols are how we give names to values (objects).</p>
<ul class="build simple">
<li>Symbols must begin with an underscore or letter</li>
<li>Symbols can contain any number of underscores, letters and numbers<ul>
<li>this_is_a_symbol</li>
<li>this_is_2</li>
<li>_AsIsThis</li>
<li>1butThisIsNot</li>
<li>nor-is-this</li>
</ul>
</li>
<li>Symbols don’t have a type; values do<ul>
<li>This is why python is ‘Dynamic’</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="symbols-and-type">
<h3>Symbols and Type<a class="headerlink" href="#symbols-and-type" title="Permalink to this headline">¶</a></h3>
<p>Evaluating the type of a <em>symbol</em> will return the type of the <em>value</em> to which
it is bound.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [19]: </span><span class="nb">type</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gh">Out[19]: </span><span class="go">int</span>
<span class="gp">In [20]: </span><span class="nb">type</span><span class="p">(</span><span class="mf">3.14</span><span class="p">)</span>
<span class="gh">Out[20]: </span><span class="go">float</span>
<span class="gp">In [21]: </span><span class="n">a</span> <span class="o">=</span> <span class="mi">42</span>
<span class="gp">In [22]: </span><span class="n">b</span> <span class="o">=</span> <span class="mf">3.14</span>
<span class="gp">In [23]: </span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gh">Out[23]: </span><span class="go">int</span>
<span class="gp">In [25]: </span><span class="n">a</span> <span class="o">=</span> <span class="n">b</span>
<span class="gp">In [26]: </span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gh">Out[26]: </span><span class="go">float</span>
</pre></div>
</div>
</div>
<div class="section" id="assignment">
<h3>Assignment<a class="headerlink" href="#assignment" title="Permalink to this headline">¶</a></h3>
<p>A <em>symbol</em> is <strong>bound</strong> to a <em>value</em> with the assignment operator: <tt class="docutils literal"><span class="pre">=</span></tt></p>
<ul class="build simple">
<li>This attaches a name to a value</li>
<li>A value can have many names (or none!)</li>
<li>Assignment is a statement, it returns no value</li>
</ul>
<p>Evaluating the name will return the value to which it is bound</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [26]: </span><span class="n">name</span> <span class="o">=</span> <span class="s">"value"</span>
<span class="gp">In [27]: </span><span class="n">name</span>
<span class="gh">Out[27]: </span><span class="go">'value'</span>
<span class="gp">In [28]: </span><span class="n">an_integer</span> <span class="o">=</span> <span class="mi">42</span>
<span class="gp">In [29]: </span><span class="n">an_integer</span>
<span class="gh">Out[29]: </span><span class="go">42</span>
<span class="gp">In [30]: </span><span class="n">a_float</span> <span class="o">=</span> <span class="mf">3.14</span>
<span class="gp">In [31]: </span><span class="n">a_float</span>
<span class="gh">Out[31]: </span><span class="go">3.14</span>
</pre></div>
</div>
</div>
<div class="section" id="variables">
<h3>Variables?<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h3>
<ul class="build simple">
<li>In most languages, what I’m calling symbols, or names, are called “variables”.</li>
<li>In fact, Ill probably call them variables in this class.</li>
<li>That’s because they are used, for the most part, for the same purposes.</li>
<li>But many of you defined a “variable” as something like:
“a place in memory that can store values”</li>
<li>That is <strong>NOT</strong> what a name in python is!</li>
<li>A name can be bound to a value – but that has nothing to do with a
location in memory.</li>
</ul>
</div>
<div class="section" id="in-place-assignment">
<h3>In-Place Assignment<a class="headerlink" href="#in-place-assignment" title="Permalink to this headline">¶</a></h3>
<p>You can also do “in-place” assignment with <tt class="docutils literal"><span class="pre">+=</span></tt>.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [32]: </span><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">In [33]: </span><span class="n">a</span>
<span class="gh">Out[33]: </span><span class="go">1</span>
<span class="gp">In [34]: </span><span class="n">a</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">In [35]: </span><span class="n">a</span>
<span class="gh">Out[35]: </span><span class="go">2</span>
<span class="gp">In [36]: </span><span class="n">a</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="gp">In [37]: </span><span class="n">a</span>
<span class="gh">Out[37]: </span><span class="go">3</span>
</pre></div>
</div>
<p>also: <tt class="docutils literal"><span class="pre">-=,</span> <span class="pre">*=,</span> <span class="pre">/=,</span> <span class="pre">**=,</span> <span class="pre">\%=</span></tt></p>
<p>(not quite – really in-place assignment for mutables....)</p>
</div>
<div class="section" id="multiple-assignment">
<h3>Multiple Assignment<a class="headerlink" href="#multiple-assignment" title="Permalink to this headline">¶</a></h3>
<p>You can assign multiple names from multiple expressions in one
statement</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [48]: </span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">In [49]: </span><span class="n">y</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">In [50]: </span><span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">x</span><span class="p">,</span> <span class="mi">3</span> <span class="o">**</span> <span class="n">y</span>
<span class="gp">In [51]: </span><span class="n">i</span>
<span class="gh">Out[51]: </span><span class="go">4</span>
<span class="gp">In [52]: </span><span class="n">j</span>