forked from swcarpentry/python-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-loop.html
147 lines (146 loc) · 10.5 KB
/
02-loop.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
<!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">
<h1 class="title">Programming with Python</h1>
<h2 class="subtitle">Repeating Actions with Loops</h2>
<div id="learning-objectives" class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives" class="objectives panel panel-warning"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Explain what a for loop does.</li>
<li>Correctly write for loops to repeat simple calculations.</li>
<li>Trace changes to a loop variable as the loop runs.</li>
<li>Trace changes to other variables as they are updated by a for loop.</li>
</ul>
</div>
</div>
<p>In the last lesson, we wrote some code that plots some values of interest from our first inflammation dataset, and reveals some suspicious features in it, such as from <code>inflammation-01.csv</code></p>
<p><img src="fig/03-loop_2_0.png" alt="Analysis of inflammation-01.csv" /><br /> but we have a dozen data sets right now and more on the way. We want to create plots for all our data sets with a single statement. To do that, we’ll have to teach the computer how to repeat things.</p>
<p>Suppose we want to print each character in the word “lead” on a line of its own. One way is to use four <code>print</code> statements:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">word = <span class="st">'lead'</span>
<span class="dt">print</span> word[<span class="dv">0</span>]
<span class="dt">print</span> word[<span class="dv">1</span>]
<span class="dt">print</span> word[<span class="dv">2</span>]
<span class="dt">print</span> word[<span class="dv">3</span>]</code></pre></div>
<pre class="output"><code>l
e
a
d</code></pre>
<p>but that’s a bad approach for two reasons:</p>
<ol style="list-style-type: decimal">
<li><p>It doesn’t scale: if we want to print the characters in a string that’s hundreds of letters long, we’d be better off just typing them in.</p></li>
<li><p>It’s fragile: if we give it a longer string, it only prints part of the data, and if we give it a shorter one, it produces an error because we’re asking for characters that don’t exist.</p></li>
</ol>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">word = <span class="st">'tin'</span>
<span class="dt">print</span> word[<span class="dv">0</span>]
<span class="dt">print</span> word[<span class="dv">1</span>]
<span class="dt">print</span> word[<span class="dv">2</span>]
<span class="dt">print</span> word[<span class="dv">3</span>]</code></pre></div>
<pre class="output"><code>t
i
n</code></pre>
<pre class="error"><code>---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-3-7974b6cdaf14> in <module>()
3 print word[1]
4 print word[2]
----> 5 print word[3]
IndexError: string index out of range</code></pre>
<p>Here’s a better approach:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">word = <span class="st">'lead'</span>
<span class="kw">for</span> char in word:
<span class="dt">print</span> char</code></pre></div>
<p>This is shorter—certainly shorter than something that prints every character in a hundred-letter string—and more robust as well:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">word = <span class="st">'oxygen'</span>
<span class="kw">for</span> char in word:
<span class="dt">print</span> char</code></pre></div>
<p>This strategy relies on the use of a <a href="reference.html#for-loop">for loop</a> to repeat an operation—in this case, printing—once for each thing in a collection. The general form of a loop is:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">for</span> variable in collection:
do things <span class="kw">with</span> variable</code></pre></div>
<p>We can call the <a href="reference.html#loop-variable">loop variable</a> anything we like, but there must be a colon at the end of the line starting the loop, and we must indent the body of the loop. Unlike many other languages, there is no command to end a loop (e.g. end for); what is indented after the for statement belongs to the loop.</p>
<p>Here’s another loop that repeatedly updates a variable:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">length = <span class="dv">0</span>
<span class="kw">for</span> vowel in <span class="st">'aeiou'</span>:
length = length + <span class="dv">1</span>
<span class="dt">print</span> <span class="st">'There are'</span>, length, <span class="st">'vowels'</span></code></pre></div>
<p>It’s worth tracing the execution of this little program step by step. Since there are five characters in <code>'aeiou'</code>, the statement on line 3 will be executed five times. The first time around, <code>length</code> is zero (the value assigned to it on line 1) and <code>vowel</code> is <code>'a'</code>. The statement adds 1 to the old value of <code>length</code>, producing 1, and updates <code>length</code> to refer to that new value. The next time around, <code>vowel</code> is <code>'e'</code> and <code>length</code> is 1, so <code>length</code> is updated to be 2. After three more updates, <code>length</code> is 5; since there is nothing left in <code>'aeiou'</code> for Python to process, the loop finishes and the <code>print</code> statement on line 4 tells us our final answer.</p>
<p>Note that a loop variable is just a variable that’s being used to record progress in a loop. It still exists after the loop is over, and we can re-use variables previously defined as loop variables as well:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">letter = <span class="st">'z'</span>
<span class="kw">for</span> letter in <span class="st">'abc'</span>:
<span class="dt">print</span> letter
<span class="dt">print</span> <span class="st">'after the loop, letter is'</span>, letter</code></pre></div>
<p>Note also that finding the length of a string is such a common operation that Python actually has a built-in function to do it called <code>len</code>:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dt">print</span> <span class="dt">len</span>(<span class="st">'aeiou'</span>)</code></pre></div>
<p><code>len</code> is much faster than any function we could write ourselves, and much easier to read than a two-line loop; it will also give us the length of many other things that we haven’t met yet, so we should always use it when we can.</p>
<div id="from-1-to-n" class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="from-1-to-n" class="challenge panel panel-success"><span class="glyphicon glyphicon-pencil"></span>From 1 to N</h2>
</div>
<div class="panel-body">
<p>Python has a built-in function called <code>range</code> that creates a list of numbers: <code>range(3)</code> produces <code>[0, 1, 2]</code>, <code>range(2, 5)</code> produces <code>[2, 3, 4]</code>. Using <code>range</code>, write a loop that uses <code>range</code> to print the first 3 natural numbers:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">1</span>
<span class="dv">2</span>
<span class="dv">3</span></code></pre></div>
</div>
</div>
<div id="computing-powers-with-loops" class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="computing-powers-with-loops" class="challenge panel panel-success"><span class="glyphicon glyphicon-pencil"></span>Computing powers with loops</h2>
</div>
<div class="panel-body">
<p>Exponentiation is built into Python:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dt">print</span> <span class="dv">5</span>**<span class="dv">3</span>
<span class="dv">125</span></code></pre></div>
<p>It also has a function called <code>pow</code> that calculates the same value. Write a loop to calculate the same result.</p>
</div>
</div>
<div id="reverse-a-string" class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="reverse-a-string" class="challenge panel panel-success"><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, so <code>'Newton'</code> becomes <code>'notweN'</code>.</p>
</div>
</div>
</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>
</body>
</html>