forked from swcarpentry/python-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstructors.html
976 lines (917 loc) · 66.2 KB
/
instructors.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Programming with Python</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Programming with Python</h1></a>
<h2 class="subtitle">Instructor’s Guide</h2>
<h2 id="legend">Legend</h2>
<p>We are using a dataset with records on inflammation from patients following an arthritis treatment.</p>
<p>We make reference in the lesson that this data is somehow strange. It is strange because it is fabricated! The script used to generate the inflammation data is included as <a href="tools/gen_inflammation.py"><code>tools/gen_inflammation.py</code></a>.</p>
<h2 id="overall">Overall</h2>
<p>This lesson is written as an introduction to Python, but its real purpose is to introduce the single most important idea in programming: how to solve problems by building functions, each of which can fit in a programmer’s working memory. In order to teach that, we must teach people a little about the mechanics of manipulating data with lists and file I/O so that their functions can do things they actually care about. Our teaching order tries to show practical uses of every idea as soon as it is introduced; instructors should resist the temptation to explain the “other 90%” of the language as well.</p>
<p>The final example asks them to build a command-line tool that works with the Unix pipe-and-filter model. We do this because it is a useful skill and because it helps learners see that the software they use isn’t magical. Tools like <code>grep</code> might be more sophisticated than the programs our learners can write at this point in their careers, but it’s crucial they realize this is a difference of scale rather than kind.</p>
<p>Explain that we use Python because:</p>
<ul>
<li>It’s free.</li>
<li>It has a lot of scientific libraries, and more are constantly being added.</li>
<li>It has a large scientific user community.</li>
<li>It’s easier for novices to learn than most of the mature alternatives. (Software Carpentry originally used Perl; when we switched, we found that we could cover as much material in two days in Python as we’d covered in three days in Perl, and that retention was higher.)</li>
</ul>
<p>We do <em>not</em> include instructions on running the Jupyter Notebook in the tutorial because we want to focus on the language rather than the tools. Instructors should, however, walk learners through some basic operations:</p>
<ul>
<li>Launch from the command line with <code>jupyter notebook</code>.</li>
<li>Create a new notebook.</li>
<li>Enter code or data in a cell and execute it.</li>
<li>Explain the difference between <code>In[#]</code> and <code>Out[#]</code>.</li>
</ul>
<p>Watching the instructor grow programs step by step is as helpful to learners as anything to do with Python. Resist the urge to update a single cell repeatedly (which is what you’d probably do in real life). Instead, clone the previous cell and write the update in the new copy so that learners have a complete record of how the program grew. Once you’ve done this, you can say, “Now why don’t we just break things into small functions right from the start?”</p>
<p>The discussion of command-line scripts assumes that students understand standard I/O and building filters, which are covered in the lesson on the shell.</p>
<h2 id="frequently-argued-issues-fai">Frequently Argued Issues (FAI)</h2>
<ul>
<li><p><code>import ... as ...</code> syntax.</p>
<p>This syntax is commonly used in the scientific Python community; it is explicitly recommended in documentation to <code>import numpy as np</code> and <code>import matplotlib.pyplot as plt</code>. Despite that, we have decided not to introduce aliasing imports in this novice lesson due to the additional cognitive load it puts on students, despite the typing that it saves. A good summary of arguments for and against can be found in <a href="https://github.com/swcarpentry/python-novice-inflammation/pull/61">PR #61</a>.</p>
<p>It is up to you as an individual instructor whether you want to introduce these aliases when you teach this lesson, but we encourage you to please read those arguments thoroughly before deciding one way or the other.</p></li>
<li><p>NumPy methods.</p>
<p>We used to use NumPy array methods in the first <a href="01-numpy.html">NumPy topic</a>. We switched these methods to the equivalent functions because a majority of instructors supported the change; see <a href="https://github.com/swcarpentry/python-novice-inflammation/pull/244">PR #244</a> for detailed arguments for and against the change.</p></li>
<li><p>Underscores vs. hyphens in filenames</p>
<p>We used to use hyphens in filenames in order to signify that these Python files should only be run as scripts and never imported. However, after some <a href="https://github.com/swcarpentry/python-novice-inflammation/pull/254">discussion</a>, including an informal Twitter poll, we switched over to underscores because many files that start off as Python scripts end up being imported eventually. For that reason, we also added <code>if __name__ == '__main__'</code> guards around <code>main()</code> calls, which is how real-world Python scripts ensure that imports do not result in side-effects.</p></li>
</ul>
<h2 id="analyzing-patient-data"><a href="01-numpy.html">Analyzing Patient Data</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="sorting-out-references"><span class="glyphicon glyphicon-pencil"></span>Sorting out references</h2>
</div>
<div class="panel-body">
<p>What does the following program print out?</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">first, second <span class="op">=</span> <span class="st">'Grace'</span>, <span class="st">'Hopper'</span>
third, fourth <span class="op">=</span> second, first
<span class="bu">print</span>(third, fourth)</code></pre></div>
<pre class="output"><code>Hopper Grace</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="slicing-strings"><span class="glyphicon glyphicon-pencil"></span>Slicing strings</h2>
</div>
<div class="panel-body">
<p>A section of an array is called a <a href="reference.html#slice">slice</a>. We can take slices of character strings as well:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">element <span class="op">=</span> <span class="st">'oxygen'</span></code></pre></div>
<p>What is the value of <code>element[:4]</code>? What about <code>element[4:]</code>? Or <code>element[:]</code>?</p>
<pre class="output"><code>oxyg
en
oxygen</code></pre>
<p>What is <code>element[-1]</code>? What is <code>element[-2]</code>?</p>
<pre class="output"><code>n
e</code></pre>
<p>Given those answers, explain what <code>element[1:-1]</code> does.</p>
<pre class="output"><code>Creates a substring from index 1 up to (not including) the final index, effectively removing the first and last letters from 'oxygen'</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="thin-slices"><span class="glyphicon glyphicon-pencil"></span>Thin slices</h2>
</div>
<div class="panel-body">
<p>The expression <code>element[3:3]</code> produces an <a href="reference.html#empty-string">empty string</a>, i.e., a string that contains no characters. If <code>data</code> holds our array of patient data, what does <code>data[3:3, 4:4]</code> produce? What about <code>data[3:3, :]</code>?</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(data[<span class="dv">3</span>:<span class="dv">3</span>, <span class="dv">4</span>:<span class="dv">4</span>])
<span class="bu">print</span>(data[<span class="dv">3</span>:<span class="dv">3</span>, :])</code></pre></div>
<pre class="output"><code>[]
[]</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="check-your-understanding-plot-scaling"><span class="glyphicon glyphicon-pencil"></span>Check your understanding: plot scaling</h2>
</div>
<div class="panel-body">
<p>Why do all of our plots stop just short of the upper end of our graph? Update your plotting code to automatically set a more appropriate scale (hint: you can make use of the <code>max</code> and <code>min</code> methods to help)</p>
<pre class="output"><code>Because matplotlib normally sets x and y axes limits to the min and max of our data (depending on data range)</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># for example:</span>
axes3.set_ylabel(<span class="st">'min'</span>)
axes3.plot(numpy.<span class="bu">min</span>(data, axis<span class="op">=</span><span class="dv">0</span>))
axes3.set_ylim(<span class="dv">0</span>,<span class="dv">6</span>)
<span class="co"># or a more automated approach:</span>
min_data <span class="op">=</span> numpy.<span class="bu">min</span>(data, axis<span class="op">=</span><span class="dv">0</span>)
axes3.set_ylabel(<span class="st">'min'</span>)
axes3.plot(min_data)
axes3.set_ylim(numpy.<span class="bu">min</span>(min_data), numpy.<span class="bu">max</span>(min_data) <span class="op">*</span> <span class="fl">1.1</span>)</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="check-your-understanding-drawing-straight-lines"><span class="glyphicon glyphicon-pencil"></span>Check your understanding: drawing straight lines</h2>
</div>
<div class="panel-body">
<p>Why are the vertical lines in our plot of the minimum inflammation per day not perfectly vertical?</p>
<pre class="output"><code>Because matplotlib interpolates (draws a straight line) between the points</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="make-your-own-plot"><span class="glyphicon glyphicon-pencil"></span>Make your own plot</h2>
</div>
<div class="panel-body">
<p>Create a plot showing the standard deviation (<code>numpy.std</code>) of the inflammation data for each day across all patients.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">max_plot <span class="op">=</span> matplotlib.pyplot.plot(numpy.std(data, axis<span class="op">=</span><span class="dv">0</span>))
matplotlib.pyplot.show()</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="moving-plots-around"><span class="glyphicon glyphicon-pencil"></span>Moving plots around</h2>
</div>
<div class="panel-body">
<p>Modify the program to display the three plots on top of one another instead of side by side.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> numpy
<span class="im">import</span> matplotlib.pyplot
data <span class="op">=</span> numpy.loadtxt(fname<span class="op">=</span><span class="st">'data/inflammation-01.csv'</span>, delimiter<span class="op">=</span><span class="st">','</span>)
<span class="co"># change figsize (swap width and height)</span>
fig <span class="op">=</span> matplotlib.pyplot.figure(figsize<span class="op">=</span>(<span class="fl">3.0</span>, <span class="fl">10.0</span>))
<span class="co"># change add_subplot (swap first two parameters)</span>
axes1 <span class="op">=</span> fig.add_subplot(<span class="dv">3</span>, <span class="dv">1</span>, <span class="dv">1</span>)
axes2 <span class="op">=</span> fig.add_subplot(<span class="dv">3</span>, <span class="dv">1</span>, <span class="dv">2</span>)
axes3 <span class="op">=</span> fig.add_subplot(<span class="dv">3</span>, <span class="dv">1</span>, <span class="dv">3</span>)
axes1.set_ylabel(<span class="st">'average'</span>)
axes1.plot(numpy.mean(data, axis<span class="op">=</span><span class="dv">0</span>))
axes2.set_ylabel(<span class="st">'max'</span>)
axes2.plot(numpy.<span class="bu">max</span>(data, axis<span class="op">=</span><span class="dv">0</span>))
axes3.set_ylabel(<span class="st">'min'</span>)
axes3.plot(numpy.<span class="bu">min</span>(data, axis<span class="op">=</span><span class="dv">0</span>))
fig.tight_layout()
matplotlib.pyplot.show()</code></pre></div>
</div>
</section>
<h2 id="repeating-actions-with-loops"><a href="02-loop.html">Repeating Actions with Loops</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="from-1-to-n"><span class="glyphicon glyphicon-pencil"></span>From 1 to N</h2>
</div>
<div class="panel-body">
<p>Using <code>range</code>, write a loop that uses <code>range</code> to print the first 3 natural numbers.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">4</span>):
<span class="bu">print</span>(i)</code></pre></div>
<pre class="output"><code>1
2
3</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="computing-powers-with-loops"><span class="glyphicon glyphicon-pencil"></span>Computing powers with loops</h2>
</div>
<div class="panel-body">
<p>Write a loop that calculates the same result as <code>5 ** 3</code> using multiplication (and without exponentiation).</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">result <span class="op">=</span> <span class="dv">1</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">0</span>, <span class="dv">3</span>):
result <span class="op">=</span> result <span class="op">*</span> <span class="dv">5</span>
<span class="bu">print</span>(result)</code></pre></div>
<pre class="output"><code>125</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="reverse-a-string"><span class="glyphicon glyphicon-pencil"></span>Reverse a string</h2>
</div>
<div class="panel-body">
<p>Write a loop that takes a string, and produces a new string with the characters in reverse order.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">newstring <span class="op">=</span> <span class="st">''</span>
oldstring <span class="op">=</span> <span class="st">'Newton'</span>
length_old <span class="op">=</span> <span class="bu">len</span>(oldstring)
<span class="cf">for</span> char_index <span class="op">in</span> <span class="bu">range</span>(length_old):
newstring <span class="op">=</span> newstring <span class="op">+</span> oldstring[length_old <span class="op">-</span> char_index <span class="op">-</span> <span class="dv">1</span>]
<span class="bu">print</span>(newstring)</code></pre></div>
<pre class="output"><code>'notweN'</code></pre>
</div>
</section>
<p>After discussing these challenges could be a good time to introduce the <code>b *= 2</code> syntax.</p>
<h2 id="storing-multiple-values-in-lists"><a href="03-lists.html">Storing Multiple Values in Lists</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="turn-a-string-into-a-list"><span class="glyphicon glyphicon-pencil"></span>Turn a string into a list</h2>
</div>
<div class="panel-body">
<p>Use a <code>for</code> loop to convert the string <code>"hello"</code> into a list of letters:</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">my_list <span class="op">=</span> []
<span class="cf">for</span> char <span class="op">in</span> <span class="st">"hello"</span>:
my_list.append(char)
<span class="bu">print</span>(my_list)</code></pre></div>
<pre class="output"><code>["h", "e", "l", "l", "o"]</code></pre>
</div>
</section>
<h2 id="analyzing-data-from-multiple-files"><a href="04-files.html">Analyzing Data from Multiple Files</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="plotting-differences"><span class="glyphicon glyphicon-pencil"></span>Plotting Differences</h2>
</div>
<div class="panel-body">
<p>Plot the difference between the average of the first dataset and the average of the second dataset, i.e., the difference between the leftmost plot of the first two figures.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> glob
<span class="im">import</span> numpy
<span class="im">import</span> matplotlib.pyplot
filenames <span class="op">=</span> glob.glob(<span class="st">'data/inflammation*.csv'</span>)
data0 <span class="op">=</span> numpy.loadtxt(fname<span class="op">=</span>filenames[<span class="dv">0</span>], delimiter<span class="op">=</span><span class="st">','</span>)
data1 <span class="op">=</span> numpy.loadtxt(fname<span class="op">=</span>filenames[<span class="dv">1</span>], delimiter<span class="op">=</span><span class="st">','</span>)
fig <span class="op">=</span> matplotlib.pyplot.figure(figsize<span class="op">=</span>(<span class="fl">10.0</span>, <span class="fl">3.0</span>))
matplotlib.pyplot.ylabel(<span class="st">'Difference in average'</span>)
matplotlib.pyplot.plot(data0.mean(axis<span class="op">=</span><span class="dv">0</span>) <span class="op">-</span> data1.mean(axis<span class="op">=</span><span class="dv">0</span>))
fig.tight_layout()
matplotlib.pyplot.show()</code></pre></div>
</div>
</section>
<h2 id="making-choices"><a href="05-cond.html">Making Choices</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="how-many-paths"><span class="glyphicon glyphicon-pencil"></span>How many paths?</h2>
</div>
<div class="panel-body">
<p>Which of the following would be printed if you were to run this code? Why did you pick this answer?</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">if</span> <span class="dv">4</span> <span class="op">></span> <span class="dv">5</span>:
<span class="bu">print</span>(<span class="st">'A'</span>)
<span class="cf">elif</span> <span class="dv">4</span> <span class="op">==</span> <span class="dv">5</span>:
<span class="bu">print</span>(<span class="st">'B'</span>)
<span class="cf">elif</span> <span class="dv">4</span> <span class="op"><</span> <span class="dv">5</span>:
<span class="bu">print</span>(<span class="st">'C'</span>)</code></pre></div>
<hr />
<p>C gets printed, because the first two conditions, <code>4 > 5</code> and <code>4 == 5</code> are not true, but <code>4 < 5</code> is true.</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="what-is-truth"><span class="glyphicon glyphicon-pencil"></span>What is truth?</h2>
</div>
<div class="panel-body">
<p>After reading and running the code below, explain the rules for which values are considered true and which are considered false.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">if</span> <span class="st">''</span>:
<span class="bu">print</span>(<span class="st">'empty string is true'</span>)
<span class="cf">if</span> <span class="st">'word'</span>:
<span class="bu">print</span>(<span class="st">'word is true'</span>)
<span class="cf">if</span> []:
<span class="bu">print</span>(<span class="st">'empty list is true'</span>)
<span class="cf">if</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]:
<span class="bu">print</span>(<span class="st">'non-empty list is true'</span>)
<span class="cf">if</span> <span class="dv">0</span>:
<span class="bu">print</span>(<span class="st">'zero is true'</span>)
<span class="cf">if</span> <span class="dv">1</span>:
<span class="bu">print</span>(<span class="st">'one is true'</span>)</code></pre></div>
<hr />
<p>First line prints nothing: an empty string is false Second line prints <code>'word is true'</code>: a non-empty string is true Third line prints nothing: an empty list is false Fourth line prints <code>'non-empty list is true'</code>: a non-empty list is true Fifth line prints nothing: 0 is false Sixth line prints <code>'one is true'</code>: 1 is true</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="close-enough"><span class="glyphicon glyphicon-pencil"></span>Close enough</h2>
</div>
<div class="panel-body">
<p>Write some conditions that print <code>True</code> if the variable <code>a</code> is within 10% of the variable <code>b</code> and <code>False</code> otherwise.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">5</span>
b <span class="op">=</span> <span class="fl">5.1</span>
<span class="cf">if</span> <span class="bu">abs</span>(a <span class="op">-</span> b) <span class="op"><</span> <span class="fl">0.1</span> <span class="op">*</span> <span class="bu">abs</span>(b):
<span class="bu">print</span>(<span class="st">'True'</span>)
<span class="cf">else</span>:
<span class="bu">print</span>(<span class="st">'False'</span>)</code></pre></div>
<p>Another possible solution:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="bu">abs</span>(a <span class="op">-</span> b) <span class="op"><</span> <span class="fl">0.1</span> <span class="op">*</span> <span class="bu">abs</span>(b))</code></pre></div>
<p>This works because the boolean objects <code>True</code> and <code>False</code> have string representations which can be <code>print</code>ed.</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="in-place-operators"><span class="glyphicon glyphicon-pencil"></span>In-place operators</h2>
</div>
<div class="panel-body">
<p>Write some code that sums the positive and negative numbers in a list separately, using in-place operators.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">positive_sum <span class="op">=</span> <span class="dv">0</span>
negative_sum <span class="op">=</span> <span class="dv">0</span>
test_list <span class="op">=</span> [<span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">6</span>, <span class="dv">1</span>, <span class="op">-</span><span class="dv">1</span>, <span class="op">-</span><span class="dv">5</span>, <span class="dv">0</span>, <span class="dv">7</span>, <span class="op">-</span><span class="dv">8</span>]
<span class="cf">for</span> num <span class="op">in</span> test_list:
<span class="cf">if</span> num <span class="op">></span> <span class="dv">0</span>:
positive_sum <span class="op">+=</span> num
<span class="cf">elif</span> num <span class="op">==</span> <span class="dv">0</span>:
<span class="cf">pass</span>
<span class="cf">else</span>:
negative_sum <span class="op">+=</span> num
<span class="bu">print</span>(positive_sum, negative_sum)</code></pre></div>
<pre class="output"><code>21 -14</code></pre>
<p>Here <code>pass</code> means “don’t do anything”. In this particular case, it’s not actually needed, since if <code>num == 0</code> neither sum needs to change, but it illustrates the use of <code>elif</code>.</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="tuples-and-exchanges"><span class="glyphicon glyphicon-pencil"></span>Tuples and exchanges</h2>
</div>
<div class="panel-body">
<p>Explain what the overall effect of this code is:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">left <span class="op">=</span> <span class="st">'L'</span>
right <span class="op">=</span> <span class="st">'R'</span>
temp <span class="op">=</span> left
left <span class="op">=</span> right
right <span class="op">=</span> temp</code></pre></div>
<hr />
<p>The code swaps the contents of the variables right and left.</p>
<p>Compare it to:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">left, right <span class="op">=</span> right, left</code></pre></div>
<hr />
<p>Do they always do the same thing? Which do you find easier to read?</p>
<hr />
<p>Yes, although it’s possible the internal implementation is different. Answers will vary on which is easier to read.</p>
</div>
</section>
<h2 id="creating-functions"><a href="06-func.html">Creating Functions</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="combining-strings"><span class="glyphicon glyphicon-pencil"></span>Combining strings</h2>
</div>
<div class="panel-body">
<p>Write a function called <code>fence</code> that takes two parameters called <code>original</code> and <code>wrapper</code> and returns a new string that has the wrapper character at the beginning and end of the original.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> fence(original, wrapper):
<span class="cf">return</span> wrapper <span class="op">+</span> original <span class="op">+</span> wrapper</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="selecting-characters-from-strings"><span class="glyphicon glyphicon-pencil"></span>Selecting characters from strings</h2>
</div>
<div class="panel-body">
<p>Write a function called <code>outer</code> that returns a string made up of just the first and last characters of its input.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> outer(input_string):
<span class="cf">return</span> input_string[<span class="dv">0</span>] <span class="op">+</span> input_string[<span class="op">-</span><span class="dv">1</span>]</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="rescaling-an-array"><span class="glyphicon glyphicon-pencil"></span>Rescaling an array</h2>
</div>
<div class="panel-body">
<p>Write a function <code>rescale</code> that takes an array as input and returns a corresponding array of values scaled to lie in the range 0.0 to 1.0. (Hint: If <span class="math inline">\(L\)</span> and <span class="math inline">\(H\)</span> are the lowest and highest values in the original array, then the replacement for a value <span class="math inline">\(v\)</span> should be <span class="math inline">\((v-L) / (H-L)\)</span>.)</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> rescale(input_array):
L <span class="op">=</span> numpy.<span class="bu">min</span>(input_array)
H <span class="op">=</span> numpy.<span class="bu">max</span>(input_array)
output_array <span class="op">=</span> (input_array <span class="op">-</span> L) <span class="op">/</span> (H <span class="op">-</span> L)
<span class="cf">return</span> output_array</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="testing-and-documenting-your-function"><span class="glyphicon glyphicon-pencil"></span>Testing and documenting your function</h2>
</div>
<div class="panel-body">
<p>Run the commands <code>help(numpy.arange)</code> and <code>help(numpy.linspace)</code> to see how to use these functions to generate regularly-spaced values, then use those values to test your <code>rescale</code> function. Once you’ve successfully tested your function, add a docstring that explains what it does.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">'''Takes an array as input, and returns a corresponding array scaled so</span>
<span class="co">that 0 corresponds to the minimum and 1 to the maximum value of the input array.</span>
<span class="co">Examples:</span>
<span class="co">>>> rescale(numpy.arange(10.0))</span>
<span class="co">array([ 0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,</span>
<span class="co"> 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])</span>
<span class="co">>>> rescale(numpy.linspace(0, 100, 5))</span>
<span class="co">array([ 0. , 0.25, 0.5 , 0.75, 1. ])</span>
<span class="co">'''</span></code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="defining-defaults"><span class="glyphicon glyphicon-pencil"></span>Defining defaults</h2>
</div>
<div class="panel-body">
<p>Rewrite the <code>rescale</code> function so that it scales data to lie between 0.0 and 1.0 by default, but will allow the caller to specify lower and upper bounds if they want. Compare your implementation to your neighbor’s: do the two functions always behave the same way?</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> rescale(input_array, low_val<span class="op">=</span><span class="fl">0.0</span>, high_val<span class="op">=</span><span class="fl">1.0</span>):
<span class="co">'''rescales input array values to lie between low_val and high_val'''</span>
L <span class="op">=</span> numpy.<span class="bu">min</span>(input_array)
H <span class="op">=</span> numpy.<span class="bu">max</span>(input_array)
intermed_array <span class="op">=</span> (input_array <span class="op">-</span> L) <span class="op">/</span> (H <span class="op">-</span> L)
output_array <span class="op">=</span> intermed_array <span class="op">*</span> (high_val <span class="op">-</span> low_val) <span class="op">+</span> low_val
<span class="cf">return</span> output_array</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="variables-inside-and-outside-functions"><span class="glyphicon glyphicon-pencil"></span>Variables inside and outside functions</h2>
</div>
<div class="panel-body">
<p>What does the following piece of code display when run - and why?</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">f <span class="op">=</span> <span class="dv">0</span>
k <span class="op">=</span> <span class="dv">0</span>
<span class="kw">def</span> f2k(f):
k <span class="op">=</span> ((f<span class="dv">-32</span>)<span class="op">*</span>(<span class="fl">5.0</span><span class="op">/</span><span class="fl">9.0</span>)) <span class="op">+</span> <span class="fl">273.15</span>
<span class="cf">return</span> k
<span class="bu">print</span>(f2k(<span class="dv">8</span>))
<span class="bu">print</span>(f2k(<span class="dv">41</span>))
<span class="bu">print</span>(f2k(<span class="dv">32</span>))
<span class="bu">print</span>(k)</code></pre></div>
<hr />
<pre class="output"><code>259.81666666666666
287.15
273.15
0</code></pre>
<p><code>k</code> is 0 because the <code>k</code> inside the function <code>f2k</code> doesn’t know about the <code>k</code> defined outside the function.</p>
</div>
</section>
<h2 id="errors-and-exceptions"><a href="07-errors.html">Errors and Exceptions</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="reading-error-messages"><span class="glyphicon glyphicon-pencil"></span>Reading Error Messages</h2>
</div>
<div class="panel-body">
<p>Read the traceback below, and identify the following pieces of information about it:</p>
<ol style="list-style-type: decimal">
<li>How many levels does the traceback have?</li>
<li>What is the file name where the error occurred?</li>
<li>What is the function name where the error occurred?</li>
<li>On which line number in this function did the error occurr?</li>
<li>What is the type of error?</li>
<li>What is the error message?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> errors_02
errors_02.print_friday_message()</code></pre></div>
<pre class="error"><code>---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-2-e4c4cbafeeb5> in <module>()
1 import errors_02
----> 2 errors_02.print_friday_message()
/Users/jhamrick/project/swc/novice/python/errors_02.py in print_friday_message()
13
14 def print_friday_message():
---> 15 print_message("Friday")
/Users/jhamrick/project/swc/novice/python/errors_02.py in print_message(day)
9 "sunday": "Aw, the weekend is almost over."
10 }
---> 11 print(messages[day])
12
13
KeyError: 'Friday'</code></pre>
<hr />
<ol style="list-style-type: decimal">
<li>3 levels</li>
<li><code>errors_02.py</code></li>
<li><code>print_message</code></li>
<li>11</li>
<li><code>KeyError</code></li>
<li>There isn’t really a message; you’re supposed to infer that <code>Friday</code> is not a key in <code>messages</code>.</li>
</ol>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="identifying-syntax-errors"><span class="glyphicon glyphicon-pencil"></span>Identifying Syntax Errors</h2>
</div>
<div class="panel-body">
<ol style="list-style-type: decimal">
<li>Read the code below, and (without running it) try to identify what the errors are.</li>
<li>Run the code, and read the error message. Is it a <code>SyntaxError</code> or an <code>IndentationError</code>?</li>
<li>Fix the error.</li>
<li>Repeat steps 2 and 3, until you have fixed all the errors.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> another_function
<span class="bu">print</span>(<span class="st">"Syntax errors are annoying."</span>)
<span class="bu">print</span>(<span class="st">"But at least python tells us about them!"</span>)
<span class="bu">print</span>(<span class="st">"So they are usually not too hard to fix."</span>)</code></pre></div>
<hr />
<p><code>SyntaxError</code> for missing <code>():</code> at end of first line, <code>IndentationError</code> for mismatch between second and third lines.</p>
<p>Fixed version:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> another_function():
<span class="bu">print</span>(<span class="st">"Syntax errors are annoying."</span>)
<span class="bu">print</span>(<span class="st">"But at least python tells us about them!"</span>)
<span class="bu">print</span>(<span class="st">"So they are usually not too hard to fix."</span>)</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="identifying-variable-name-errors"><span class="glyphicon glyphicon-pencil"></span>Identifying Variable Name Errors</h2>
</div>
<div class="panel-body">
<ol style="list-style-type: decimal">
<li>Read the code below, and (without running it) try to identify what the errors are.</li>
<li>Run the code, and read the error message. What type of <code>NameError</code> do you think this is? In other words, is it a string with no quotes, a misspelled variable, or a variable that should have been defined but was not?</li>
<li>Fix the error.</li>
<li>Repeat steps 2 and 3, until you have fixed all the errors.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> number <span class="op">in</span> <span class="bu">range</span>(<span class="dv">10</span>):
<span class="co"># use a if the number is a multiple of 3, otherwise use b</span>
<span class="cf">if</span> (Number <span class="op">%</span> <span class="dv">3</span>) <span class="op">==</span> <span class="dv">0</span>:
message <span class="op">=</span> message <span class="op">+</span> a
<span class="cf">else</span>:
message <span class="op">=</span> message <span class="op">+</span> <span class="st">"b"</span>
<span class="bu">print</span>(message)</code></pre></div>
<hr />
<p>3 <code>NameError</code>s for <code>number</code> being misspelled, for <code>message</code> not defined, and for <code>a</code> not being in quotes.</p>
<p>Fixed version:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">message <span class="op">=</span> <span class="st">""</span>
<span class="cf">for</span> number <span class="op">in</span> <span class="bu">range</span>(<span class="dv">10</span>):
<span class="co"># use a if the number is a multiple of 3, otherwise use b</span>
<span class="cf">if</span> (number <span class="op">%</span> <span class="dv">3</span>) <span class="op">==</span> <span class="dv">0</span>:
message <span class="op">=</span> message <span class="op">+</span> <span class="st">"a"</span>
<span class="cf">else</span>:
message <span class="op">=</span> message <span class="op">+</span> <span class="st">"b"</span>
<span class="bu">print</span>(message)</code></pre></div>
<pre class="output"><code>abbabbabba</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="identifying-item-errors"><span class="glyphicon glyphicon-pencil"></span>Identifying Item Errors</h2>
</div>
<div class="panel-body">
<ol style="list-style-type: decimal">
<li>Read the code below, and (without running it) try to identify what the errors are.</li>
<li>Run the code, and read the error message. What type of error is it?</li>
<li>Fix the error.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">seasons <span class="op">=</span> [<span class="st">'Spring'</span>, <span class="st">'Summer'</span>, <span class="st">'Fall'</span>, <span class="st">'Winter'</span>]
<span class="bu">print</span>(<span class="st">'My favorite season is '</span>, seasons[<span class="dv">4</span>])</code></pre></div>
<hr />
<p><code>IndexError</code>; the last entry is <code>seasons[3]</code>, so <code>seasons[4]</code> doesn’t make sense.</p>
<p>Fixed version:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">seasons <span class="op">=</span> [<span class="st">'Spring'</span>, <span class="st">'Summer'</span>, <span class="st">'Fall'</span>, <span class="st">'Winter'</span>]
<span class="bu">print</span>(<span class="st">'My favorite season is '</span>, seasons[<span class="op">-</span><span class="dv">1</span>])</code></pre></div>
</div>
</section>
<h2 id="defensive-programming"><a href="08-defensive.html">Defensive Programming</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="pre--and-post-conditions"><span class="glyphicon glyphicon-pencil"></span>Pre- and post-conditions</h2>
</div>
<div class="panel-body">
<p>Suppose you are writing a function called <code>average</code> that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not hers or vice versa?</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># a possible pre-condition:</span>
<span class="cf">assert</span> <span class="bu">len</span>(<span class="bu">input</span>) <span class="op">></span> <span class="dv">0</span>, <span class="st">'List length must be non-zero'</span>
<span class="co"># a possible post-condition:</span>
<span class="cf">assert</span> numpy.<span class="bu">min</span>(<span class="bu">input</span>) <span class="op"><</span> average <span class="op"><</span> numpy.<span class="bu">max</span>(<span class="bu">input</span>), <span class="st">'Average should be between min and max of input values'</span></code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="testing-assertions"><span class="glyphicon glyphicon-pencil"></span>Testing assertions</h2>
</div>
<div class="panel-body">
<p>Given a sequence of values, the function <code>running</code> returns a list containing the running totals at each index. Explain in words what the assertions in this function check, and for each one, give an example of input that will make that assertion fail.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> running(values):
<span class="cf">assert</span> <span class="bu">len</span>(values) <span class="op">></span> <span class="dv">0</span>
result <span class="op">=</span> [values[<span class="dv">0</span>]]
<span class="cf">for</span> v <span class="op">in</span> values[<span class="dv">1</span>:]:
<span class="cf">assert</span> result[<span class="op">-</span><span class="dv">1</span>] <span class="op">>=</span> <span class="dv">0</span>
result.append(result[<span class="op">-</span><span class="dv">1</span>] <span class="op">+</span> v)
<span class="cf">assert</span> result[<span class="op">-</span><span class="dv">1</span>] <span class="op">>=</span> result[<span class="dv">0</span>]
<span class="cf">return</span> result</code></pre></div>
<hr />
<ul>
<li>The first assertion checks that the input sequence <code>values</code> is not empty. An empty sequence such as <code>[]</code> will make it fail.</li>
<li>The second assertion checks that the first value in the list is positive. Input such as <code>[-1,0,2,3]</code> will make it fail.</li>
<li>The third assertion checks that the running total always increases. Input such as <code>[0,1,3,-5,4]</code> will make it fail.</li>
</ul>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="fixing-and-testing"><span class="glyphicon glyphicon-pencil"></span>Fixing and testing</h2>
</div>
<div class="panel-body">
<p>Fix <code>range_overlap</code>. Re-run <code>test_range_overlap</code> after each change you make.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> numpy
<span class="kw">def</span> range_overlap(ranges):
<span class="co">'''Return common overlap among a set of [low, high] ranges.'''</span>
<span class="cf">if</span> <span class="bu">len</span>(ranges) <span class="op">==</span> <span class="dv">1</span>: <span class="co"># only one entry, so return it</span>
<span class="cf">return</span> ranges[<span class="dv">0</span>]
lowest <span class="op">=</span> <span class="op">-</span>numpy.inf <span class="co"># lowest possible number</span>
highest <span class="op">=</span> numpy.inf <span class="co"># highest possible number</span>
<span class="cf">for</span> (low, high) <span class="op">in</span> ranges:
lowest <span class="op">=</span> <span class="bu">max</span>(lowest, low)
highest <span class="op">=</span> <span class="bu">min</span>(highest, high)
<span class="cf">if</span> lowest <span class="op">>=</span> highest: <span class="co"># no overlap</span>
<span class="cf">return</span> <span class="va">None</span>
<span class="cf">else</span>:
<span class="cf">return</span> (lowest, highest)</code></pre></div>
</div>
</section>
<h2 id="debugging"><a href="09-debugging.html">Debugging</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="debug-the-following-problem"><span class="glyphicon glyphicon-pencil"></span>Debug the following problem</h2>
</div>
<div class="panel-body">
<p>This exercise has the aim of ensuring learners are able to step through unseen code with unexpected output to locate issues. The issues present are that:</p>
<ul>
<li><p>The loop is not being utilised correctly. <code>height</code> and <code>weight</code> are always set as the first patient’s data during each iteration of the loop.</p></li>
<li><p>The height/weight variables are reversed in the function call to <code>calculate_bmi(...)</code></p></li>
</ul>
</div>
</section>
<h2 id="command-line-programs"><a href="10-cmdline.html">Command-Line Programs</a></h2>
<p>Solutions to exercises:</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="arithmetic-on-the-command-line"><span class="glyphicon glyphicon-pencil"></span>Arithmetic on the command line</h2>
</div>
<div class="panel-body">
<p>Write a command-line program that does addition and subtraction:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">python</span> arith.py add 1 2</code></pre></div>
<pre class="output"><code>3</code></pre>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">python</span> arith.py subtract 3 4</code></pre></div>
<pre class="output"><code>-1</code></pre>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># this is code/arith.py</span>
<span class="im">import</span> sys
<span class="kw">def</span> main():
<span class="cf">assert</span> <span class="bu">len</span>(sys.argv) <span class="op">==</span> <span class="dv">4</span>, <span class="st">'Need exactly 3 arguments'</span>
operator <span class="op">=</span> sys.argv[<span class="dv">1</span>]
<span class="cf">assert</span> operator <span class="op">in</span> [<span class="st">'add'</span>, <span class="st">'subtract'</span>, <span class="st">'multiply'</span>, <span class="st">'divide'</span>], <span class="op">\</span>
<span class="co">'Operator is not one of add, subtract, multiply, or divide: bailing out'</span>
<span class="cf">try</span>:
operand1, operand2 <span class="op">=</span> <span class="bu">float</span>(sys.argv[<span class="dv">2</span>]), <span class="bu">float</span>(sys.argv[<span class="dv">3</span>])
<span class="cf">except</span> <span class="pp">ValueError</span>:
<span class="bu">print</span>(<span class="st">'cannot convert input to a number: bailing out'</span>)
<span class="cf">return</span>
do_arithmetic(operand1, operator, operand2)
<span class="kw">def</span> do_arithmetic(operand1, operator, operand2):
<span class="cf">if</span> operator <span class="op">==</span> <span class="st">'add'</span>:
value <span class="op">=</span> operand1 <span class="op">+</span> operand2
<span class="cf">elif</span> operator <span class="op">==</span> <span class="st">'subtract'</span>:
value <span class="op">=</span> operand1 <span class="op">-</span> operand2
<span class="cf">elif</span> operator <span class="op">==</span> <span class="st">'multiply'</span>:
value <span class="op">=</span> operand1 <span class="op">*</span> operand2
<span class="cf">elif</span> operator <span class="op">==</span> <span class="st">'divide'</span>:
value <span class="op">=</span> operand1 <span class="op">/</span> operand2
<span class="bu">print</span>(value)
main()</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="finding-particular-files"><span class="glyphicon glyphicon-pencil"></span>Finding particular files</h2>
</div>
<div class="panel-body">
<p>Using the <code>glob</code> module introduced <a href="04-files.html">earlier</a>, write a simple version of <code>ls</code> that shows files in the current directory with a particular suffix. A call to this script should look like this:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">python</span> my_ls.py py</code></pre></div>
<pre class="output"><code>left.py
right.py
zero.py</code></pre>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># this is code/my_ls.py</span>
<span class="im">import</span> sys
<span class="im">import</span> glob
<span class="kw">def</span> main():
<span class="co">'''prints names of all files with sys.argv as suffix'''</span>
<span class="cf">assert</span> <span class="bu">len</span>(sys.argv) <span class="op">>=</span> <span class="dv">2</span>, <span class="st">'Argument list cannot be empty'</span>
suffix <span class="op">=</span> sys.argv[<span class="dv">1</span>] <span class="co"># NB: behaviour is not as you'd expect if sys.argv[1] is *</span>
glob_input <span class="op">=</span> <span class="st">'*.'</span> <span class="op">+</span> suffix <span class="co"># construct the input</span>
glob_output <span class="op">=</span> <span class="bu">sorted</span>(glob.glob(glob_input)) <span class="co"># call the glob function</span>
<span class="cf">for</span> item <span class="op">in</span> glob_output: <span class="co"># print the output</span>
<span class="bu">print</span>(item)
<span class="cf">return</span>
main()</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="changing-flags"><span class="glyphicon glyphicon-pencil"></span>Changing flags</h2>
</div>
<div class="panel-body">
<p>Rewrite <code>readings.py</code> so that it uses <code>-n</code>, <code>-m</code>, and <code>-x</code> instead of <code>--min</code>, <code>--mean</code>, and <code>--max</code> respectively. Is the code easier to read? Is the program easier to understand?</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># this is code/readings_07.py</span>
<span class="im">import</span> sys
<span class="im">import</span> numpy
<span class="kw">def</span> main():
script <span class="op">=</span> sys.argv[<span class="dv">0</span>]
action <span class="op">=</span> sys.argv[<span class="dv">1</span>]
filenames <span class="op">=</span> sys.argv[<span class="dv">2</span>:]
<span class="cf">assert</span> action <span class="op">in</span> [<span class="st">'-n'</span>, <span class="st">'-m'</span>, <span class="st">'-x'</span>], <span class="op">\</span>
<span class="co">'Action is not one of -n, -m, or -x: '</span> <span class="op">+</span> action
<span class="cf">if</span> <span class="bu">len</span>(filenames) <span class="op">==</span> <span class="dv">0</span>:
process(sys.stdin, action)
<span class="cf">else</span>:
<span class="cf">for</span> f <span class="op">in</span> filenames:
process(f, action)
<span class="kw">def</span> process(filename, action):
data <span class="op">=</span> numpy.loadtxt(filename, delimiter<span class="op">=</span><span class="st">','</span>)
<span class="cf">if</span> action <span class="op">==</span> <span class="st">'-n'</span>:
values <span class="op">=</span> numpy.<span class="bu">min</span>(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">elif</span> action <span class="op">==</span> <span class="st">'-m'</span>:
values <span class="op">=</span> numpy.mean(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">elif</span> action <span class="op">==</span> <span class="st">'-x'</span>:
values <span class="op">=</span> numpy.<span class="bu">max</span>(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">for</span> m <span class="op">in</span> values:
<span class="bu">print</span>(m)
main()</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="adding-a-help-message"><span class="glyphicon glyphicon-pencil"></span>Adding a help message</h2>
</div>
<div class="panel-body">
<p>Separately, modify <code>readings.py</code> so that if no parameters are given (i.e., no action is specified and no filenames are given), it prints a message explaining how it should be used.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># this is code/readings_08.py</span>
<span class="im">import</span> sys
<span class="im">import</span> numpy
<span class="kw">def</span> main():
script <span class="op">=</span> sys.argv[<span class="dv">0</span>]
<span class="cf">if</span> <span class="bu">len</span>(sys.argv) <span class="op">==</span> <span class="dv">1</span>: <span class="co"># no arguments, so print help message</span>
<span class="bu">print</span>(<span class="st">"""Usage: python readings_08.py action filenames</span>
<span class="st"> action must be one of --min --mean --max</span>
<span class="st"> if filenames is blank, input is taken from stdin;</span>
<span class="st"> otherwise, each filename in the list of arguments is processed in turn"""</span>)
<span class="cf">return</span>
action <span class="op">=</span> sys.argv[<span class="dv">1</span>]
filenames <span class="op">=</span> sys.argv[<span class="dv">2</span>:]
<span class="cf">assert</span> action <span class="op">in</span> [<span class="st">'--min'</span>, <span class="st">'--mean'</span>, <span class="st">'--max'</span>], <span class="op">\</span>
<span class="co">'Action is not one of --min, --mean, or --max: '</span> <span class="op">+</span> action
<span class="cf">if</span> <span class="bu">len</span>(filenames) <span class="op">==</span> <span class="dv">0</span>:
process(sys.stdin, action)
<span class="cf">else</span>:
<span class="cf">for</span> f <span class="op">in</span> filenames:
process(f, action)
<span class="kw">def</span> process(filename, action):
data <span class="op">=</span> numpy.loadtxt(filename, delimiter<span class="op">=</span><span class="st">','</span>)
<span class="cf">if</span> action <span class="op">==</span> <span class="st">'--min'</span>:
values <span class="op">=</span> numpy.<span class="bu">min</span>(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">elif</span> action <span class="op">==</span> <span class="st">'--mean'</span>:
values <span class="op">=</span> numpy.mean(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">elif</span> action <span class="op">==</span> <span class="st">'--max'</span>:
values <span class="op">=</span> numpy.<span class="bu">max</span>(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">for</span> m <span class="op">in</span> values:
<span class="bu">print</span>(m)
main()</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="adding-a-default-action"><span class="glyphicon glyphicon-pencil"></span>Adding a default action</h2>
</div>
<div class="panel-body">
<p>Separately, modify <code>readings.py</code> so that if no action is given it displays the means of the data.</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># this is code/readings_09.py</span>
<span class="im">import</span> sys
<span class="im">import</span> numpy
<span class="kw">def</span> main():
script <span class="op">=</span> sys.argv[<span class="dv">0</span>]
action <span class="op">=</span> sys.argv[<span class="dv">1</span>]
<span class="cf">if</span> action <span class="op">not</span> <span class="op">in</span> [<span class="st">'--min'</span>, <span class="st">'--mean'</span>, <span class="st">'--max'</span>]: <span class="co"># if no action given</span>
action <span class="op">=</span> <span class="st">'--mean'</span> <span class="co"># set a default action, that being mean</span>
filenames <span class="op">=</span> sys.argv[<span class="dv">1</span>:] <span class="co"># start the filenames one place earlier in the argv list</span>
<span class="cf">else</span>:
filenames <span class="op">=</span> sys.argv[<span class="dv">2</span>:]
<span class="cf">if</span> <span class="bu">len</span>(filenames) <span class="op">==</span> <span class="dv">0</span>:
process(sys.stdin, action)
<span class="cf">else</span>:
<span class="cf">for</span> f <span class="op">in</span> filenames:
process(f, action)
<span class="kw">def</span> process(filename, action):
data <span class="op">=</span> numpy.loadtxt(filename, delimiter<span class="op">=</span><span class="st">','</span>)
<span class="cf">if</span> action <span class="op">==</span> <span class="st">'--min'</span>:
values <span class="op">=</span> numpy.<span class="bu">min</span>(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">elif</span> action <span class="op">==</span> <span class="st">'--mean'</span>:
values <span class="op">=</span> numpy.mean(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">elif</span> action <span class="op">==</span> <span class="st">'--max'</span>:
values <span class="op">=</span> numpy.<span class="bu">max</span>(data, axis<span class="op">=</span><span class="dv">1</span>)
<span class="cf">for</span> m <span class="op">in</span> values:
<span class="bu">print</span>(m)
main()</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="a-file-checker"><span class="glyphicon glyphicon-pencil"></span>A file-checker</h2>
</div>
<div class="panel-body">
<p>Write a program called <code>check.py</code> that takes the names of one or more inflammation data files as arguments and checks that all the files have the same number of rows and columns. What is the best way to test your program?</p>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># this is code/check.py</span>
<span class="im">import</span> sys
<span class="im">import</span> numpy
<span class="kw">def</span> main():
script <span class="op">=</span> sys.argv[<span class="dv">0</span>]
filenames <span class="op">=</span> sys.argv[<span class="dv">1</span>:]
<span class="cf">if</span> <span class="bu">len</span>(filenames) <span class="op"><=</span><span class="dv">1</span>: <span class="co">#nothing to check</span>
<span class="bu">print</span>(<span class="st">'Only 1 file specified on input'</span>)
<span class="cf">else</span>:
nrow0, ncol0 <span class="op">=</span> row_col_count(filenames[<span class="dv">0</span>])
<span class="bu">print</span>(<span class="st">'First file </span><span class="sc">%s</span><span class="st">: </span><span class="sc">%d</span><span class="st"> rows and </span><span class="sc">%d</span><span class="st"> columns'</span> <span class="op">%</span> (filenames[<span class="dv">0</span>], nrow0, ncol0))
<span class="cf">for</span> f <span class="op">in</span> filenames[<span class="dv">1</span>:]:
nrow, ncol <span class="op">=</span> row_col_count(f)
<span class="cf">if</span> nrow <span class="op">!=</span> nrow0 <span class="op">or</span> ncol <span class="op">!=</span> ncol0:
<span class="bu">print</span>(<span class="st">'File </span><span class="sc">%s</span><span class="st"> does not check: </span><span class="sc">%d</span><span class="st"> rows and </span><span class="sc">%d</span><span class="st"> columns'</span> <span class="op">%</span> (f, nrow, ncol))
<span class="cf">else</span>:
<span class="bu">print</span>(<span class="st">'File </span><span class="sc">%s</span><span class="st"> checks'</span> <span class="op">%</span> f)
<span class="cf">return</span>
<span class="kw">def</span> row_col_count(filename):
<span class="cf">try</span>:
nrow, ncol <span class="op">=</span> numpy.loadtxt(filename, delimiter<span class="op">=</span><span class="st">','</span>).shape
<span class="cf">except</span> <span class="pp">ValueError</span>: <span class="co">#get this if file doesn't have same number of rows and columns, or if it has non-numeric content</span>
nrow, ncol <span class="op">=</span> (<span class="dv">0</span>, <span class="dv">0</span>)
<span class="cf">return</span> nrow, ncol
main()</code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="counting-lines"><span class="glyphicon glyphicon-pencil"></span>Counting lines</h2>
</div>
<div class="panel-body">
<p>Write a program called <code>line_count.py</code> that works like the Unix <code>wc</code> command:</p>
<ul>
<li>If no filenames are given, it reports the number of lines in standard input.</li>
<li>If one or more filenames are given, it reports the number of lines in each, followed by the total number of lines.</li>
</ul>
<hr />
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># this is code/line_count.py</span>
<span class="im">import</span> sys
<span class="kw">def</span> main():
<span class="co">'''print each input filename and the number of lines in it,</span>
<span class="co"> and print the sum of the number of lines'''</span>
filenames <span class="op">=</span> sys.argv[<span class="dv">1</span>:]
sum_nlines <span class="op">=</span> <span class="dv">0</span> <span class="co">#initialize counting variable</span>
<span class="cf">if</span> <span class="bu">len</span>(filenames) <span class="op">==</span> <span class="dv">0</span>: <span class="co"># no filenames, just stdin</span>
sum_nlines <span class="op">=</span> count_file_like(sys.stdin)
<span class="bu">print</span>(<span class="st">'stdin: </span><span class="sc">%d</span><span class="st">'</span> <span class="op">%</span> sum_nlines)
<span class="cf">else</span>:
<span class="cf">for</span> f <span class="op">in</span> filenames:
n <span class="op">=</span> count_file(f)
<span class="bu">print</span>(<span class="st">'</span><span class="sc">%s</span><span class="st"> </span><span class="sc">%d</span><span class="st">'</span> <span class="op">%</span> (f, n))
sum_nlines <span class="op">+=</span> n
<span class="bu">print</span>(<span class="st">'total: </span><span class="sc">%d</span><span class="st">'</span> <span class="op">%</span> sum_nlines)
<span class="kw">def</span> count_file(filename):
<span class="co">'''count the number of lines in a file'''</span>
f <span class="op">=</span> <span class="bu">open</span>(filename,<span class="st">'r'</span>)
nlines <span class="op">=</span> <span class="bu">len</span>(f.readlines())
f.close()
<span class="cf">return</span>(nlines)
<span class="kw">def</span> count_file_like(file_like):
<span class="co">'''count the number of lines in a file-like object (eg stdin)'''</span>
n <span class="op">=</span> <span class="dv">0</span>
<span class="cf">for</span> line <span class="op">in</span> file_like:
n <span class="op">=</span> n<span class="dv">+1</span>
<span class="cf">return</span> n
main()</code></pre></div>
</div>
</section>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/python-novice-inflammation">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37305346-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>