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
<p>Each <codeclass="docutils literal notranslate"><spanclass="pre">if</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">elif</span></code>, and <codeclass="docutils literal notranslate"><spanclass="pre">else</span></code> statement must end in a colon
435
+
character, and the body of each of these statements is <aclass="reference external" href="https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Introduction.html#Python-Uses-Whitespace-to-Delimit-Scope">delimited by
436
+
whitespace</a>.</p>
434
437
<p>The following pseudo-code demonstrates the general template for
Copy file name to clipboardExpand all lines: docs/Module2_EssentialsOfPython/ForLoops.html
+3-2
Original file line number
Diff line number
Diff line change
@@ -260,8 +260,9 @@ <h2>For-Loops<a class="headerlink" href="#For-Loops" title="Permalink to this he
260
260
<p>Where <codeclass="docutils literal notranslate"><spanclass="pre"><var></span></code> is any valid variable-identifier and <codeclass="docutils literal notranslate"><spanclass="pre"><iterable></span></code> is
261
261
any <strong>iterable</strong>. We will discuss iterables more formally in the next
262
262
section; suffice it to know that every sequence-type object is an
263
-
iterable. The “body” of the for-loop is the code indented beneath the
264
-
for-loop statement.</p>
263
+
iterable. The <codeclass="docutils literal notranslate"><spanclass="pre">for</span></code>statement must end in a colon character, and the
<li><codeclass="docutils literal notranslate"><spanclass="pre"><function</span><spanclass="pre">name></span></code> can be any valid variable name, and <em>must</em> be
304
304
followed by parentheses and then a colon.</li>
305
305
<li><codeclass="docutils literal notranslate"><spanclass="pre"><function</span><spanclass="pre">signature></span></code> specifies the input arguments to the
306
-
function, and may be left blank (if the function does not accept any
307
-
arguments).</li>
306
+
function, and may be left blank if the function does not accept any
307
+
arguments (the parentheses must still be included, but will not
308
+
encapsulate anything).</li>
308
309
<li>The documentation string (commonly referred to as a “docstring”) may
309
310
span multiple lines, and should indicate what the function’s purpose
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">return</span></code> statement is also reserved by Python. It denotes the end
317
318
of a function; if reached, a <codeclass="docutils literal notranslate"><spanclass="pre">return</span></code> statement immediately concludes
318
319
the execution of the function and returns the specified object.</p>
319
-
<p>Note that, like an if-statement and a for-loop, the content of a
320
-
function is indicated by indentation:</p>
320
+
<p>Note that, like an if-statement and a for-loop, the <codeclass="docutils literal notranslate"><spanclass="pre">def</span></code> statment
321
+
must end in a colon and the body of the function is <aclass="reference external" href="https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Introduction.html#Python-Uses-Whitespace-to-Delimit-Scope">delimited by
<p><codeclass="docutils literal notranslate"><spanclass="pre"><expression></span></code> can be any valid single-line of Python code that
@@ -421,10 +421,9 @@ <h2>Creating your own generator: generator comprehensions<a class="headerlink" h
421
421
<divclass="admonition warning">
422
422
<pclass="first fa fa-exclamation-circle"><strong>Note</strong>:</p>
423
423
<pclass="last">Generator comprehensions are <strong>not</strong> the only method for defining
424
-
generators in Python. We will briefly introduced a more fully-fledged
425
-
syntax for defining generators. One can define a generator similar to
426
-
the way one can define a function (which we will encounter soon). <aclass="reference external" href="https://docs.python.org/3/tutorial/classes.html#generators">See
427
-
this section of the official Python
424
+
generators in Python. One can define a generator similar to the way one
425
+
can define a function (which we will encounter soon). <aclass="reference external" href="https://docs.python.org/3/tutorial/classes.html#generators">See this section
426
+
of the official Python
428
427
tutorial</a>
429
428
if you are interested in diving deeper into generators.</p>
<h3>Using generator comprehensions on the fly<aclass="headerlink" href="#Using-generator-comprehensions-on-the-fly" title="Permalink to this headline">¶</a></h3>
<spanclass="k">if</span><spanclass="n">num</span><spanclass="o">%</span><spanclass="mi">3</span><spanclass="o">==</span><spanclass="mi">0</span><spanclass="p">:</span><spanclass="c1"># x % y gives the remainder of x / y</span>
254
+
<spanclass="k">if</span><spanclass="n">num</span><spanclass="o">%</span><spanclass="mi">3</span><spanclass="o">==</span><spanclass="mi">0</span><spanclass="p">:</span><spanclass="c1"># recall: x % y gives the remainder of x / y</span>
<h2>Python Uses Whitespace to Delimit Scope<aclass="headerlink" href="#Python-Uses-Whitespace-to-Delimit-Scope" title="Permalink to this headline">¶</a></h2>
283
+
<p>While the concepts of function definitions, loops, and conditional
284
+
statements are shared across the majority of modern programming
285
+
languages, the languages often differ in their syntax for delimiting the
286
+
<em>bodies</em> of these constructs. For example, where C++ uses “curly braces”
287
+
as delimiters, e.g.:</p>
288
+
<divclass="highlight-cpp notranslate"><divclass="highlight"><pre><span></span><spanclass="c1">// example showing that C++ uses curly braces to delimit scope</span>
<p>Python <strong>uses whitespace (i.e. indentation) to delimit scope</strong>:</p>
301
+
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># example showing that Python uses whitespace to delimit scope</span>
<p>Look to the example at the beginning of this section to see the bodies
312
+
of the function definition, the for-loop, and the subsequent if-else
313
+
block were all separated by increasing levels of indentation.</p>
314
+
<p>Python’s syntax is quite flexible in terms of what it defines as a
315
+
whitespace delimiter. Its rules are that:</p>
316
+
<ulclass="simple">
317
+
<li>One or more whitespace characters (spaces or tabs) is sufficient to
318
+
serve as indentation.</li>
319
+
<li>A given indented block must use a uniform level of indentation. E.g.
320
+
if the first line of an indented block has two leading spaces, all
321
+
subsequent lines in that block must lead with exactly two white
322
+
spaces.</li>
323
+
</ul>
324
+
<p>While Python’s syntax is relatively forgiving, I am not: the <aclass="reference external" href="https://www.python.org/dev/peps/pep-0008/#indentation">standard
325
+
style</a> for
326
+
indenting in Python is to <strong>use four space characters</strong> for each level
327
+
of indentation. It is strongly advised that you adhere to this standard.
328
+
Most IDEs and consoles (including Jupyter notebooks) will automatically
329
+
add a four-space indentation for you when you enter into the body of one
330
+
of the aforementioned constructs.</p>
331
+
<p>Let’s review these ruled by considering a few simple examples (including
332
+
incorrect examples) of delimiting scope in Python.</p>
333
+
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># OK, but gross: The use of a single whitespace</span>
334
+
<spanclass="c1"># makes the indentation hard to see. Use four spaces.</span>
<spanclass="n">x</span><spanclass="o">=</span><spanclass="mi">1</span><spanclass="c1"># one space</span>
337
+
<spanclass="n">y</span><spanclass="o">=</span><spanclass="mi">2</span><spanclass="c1"># one space</span>
338
+
</pre></div>
339
+
</div>
340
+
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># BAD: the inconsistent level of indentation in this</span>
341
+
<spanclass="c1"># single block will cause this code to raise an IndentationError</span>
<spanclass="n">x</span><spanclass="o">=</span><spanclass="n">x</span><spanclass="o">+</span><spanclass="mi">1</span><spanclass="c1"># four spaces</span>
<spanclass="n">z</span><spanclass="o">=</span><spanclass="n">x</span><spanclass="o">+</span><spanclass="n">y</span><spanclass="c1"># four spaces</span>
346
+
<spanclass="k">return</span><spanclass="n">z</span><spanclass="c1"># four spaces</span>
347
+
</pre></div>
348
+
</div>
349
+
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># OK, but gross: The if-block uses four spaces as a delimiter.</span>
350
+
<spanclass="c1"># The else-block uses two spaces as a delimiter. This is technically</span>
351
+
<spanclass="c1"># okay since indentation is consistent within each block. One should</span>
352
+
<spanclass="c1"># always use four spaces for indentation.</span>
<spanclass="n">x</span><spanclass="o">=</span><spanclass="mi">3</span><spanclass="c1"># four spaces</span>
355
+
<spanclass="n">y</span><spanclass="o">=</span><spanclass="mi">2</span><spanclass="c1"># four spaces</span>
356
+
<spanclass="k">else</span><spanclass="p">:</span>
357
+
<spanclass="n">x</span><spanclass="o">=</span><spanclass="mi">2</span><spanclass="c1"># two spaces</span>
358
+
<spanclass="n">y</span><spanclass="o">=</span><spanclass="mi">1</span><spanclass="c1"># two spaces</span>
359
+
</pre></div>
360
+
</div>
361
+
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Good! The for-loop's body is defined by one level</span>
362
+
<spanclass="c1"># of four-space indentation, and the contained if-else</span>
363
+
<spanclass="c1"># blocks each have their own additional four-space indentations.</span>
<spanclass="k">if</span><spanclass="n">i</span><spanclass="o">==</span><spanclass="mi">2</span><spanclass="ow">or</span><spanclass="n">i</span><spanclass="o">==</span><spanclass="mi">4</span><spanclass="p">:</span><spanclass="c1"># four spaces</span>
0 commit comments