Skip to content

Commit 0e91052

Browse files
committed
minor revisions
1 parent 95cd4b0 commit 0e91052

File tree

121 files changed

+5995
-1589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+5995
-1589
lines changed
Binary file not shown.

docs/.doctrees/environment.pickle

1.09 KB
Binary file not shown.

docs/.doctrees/index.doctree

3.28 KB
Binary file not shown.

docs/Module2_EssentialsOfPython/Basic_Objects.html

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<li class="toctree-l4"><a class="reference internal" href="#Floating-Point-Numbers">Floating-Point Numbers</a></li>
106106
<li class="toctree-l4"><a class="reference internal" href="#Complex-Numbers">Complex Numbers</a></li>
107107
<li class="toctree-l4"><a class="reference internal" href="#Augmented-Assignment-Statements">Augmented Assignment Statements</a></li>
108+
<li class="toctree-l4"><a class="reference internal" href="#Improving-The-Readability-of-Numbers">Improving The Readability of Numbers</a></li>
108109
</ul>
109110
</li>
110111
<li class="toctree-l3"><a class="reference internal" href="#The-Boolean-Type">The Boolean Type</a><ul>
@@ -557,8 +558,8 @@ <h4>Understanding Numerical Precision<a class="headerlink" href="#Understanding-
557558
number is written in scientific notation. The computer will not be able
558559
to reliably represent a number’s digits beyond those accounted for by
559560
the allotted 8 bytes. For instance, the following Python integer is
560-
defined with 23 digits, but when this number is converted to a float, it
561-
only retains 16 decimal places in scientific notation:</p>
561+
defined with 100 digits, but when this number is converted to a float,
562+
it only retains 15 decimal places in scientific notation:</p>
562563
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Demonstrating the finite-precision of a float.</span>
563564

564565
<span class="c1"># An integer with 100 digits - Python will use as</span>
@@ -574,10 +575,15 @@ <h4>Understanding Numerical Precision<a class="headerlink" href="#Understanding-
574575
<span class="mf">1.111111111111111e+99</span>
575576
</pre></div>
576577
</div>
577-
<p>The computer cannot keep track of those last 6 decimal places, because
578+
<p>The computer cannot keep track of those last 84 decimal places because
578579
doing so would require more than 8 bytes of memory to store the entire
579-
value of that float. As such, attempting to modify a number in decimal
580-
places beyond its numerical precision does not have any effect:</p>
580+
value of that float. If you had been diligently counting stars in the
581+
sky (perhaps across many universes, this number far exceeds the
582+
estimated number of stars in our universe), you would have just lost
583+
track of over <span class="math">\(1\times10^{83}\)</span> of them simply by converting your
584+
integer count to a float!</p>
585+
<p>As such, attempting to modify a floating point number in decimal places
586+
beyond its numerical precision does not have any effect:</p>
581587
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># changing a float beyond its precision has no effect</span>
582588
<span class="o">&gt;&gt;&gt;</span> <span class="mf">1.</span> <span class="o">+</span> <span class="mf">1e-16</span>
583589
<span class="mf">1.0</span>
@@ -662,13 +668,29 @@ <h3>Complex Numbers<a class="headerlink" href="#Complex-Numbers" title="Permalin
662668
<span class="o">&gt;&gt;&gt;</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
663669
<span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="mi">3j</span><span class="p">)</span>
664670

671+
<span class="o">&gt;&gt;&gt;</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
672+
<span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">0j</span><span class="p">)</span>
673+
665674
<span class="o">&gt;&gt;&gt;</span> <span class="nb">type</span><span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="mi">3j</span><span class="p">)</span>
666675
<span class="nb">complex</span>
667676

668677
<span class="o">&gt;&gt;&gt;</span> <span class="nb">isinstance</span><span class="p">(</span><span class="mi">2</span><span class="o">-</span><span class="mi">4j</span><span class="p">,</span> <span class="nb">complex</span><span class="p">)</span>
669678
<span class="bp">True</span>
670679
</pre></div>
671680
</div>
681+
<p>Note that <code class="docutils literal"><span class="pre">j</span></code> is not, by itself, reserved as a special placeholder for
682+
<span class="math">\(i\)</span>. Rather, <code class="docutils literal"><span class="pre">j</span></code> must be preceded immediately with a numerical
683+
literal (i.e.&nbsp;you cannot use a variable) in order for the Python
684+
interpreter to treat it as a complex number.</p>
685+
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># `j` by itself is treated like any other character</span>
686+
<span class="o">&gt;&gt;&gt;</span> <span class="n">j</span>
687+
<span class="ne">NameError</span><span class="p">:</span> <span class="n">name</span> <span class="s1">&#39;j&#39;</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">defined</span>
688+
689+
<span class="c1"># `1j` is interpreted as the imaginary number</span>
690+
<span class="o">&gt;&gt;&gt;</span> <span class="p">(</span><span class="mi">1j</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
691+
<span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">0j</span><span class="p">)</span>
692+
</pre></div>
693+
</div>
672694
<p>You can access <code class="docutils literal"><span class="pre">a</span></code> and <code class="docutils literal"><span class="pre">b</span></code> from <code class="docutils literal"><span class="pre">a</span> <span class="pre">+</span> <span class="pre">bj</span></code>, the real and imaginary
673695
parts of the complex number, respectively.</p>
674696
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Accessing the real and imaginary parts of</span>
@@ -700,8 +722,11 @@ <h3>Complex Numbers<a class="headerlink" href="#Complex-Numbers" title="Permalin
700722
<p>2. What type of number will be returned if you perform a mathematical
701723
operation using an integer and a floating-point number? Does this hold
702724
for all the arithmetic operations? Determine this by trial and error.</p>
703-
<p class="last">3. Given the function <span class="math">\(f(x) = e^{|x - 2|}\)</span>, make use of the
725+
<p>3. Given the function <span class="math">\(f(x) = e^{|x - 2|}\)</span>, make use of the
704726
<code class="docutils literal"><span class="pre">math</span></code> module to compute <span class="math">\(f(-0.2)\)</span>.</p>
727+
<p class="last">4. Using Python’s syntax for scientific notation, write an expression
728+
that verifies that one trillion divided by one billion is equal to one
729+
thousand</p>
705730
</div>
706731
</div>
707732
<div class="section" id="Augmented-Assignment-Statements">
@@ -741,6 +766,35 @@ <h3>Augmented Assignment Statements<a class="headerlink" href="#Augmented-Assign
741766
<li><code class="docutils literal"><span class="pre">x</span> <span class="pre">**=</span> <span class="pre">n</span></code> <span class="math">\(\rightarrow\)</span> <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">**</span> <span class="pre">n</span></code></li>
742767
</ul>
743768
</div>
769+
<div class="section" id="Improving-The-Readability-of-Numbers">
770+
<h3>Improving The Readability of Numbers<a class="headerlink" href="#Improving-The-Readability-of-Numbers" title="Permalink to this headline"></a></h3>
771+
<p>Python version 3.6 introduced the ability to include underscores between
772+
the digits of a number as a visual delimiter. This character can be used
773+
to improve the readability of long numbers in your code. For example the
774+
number <code class="docutils literal"><span class="pre">662607004</span></code> can be rewritten as <code class="docutils literal"><span class="pre">662_607_004</span></code>, using <code class="docutils literal"><span class="pre">_</span></code> to
775+
delimit digits separated by orders of one thousand. Leading, trailing,
776+
or multiple underscores in a row are not allowed; otherwise this
777+
character can be included anywhere within a numerical literal.</p>
778+
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># examples of using `_` as a visual delimiter in numbers</span>
779+
<span class="o">&gt;&gt;&gt;</span> <span class="mi">1</span><span class="n">_000_000</span> <span class="c1"># this is nice!</span>
780+
<span class="mi">1000000</span>
781+
782+
<span class="c1"># this is gross but is permitted</span>
783+
<span class="o">&gt;&gt;&gt;</span> <span class="mi">2</span><span class="n">_3_4</span><span class="o">.</span><span class="mi">5</span><span class="n">_6_7</span>
784+
<span class="mf">234.567</span>
785+
786+
<span class="c1"># underscores work with all variety of numerical literals</span>
787+
<span class="o">&gt;&gt;&gt;</span> <span class="mi">10</span><span class="n">_000j</span>
788+
<span class="mi">10000j</span>
789+
</pre></div>
790+
</div>
791+
<div class="admonition warning">
792+
<p class="first fa fa-exclamation-circle"><strong>Compatibility Warning</strong></p>
793+
<p class="last">The permitted use of the underscore character, <code class="docutils literal"><span class="pre">_</span></code>, in numerical
794+
literals was introduced in Python 3.6. Thus utilizing this syntax in
795+
your code will render it incompatible with Python 3.5 and earlier.</p>
796+
</div>
797+
</div>
744798
</div>
745799
<div class="section" id="The-Boolean-Type">
746800
<h2>The Boolean Type<a class="headerlink" href="#The-Boolean-Type" title="Permalink to this headline"></a></h2>
@@ -1366,12 +1420,25 @@ <h2>Reading Comprehension Exercise Solutions:<a class="headerlink" href="#Readin
13661420
<span class="go">1.0</span>
13671421
</pre></div>
13681422
</div>
1369-
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># 3. Given the function f(x)=e^|x−2|f(x), make use</span>
1370-
<span class="c1"># of the math module to compute f(−0.2).</span>
1371-
<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">exp</span>
1372-
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="o">-</span><span class="mf">0.2</span>
1373-
<span class="o">&gt;&gt;&gt;</span> <span class="n">exp</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">))</span>
1374-
<span class="mf">9.025013499434122</span>
1423+
<p>3. Given the function <span class="math">\(f(x) = e^{|x - 2|}\)</span>, make use of the
1424+
<code class="docutils literal"><span class="pre">math</span></code> module to compute <span class="math">\(f(-0.2)\)</span>.</p>
1425+
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">exp</span>
1426+
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="o">-</span><span class="mf">0.2</span>
1427+
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">))</span>
1428+
<span class="go">9.025013499434122</span>
1429+
</pre></div>
1430+
</div>
1431+
<p>4. Using Python’s syntax for scientific notation, write an expression
1432+
that verifies that one trillion divided by one billion is equal to one
1433+
thousand.</p>
1434+
<blockquote>
1435+
<div>As cautioned above, we should avoid checking to see if two floating
1436+
point numbers are exactly equal, and instead simply ensure that they
1437+
are close in value. Keep in mind that a number written Python’s
1438+
using scientific syntax will produce a float.</div></blockquote>
1439+
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">isclose</span>
1440+
<span class="gp">&gt;&gt;&gt; </span><span class="n">isclose</span><span class="p">(</span><span class="mf">1e12</span> <span class="o">/</span> <span class="mf">1e9</span><span class="p">,</span> <span class="mf">1e3</span><span class="p">)</span>
1441+
<span class="go">True</span>
13751442
</pre></div>
13761443
</div>
13771444
<p><strong>Boolean expressions: Solutions</strong></p>

docs/_sources/Module2_EssentialsOfPython/Basic_Objects.ipynb.txt

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
"\n",
242242
"Floats have a couple of built-in functions available to them, as detailed in the [official documentation](https://docs.python.org/3/library/stdtypes.html#additional-methods-on-float).\n",
243243
"\n",
244+
"\n",
244245
"#### Scientific Notation\n",
245246
"A float can also be created using familiar scientific notation. The character `e` is used to represent $\\times 10$, and the proceeding number is the exponent. Here are some examples of traditional scientific notation, and their corresponding representation in Python:\n",
246247
"\n",
@@ -254,12 +255,17 @@
254255
"# scientific notation\n",
255256
">>> 0.0000001 # seven leading-zeros\n",
256257
"1e-07\n",
257-
"```\n",
258-
"\n",
258+
"```"
259+
]
260+
},
261+
{
262+
"cell_type": "markdown",
263+
"metadata": {},
264+
"source": [
259265
"#### Understanding Numerical Precision\n",
260266
"Whereas a Python integer can be made to be as large as you'd like, a floating-point number is *limited in the number of digits it can store*. That is, your computer will only use a set amount of memory, 8 bytes (32 bits) on most machines, to store the value of a floating-point number. \n",
261267
"\n",
262-
"In effect, this means that a float can only be represented with a *numerical precision* of approximately 16 decimal places, when that number is written in scientific notation. The computer will not be able to reliably represent a number's digits beyond those accounted for by the allotted 8 bytes. For instance, the following Python integer is defined with 23 digits, but when this number is converted to a float, it only retains 16 decimal places in scientific notation: \n",
268+
"In effect, this means that a float can only be represented with a *numerical precision* of approximately 16 decimal places, when that number is written in scientific notation. The computer will not be able to reliably represent a number's digits beyond those accounted for by the allotted 8 bytes. For instance, the following Python integer is defined with 100 digits, but when this number is converted to a float, it only retains 15 decimal places in scientific notation: \n",
263269
"```python\n",
264270
"# Demonstrating the finite-precision of a float.\n",
265271
"\n",
@@ -275,7 +281,9 @@
275281
">>> float(\"1\"*100) # creates a string with 100 1s and makes it a float\n",
276282
"1.111111111111111e+99\n",
277283
"```\n",
278-
"The computer cannot keep track of those last 6 decimal places, because doing so would require more than 8 bytes of memory to store the entire value of that float. As such, attempting to modify a number in decimal places beyond its numerical precision does not have any effect:\n",
284+
"The computer cannot keep track of those last 84 decimal places because doing so would require more than 8 bytes of memory to store the entire value of that float. If you had been diligently counting stars in the sky (perhaps across many universes, this number far exceeds the estimated number of stars in our universe), you would have just lost track of over $1\\times10^{83}$ of them simply by converting your integer count to a float! \n",
285+
"\n",
286+
"As such, attempting to modify a floating point number in decimal places beyond its numerical precision does not have any effect:\n",
279287
"\n",
280288
"```python\n",
281289
"# changing a float beyond its precision has no effect \n",
@@ -336,13 +344,28 @@
336344
">>> complex(2, 3)\n",
337345
"(2+3j)\n",
338346
"\n",
347+
">>> complex(0, 1)**2\n",
348+
"(-1+0j)\n",
349+
"\n",
339350
">>> type(2+3j)\n",
340351
"complex\n",
341352
"\n",
342353
">>> isinstance(2-4j, complex)\n",
343354
"True\n",
344355
"```\n",
345356
"\n",
357+
"Note that `j` is not, by itself, reserved as a special placeholder for $i$. Rather, `j` must be preceded immediately with a numerical literal (i.e. you cannot use a variable) in order for the Python interpreter to treat it as a complex number.\n",
358+
"\n",
359+
"```python\n",
360+
"# `j` by itself is treated like any other character\n",
361+
">>> j\n",
362+
"NameError: name 'j' is not defined\n",
363+
"\n",
364+
"# `1j` is interpreted as the imaginary number\n",
365+
">>> (1j) ** 2\n",
366+
"(-1+0j)\n",
367+
"```\n",
368+
"\n",
346369
"You can access `a` and `b` from `a + bj`, the real and imaginary parts of the complex number, respectively.\n",
347370
"```python\n",
348371
"# Accessing the real and imaginary parts of\n",
@@ -381,6 +404,7 @@
381404
"\n",
382405
"3\\. Given the function $f(x) = e^{|x - 2|}$, make use of the `math` module to compute $f(-0.2)$.\n",
383406
"\n",
407+
"4\\. Using Python's syntax for scientific notation, write an expression that verifies that one trillion divided by one billion is equal to one thousand\n",
384408
"</div>"
385409
]
386410
},
@@ -417,6 +441,36 @@
417441
"- `x **= n` $\\rightarrow$ `x = x ** n`"
418442
]
419443
},
444+
{
445+
"cell_type": "markdown",
446+
"metadata": {},
447+
"source": [
448+
"### Improving The Readability of Numbers\n",
449+
"Python version 3.6 introduced the ability to include underscores between the digits of a number as a visual delimiter. This character can be used to improve the readability of long numbers in your code. For example the number `662607004` can be rewritten as `662_607_004`, using `_` to delimit digits separated by orders of one thousand. Leading, trailing, or multiple underscores in a row are not allowed; otherwise this character can be included anywhere within a numerical literal.\n",
450+
"\n",
451+
"```python\n",
452+
"# examples of using `_` as a visual delimiter in numbers\n",
453+
">>> 1_000_000 # this is nice!\n",
454+
"1000000\n",
455+
"\n",
456+
"# this is gross but is permitted\n",
457+
">>> 2_3_4.5_6_7 \n",
458+
"234.567\n",
459+
"\n",
460+
"# underscores work with all variety of numerical literals\n",
461+
">>> 10_000j \n",
462+
"10000j\n",
463+
"```\n",
464+
"\n",
465+
"<div class=\"alert alert-warning\">\n",
466+
"\n",
467+
"**Compatibility Warning**\n",
468+
"\n",
469+
"The permitted use of the underscore character, `_`, in numerical literals was introduced in Python 3.6. Thus utilizing this syntax in your code will render it incompatible with Python 3.5 and earlier. \n",
470+
"\n",
471+
"</div>"
472+
]
473+
},
420474
{
421475
"cell_type": "markdown",
422476
"metadata": {},
@@ -1005,13 +1059,22 @@
10051059
"1.0\n",
10061060
"```\n",
10071061
"\n",
1062+
"3\\. Given the function $f(x) = e^{|x - 2|}$, make use of the `math` module to compute $f(-0.2)$.\n",
10081063
"```python\n",
1009-
"# 3. Given the function f(x)=e^|x−2|f(x), make use \n",
1010-
"# of the math module to compute f(−0.2).\n",
10111064
">>> from math import exp\n",
10121065
">>> x = -0.2\n",
10131066
">>> exp(abs(x - 2))\n",
10141067
"9.025013499434122\n",
1068+
"```\n",
1069+
"\n",
1070+
"4\\. Using Python's syntax for scientific notation, write an expression that verifies that one trillion divided by one billion is equal to one thousand.\n",
1071+
"\n",
1072+
"> As cautioned above, we should avoid checking to see if two floating point numbers are exactly equal, and instead simply ensure that they are close in value. Keep in mind that a number written Python's using scientific syntax will produce a float. \n",
1073+
"\n",
1074+
"```python\n",
1075+
">>> from math import isclose\n",
1076+
">>> isclose(1e12 / 1e9, 1e3)\n",
1077+
"True\n",
10151078
"```"
10161079
]
10171080
},

0 commit comments

Comments
 (0)