forked from swcarpentry/python-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-cond.html
190 lines (189 loc) · 16.2 KB
/
05-cond.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
<!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">Making Choices</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 the similarities and differences between tuples and lists.</li>
<li>Write conditional statements including <code>if</code>, <code>elif</code>, and <code>else</code> branches.</li>
<li>Correctly evaluate expressions containing <code>and</code> and <code>or</code>.</li>
</ul>
</div>
</section>
<p>In our last lesson, we discovered something suspicious was going on in our inflammation data by drawing some plots. How can we use Python to automatically recognize the different features we saw, and take a different action for each? In this lesson, we’ll learn how to write code that runs only when certain conditions are true.</p>
<h2 id="conditionals">Conditionals</h2>
<p>We can ask Python to take different actions, depending on a condition, with an <code>if</code> statement:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">num <span class="op">=</span> <span class="dv">37</span>
<span class="cf">if</span> num <span class="op">></span> <span class="dv">100</span>:
<span class="bu">print</span>(<span class="st">'greater'</span>)
<span class="cf">else</span>:
<span class="bu">print</span>(<span class="st">'not greater'</span>)
<span class="bu">print</span>(<span class="st">'done'</span>)</code></pre></div>
<pre class="output"><code>not greater
done
</code></pre>
<p>The second line of this code uses the keyword <code>if</code> to tell Python that we want to make a choice. If the test that follows the <code>if</code> statement is true, the body of the <code>if</code> (i.e., the lines indented underneath it) are executed. If the test is false, the body of the <code>else</code> is executed instead. Only one or the other is ever executed:</p>
<p><img src="fig/python-flowchart-conditional.svg" alt="Executing a Conditional" /><br />
Conditional statements don’t have to include an <code>else</code>. If there isn’t one, Python simply does nothing if the test is false:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">num <span class="op">=</span> <span class="dv">53</span>
<span class="bu">print</span>(<span class="st">'before conditional...'</span>)
<span class="cf">if</span> num <span class="op">></span> <span class="dv">100</span>:
<span class="bu">print</span>(<span class="st">'53 is greater than 100'</span>)
<span class="bu">print</span>(<span class="st">'...after conditional'</span>)</code></pre></div>
<pre class="output"><code>before conditional...
...after conditional</code></pre>
<p>We can also chain several tests together using <code>elif</code>, which is short for “else if”. The following Python code uses <code>elif</code> to print the sign of a number.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">num <span class="op">=</span> <span class="op">-</span><span class="dv">3</span>
<span class="cf">if</span> num <span class="op">></span> <span class="dv">0</span>:
<span class="bu">print</span>(num, <span class="st">"is positive"</span>)
<span class="cf">elif</span> num <span class="op">==</span> <span class="dv">0</span>:
<span class="bu">print</span>(num, <span class="st">"is zero"</span>)
<span class="cf">else</span>:
<span class="bu">print</span>(num, <span class="st">"is negative"</span>)</code></pre></div>
<pre class="output"><code>"-3 is negative"</code></pre>
<p>One important thing to notice in the code above is that we use a double equals sign <code>==</code> to test for equality rather than a single equals sign because the latter is used to mean assignment.</p>
<p>We can also combine tests using <code>and</code> and <code>or</code>. <code>and</code> is only true if both parts are true:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">if</span> (<span class="dv">1</span> <span class="op">></span> <span class="dv">0</span>) <span class="op">and</span> (<span class="op">-</span><span class="dv">1</span> <span class="op">></span> <span class="dv">0</span>):
<span class="bu">print</span>(<span class="st">'both parts are true'</span>)
<span class="cf">else</span>:
<span class="bu">print</span>(<span class="st">'at least one part is false'</span>)</code></pre></div>
<pre class="output"><code>at least one part is false</code></pre>
<p>while <code>or</code> is true if at least one part is true:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">if</span> (<span class="dv">1</span> <span class="op"><</span> <span class="dv">0</span>) <span class="op">or</span> (<span class="op">-</span><span class="dv">1</span> <span class="op"><</span> <span class="dv">0</span>):
<span class="bu">print</span>(<span class="st">'at least one test is true'</span>)</code></pre></div>
<pre class="output"><code>at least one test is true</code></pre>
<h2 id="checking-our-data">Checking our Data</h2>
<p>Now that we’ve seen how conditionals work, we can use them to check for the suspicious features we saw in our inflammation data. In the first couple of plots, the maximum inflammation per day seemed to rise like a straight line, one unit per day. We can check for this inside the <code>for</code> loop we wrote with the following conditional:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">if</span> data.<span class="bu">max</span>(axis<span class="op">=</span><span class="dv">0</span>)[<span class="dv">0</span>] <span class="op">==</span> <span class="dv">0</span> <span class="op">and</span> data.<span class="bu">max</span>(axis<span class="op">=</span><span class="dv">0</span>)[<span class="dv">20</span>] <span class="op">==</span> <span class="dv">20</span>:
<span class="bu">print</span>(<span class="st">'Suspicious looking maxima!'</span>)</code></pre></div>
<p>We also saw a different problem in the third dataset; the minima per day were all zero (looks like a healthy person snuck into our study). We can also check for this with an <code>elif</code> condition:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">elif</span> data.<span class="bu">min</span>(axis<span class="op">=</span><span class="dv">0</span>).<span class="bu">sum</span>() <span class="op">==</span> <span class="dv">0</span>:
<span class="bu">print</span>(<span class="st">'Minima add up to zero!'</span>)</code></pre></div>
<p>And if neither of these conditions are true, we can use <code>else</code> to give the all-clear:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">else</span>:
<span class="bu">print</span>(<span class="st">'Seems OK!'</span>)</code></pre></div>
<p>Let’s test that out:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">data <span class="op">=</span> numpy.loadtxt(fname<span class="op">=</span><span class="st">'inflammation-01.csv'</span>, delimiter<span class="op">=</span><span class="st">','</span>)
<span class="cf">if</span> data.<span class="bu">max</span>(axis<span class="op">=</span><span class="dv">0</span>)[<span class="dv">0</span>] <span class="op">==</span> <span class="dv">0</span> <span class="op">and</span> data.<span class="bu">max</span>(axis<span class="op">=</span><span class="dv">0</span>)[<span class="dv">20</span>] <span class="op">==</span> <span class="dv">20</span>:
<span class="bu">print</span>(<span class="st">'Suspicious looking maxima!'</span>)
<span class="cf">elif</span> data.<span class="bu">min</span>(axis<span class="op">=</span><span class="dv">0</span>).<span class="bu">sum</span>() <span class="op">==</span> <span class="dv">0</span>:
<span class="bu">print</span>(<span class="st">'Minima add up to zero!'</span>)
<span class="cf">else</span>:
<span class="bu">print</span>(<span class="st">'Seems OK!'</span>)</code></pre></div>
<pre class="output"><code>Suspicious looking maxima!</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">data <span class="op">=</span> numpy.loadtxt(fname<span class="op">=</span><span class="st">'inflammation-03.csv'</span>, delimiter<span class="op">=</span><span class="st">','</span>)
<span class="cf">if</span> data.<span class="bu">max</span>(axis<span class="op">=</span><span class="dv">0</span>)[<span class="dv">0</span>] <span class="op">==</span> <span class="dv">0</span> <span class="op">and</span> data.<span class="bu">max</span>(axis<span class="op">=</span><span class="dv">0</span>)[<span class="dv">20</span>] <span class="op">==</span> <span class="dv">20</span>:
<span class="bu">print</span>(<span class="st">'Suspicious looking maxima!'</span>)
<span class="cf">elif</span> data.<span class="bu">min</span>(axis<span class="op">=</span><span class="dv">0</span>).<span class="bu">sum</span>() <span class="op">==</span> <span class="dv">0</span>:
<span class="bu">print</span>(<span class="st">'Minima add up to zero!'</span>)
<span class="cf">else</span>:
<span class="bu">print</span>(<span class="st">'Seems OK!'</span>)</code></pre></div>
<pre class="output"><code>Minima add up to zero!</code></pre>
<p>In this way, we have asked Python to do something different depending on the condition of our data. Here we printed messages in all cases, but we could also imagine not using the <code>else</code> catch-all so that messages are only printed when something is wrong, freeing us from having to manually examine every plot for features we’ve seen before.</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>
<ol style="list-style-type: decimal">
<li>A</li>
<li>B</li>
<li>C</li>
<li>B and C</li>
</ol>
<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>
</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><code>True</code> and <code>False</code> are special words in Python called <code>booleans</code> which represent true and false statements. However, they aren’t the only values in Python that are true and false. In fact, <em>any</em> value can be used in an <code>if</code> or <code>elif</code>. After reading and running the code below, explain what the rule is 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>
</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. Compare your implementation with your partner’s: do you get the same answer for all possible pairs of numbers?</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>Python (and most other languages in the C family) provides <a href="reference.html#in-place-operator">in-place operators</a> that work like this:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">x <span class="op">=</span> <span class="dv">1</span> <span class="co"># original value</span>
x <span class="op">+=</span> <span class="dv">1</span> <span class="co"># add one to x, assigning result back to x</span>
x <span class="op">*=</span> <span class="dv">3</span> <span class="co"># multiply x by 3</span>
<span class="bu">print</span>(x)</code></pre></div>
<pre class="output"><code>6</code></pre>
<p>Write some code that sums the positive and negative numbers in a list separately, using in-place operators. Do you think the result is more or less readable than writing the same without in-place operators?</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:admin@software-carpentry.org">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>