/
Conditional_Statements.html
158 lines (143 loc) · 12.3 KB
/
Conditional_Statements.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
<div id="ipython-notebook">
<a class="interact-button" href="http://datahub.berkeley.edu/user-redirect/interact?repo=textbook&path=notebooks/Conditional_Statements.ipynb">Interact</a>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$']],
processEscapes: true
}
});
</script>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Conditional-Statements">Conditional Statements<a class="anchor-link" href="#Conditional-Statements">¶</a></h3><p>In many situations, actions and results depends on a specific set of conditions being satisfied. For example, individuals in randomized controlled trials receive the treatment if they have been assigned to the treatment group. A gambler makes money if she wins her bet.</p>
<p>In this section we will learn how to describe such situations using code. A <em>conditional statement</em> is a multi-line statement that allows Python to choose among different alternatives based on the truth value of an expression. While conditional statements can appear anywhere, they appear most often within the body of a function in order to express alternative behavior depending on argument values.</p>
<p>A conditional statement always begins with an <code>if</code> header, which is a single line followed by an indented body. The body is only executed if the expression directly following <code>if</code> (called the <em>if expression</em>) evaluates to a true value. If the <em>if expression</em> evaluates to a false value, then the body of the <code>if</code> is skipped.</p>
<p>Let us start defining a function that returns the sign of a number.</p></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">if</span> <span class="n">x</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Positive'</span>
</pre></div></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">sign</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
</pre></div></div></div>
<div class="output_text output_subarea output_execute_result">
<pre>'Positive'</pre></div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This function returns the correct sign if the input is a positive number. But if the input is not a positive number, then the <em>if expression</em> evaluates to a false value, and so the <code>return</code> statement is skipped and the function call has no value.</p></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">sign</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
</pre></div></div></div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>So let us refine our function to return <code>Negative</code> if the input is a negative number. We can do this by adding an <code>elif</code> clause, where <code>elif</code> if Python's shorthand for the phrase "else, if".</p></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">if</span> <span class="n">x</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Positive'</span>
<span class="k">elif</span> <span class="n">x</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Negative'</span>
</pre></div></div></div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Now <code>sign</code> returns the correct answer when the input is -3:</p></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">sign</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
</pre></div></div></div>
<div class="output_text output_subarea output_execute_result">
<pre>'Negative'</pre></div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>What if the input is 0? To deal with this case, we can add another <code>elif</code> clause:</p></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">if</span> <span class="n">x</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Positive'</span>
<span class="k">elif</span> <span class="n">x</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Negative'</span>
<span class="k">elif</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Neither positive nor negative'</span>
</pre></div></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">sign</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</pre></div></div></div>
<div class="output_text output_subarea output_execute_result">
<pre>'Neither positive nor negative'</pre></div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Equivalently, we can replaced the final <code>elif</code> clause by an <code>else</code> clause, whose body will be executed only if all the previous comparisons are false; that is, if the input value is equal to 0.</p></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">if</span> <span class="n">x</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Positive'</span>
<span class="k">elif</span> <span class="n">x</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Negative'</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'Neither positive nor negative'</span>
</pre></div></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">sign</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</pre></div></div></div>
<div class="output_text output_subarea output_execute_result">
<pre>'Neither positive nor negative'</pre></div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-General-Form">The General Form<a class="anchor-link" href="#The-General-Form">¶</a></h3><p>A conditional statement can also have multiple clauses with multiple bodies, and only one of those bodies can ever be executed. The general format of a multi-clause conditional statement appears below.</p>
<pre><code>if <if expression>:
<if body>
elif <elif expression 0>:
<elif body 0>
elif <elif expression 1>:
<elif body 1>
...
else:
<else body>
</code></pre>
<p>There is always exactly one <code>if</code> clause, but there can be any number of <code>elif</code> clauses. Python will evaluate the <code>if</code> and <code>elif</code> expressions in the headers in order until one is found that is a true value, then execute the corresponding body. The <code>else</code> clause is optional. When an <code>else</code> header is provided, its <em>else body</em> is executed only if none of the header expressions of the previous clauses are true. The <code>else</code> clause must always come at the end (or not at all).</p></div></div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id='Example:-"The-Other-One"'>Example: "The Other One"<a class="anchor-link" href='#Example:-"The-Other-One"'>¶</a></h3><p>We will now use conditional statements to define a function that seems rather artificial and contrary, but will come in handy later in the chapter. It takes an array with two elements (for example, <code>red</code> and <code>blue</code>), and another element to compare. If that element is <code>red</code>, the function returns <code>blue</code>. If the element is (for example) <code>blue</code>, the function returns <code>red</code>. That is why we'll call the function <code>other_one</code>.</p></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">other_one</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">a_b</span><span class="p">):</span>
<span class="sd">"""Compare x with the two elements of a_b;</span>
<span class="sd"> if it is equal to one of them, return the other one;</span>
<span class="sd"> if it is not equal to either of them, return an error message.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="n">a_b</span><span class="o">.</span><span class="n">item</span><span class="p">(</span><span class="mi">0</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a_b</span><span class="o">.</span><span class="n">item</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">x</span> <span class="o">==</span> <span class="n">a_b</span><span class="o">.</span><span class="n">item</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a_b</span><span class="o">.</span><span class="n">item</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">'The input is not valid.'</span>
</pre></div></div></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">colors</span> <span class="o">=</span> <span class="n">make_array</span><span class="p">(</span><span class="s1">'red'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">)</span>
<span class="n">other_one</span><span class="p">(</span><span class="s1">'red'</span><span class="p">,</span> <span class="n">colors</span><span class="p">)</span>
</pre></div></div></div>
<div class="output_text output_subarea output_execute_result">
<pre>'blue'</pre></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">other_one</span><span class="p">(</span><span class="s1">'blue'</span><span class="p">,</span> <span class="n">colors</span><span class="p">)</span>
</pre></div></div></div>
<div class="output_text output_subarea output_execute_result">
<pre>'red'</pre></div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">other_one</span><span class="p">(</span><span class="s1">'potato'</span><span class="p">,</span> <span class="n">colors</span><span class="p">)</span>
</pre></div></div></div>
<div class="output_text output_subarea output_execute_result">
<pre>'The input is not valid.'</pre></div></div>