You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/Module2_EssentialsOfPython/ConditionalStatements.html
+40-23
Original file line number
Diff line number
Diff line change
@@ -234,16 +234,26 @@ <h1>Conditional Statements<a class="headerlink" href="#Conditional-Statements" t
234
234
<p>In this section, we will be introduced to the <codeclass="docutils literal"><spanclass="pre">if</span></code>, <codeclass="docutils literal"><spanclass="pre">else</span></code>, and
235
235
<codeclass="docutils literal"><spanclass="pre">elif</span></code> statements. These allow you to specify that blocks of code are
236
236
to be executed only if specified conditions are found to be true, or
237
-
perhaps alternative code if the condition is found to be false.</p>
237
+
perhaps alternative code if the condition is found to be false. For
238
+
example, the following code will square <codeclass="docutils literal"><spanclass="pre">x</span></code> if it is a negative
239
+
number, and will cube <codeclass="docutils literal"><spanclass="pre">x</span></code> if it is a positive number:</p>
240
+
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># a simple if-else block</span>
<h2>Comparison Operations<aclass="headerlink" href="#Comparison-Operations" title="Permalink to this headline">¶</a></h2>
245
-
<p>Comparison statements will evaluate explicitly to either <codeclass="docutils literal"><spanclass="pre">True</span></code> or
246
-
<codeclass="docutils literal"><spanclass="pre">False</span></code>. There are eight comparison operations in Python:</p>
254
+
<p>Comparison statements will evaluate explicitly to either of the
255
+
boolean-objects: <codeclass="docutils literal"><spanclass="pre">True</span></code> or <codeclass="docutils literal"><spanclass="pre">False</span></code>. There are eight comparison
256
+
operations in Python:</p>
247
257
<tableborder="1" class="docutils">
248
258
<colgroup>
249
259
<colwidth="32%" />
@@ -349,8 +359,9 @@ <h2><code class="docutils literal"><span class="pre">bool</span></code> and Trut
349
359
</pre></div>
350
360
</div>
351
361
<p>and non-zero Python integers are associated with <codeclass="docutils literal"><spanclass="pre">True</span></code>:</p>
<p>What will happen if <codeclass="docutils literal"><spanclass="pre">my_list</span></code> is <codeclass="docutils literal"><spanclass="pre">[]</span></code>? Will <codeclass="docutils literal"><spanclass="pre">IndexError</span></code> be
500
511
raised? What will <codeclass="docutils literal"><spanclass="pre">first_item</span></code> be?</p>
501
-
<p>2. The variable <codeclass="docutils literal"><spanclass="pre">my_file</span></code> is a string like <codeclass="docutils literal"><spanclass="pre">"code.py"</span></code>,
502
-
<codeclass="docutils literal"><spanclass="pre">"my_doc.txt"</span></code>, etc, where a period denotes the end of the file-name
503
-
and the beginning of the file-type. <codeclass="docutils literal"><spanclass="pre">my_file</span></code> will have at most one
504
-
period in it. Write code that creates a variable which stores only the
505
-
filename. Accommodate cases where <codeclass="docutils literal"><spanclass="pre">my_file</span></code> does <em>not</em> include a
506
-
file-type.</p>
507
-
<p>That is, <codeclass="docutils literal"><spanclass="pre">"code.py"</span></code><spanclass="math">\(\rightarrow\)</span><codeclass="docutils literal"><spanclass="pre">"code"</span></code>, <codeclass="docutils literal"><spanclass="pre">"doc2.pdf"</span></code>
<spanclass="k">print</span><spanclass="p">(</span><spanclass="s2">"x + y = "</span><spanclass="p">,</span><spanclass="n">x</span><spanclass="o">+</span><spanclass="n">y</span><spanclass="p">)</span>
226
229
<spanclass="k">print</span><spanclass="p">(</span><spanclass="s2">"x - y = "</span><spanclass="p">,</span><spanclass="n">x</span><spanclass="o">-</span><spanclass="n">y</span><spanclass="p">)</span>
<h2>Reading Comprehension Exercise Solutions:<aclass="headerlink" href="#Reading-Comprehension-Exercise-Solutions:" title="Permalink to this headline">¶</a></h2>
0 commit comments