forked from swcarpentry/python-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
02-loop.html
178 lines (175 loc) · 12.6 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
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
<!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">Repeating Actions with Loops</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives"><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>
</section>
<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 />
We have a dozen data sets right now, though, and more on the way. We want to create plots for all of our data sets with a single statement. To do that, we’ll have to teach the computer how to repeat things.</p>
<p>An example task that we might want to repeat is printing each character in a word on a line of its own.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">word <span class="op">=</span> <span class="st">'lead'</span></code></pre></div>
<p>We can access a character in a string using its index. For example, we can get the first character of the word ‘lead’, by using word[0]. One way to print each character is to use four <code>print</code> statements:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(word[<span class="dv">0</span>])
<span class="bu">print</span>(word[<span class="dv">1</span>])
<span class="bu">print</span>(word[<span class="dv">2</span>])
<span class="bu">print</span>(word[<span class="dv">3</span>])</code></pre></div>
<pre class="output"><code>l
e
a
d</code></pre>
<p>This is 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="op">=</span> <span class="st">'tin'</span>
<span class="bu">print</span>(word[<span class="dv">0</span>])
<span class="bu">print</span>(word[<span class="dv">1</span>])
<span class="bu">print</span>(word[<span class="dv">2</span>])
<span class="bu">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="op">=</span> <span class="st">'lead'</span>
<span class="cf">for</span> char <span class="op">in</span> word:
<span class="bu">print</span>(char)</code></pre></div>
<pre class="output"><code>l
e
a
d</code></pre>
<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="op">=</span> <span class="st">'oxygen'</span>
<span class="cf">for</span> char <span class="op">in</span> word:
<span class="bu">print</span>(char)</code></pre></div>
<pre class="output"><code>o
x
y
g
e
n</code></pre>
<p>The improved version uses 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="cf">for</span> variable <span class="op">in</span> collection:
do things <span class="cf">with</span> variable</code></pre></div>
<p>Using the oxygen example above, the loop might look like this: <img src="./fig/loops_image.png" alt="loop_image" /></p>
<p>Where each character (<code>char</code>) in the variable <code>word</code> is looped through and printed one character after another. The numbers in the diagram denote which loop cycle the character was printed in (1 being the first loop, and 6 being the final loop).</p>
<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 anything we want to run inside the loop. Unlike many other languages, there is no command to signify the end of the loop body (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="op">=</span> <span class="dv">0</span>
<span class="cf">for</span> vowel <span class="op">in</span> <span class="st">'aeiou'</span>:
length <span class="op">=</span> length <span class="op">+</span> <span class="dv">1</span>
<span class="bu">print</span>(<span class="st">'There are'</span>, length, <span class="st">'vowels'</span>)</code></pre></div>
<pre class="output"><code>There are 5 vowels</code></pre>
<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="op">=</span> <span class="st">'z'</span>
<span class="cf">for</span> letter <span class="op">in</span> <span class="st">'abc'</span>:
<span class="bu">print</span>(letter)
<span class="bu">print</span>(<span class="st">'after the loop, letter is'</span>, letter)</code></pre></div>
<pre class="output"><code>a
b
c
after the loop, letter is c</code></pre>
<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="bu">print</span>(<span class="bu">len</span>(<span class="st">'aeiou'</span>))</code></pre></div>
<pre class="output"><code>5</code></pre>
<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>
<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>Python has a built-in function called <code>range</code> that creates a sequence of numbers. Range can accept 1-3 parameters. If one parameter is input, range creates an array of that length, starting at zero and incrementing by 1. If 2 parameters are input, range starts at the first and ends just before the second, incrementing by one. If range is passed 3 parameters, it starts at the first one, ends just before the second one, and increments by the third one. For example, <code>range(3)</code> produces the numbers 0, 1, 2, while <code>range(2, 5)</code> produces 2, 3, 4, and <code>range(3, 10, 3)</code> produces 3, 6, 9. 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>
</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>Exponentiation is built into Python:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="dv">5</span> <span class="op">**</span> <span class="dv">3</span>)</code></pre></div>
<pre class="output"><code>125</code></pre>
<p>Write a loop that calculates the same result as <code>5 ** 3</code> using multiplication (and without exponentiation).</p>
</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, so <code>'Newton'</code> becomes <code>'notweN'</code>.</p>
</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>