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>You can access <codeclass="docutils literal"><spanclass="pre">a</span></code> and <codeclass="docutils literal"><spanclass="pre">b</span></code> from <codeclass="docutils literal"><spanclass="pre">a</span><spanclass="pre">+</span><spanclass="pre">bj</span></code>, the real and imaginary
673
695
parts of the complex number, respectively.</p>
674
696
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># Accessing the real and imaginary parts of</span>
<h3>Improving The Readability of Numbers<aclass="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 <codeclass="docutils literal"><spanclass="pre">662607004</span></code> can be rewritten as <codeclass="docutils literal"><spanclass="pre">662_607_004</span></code>, using <codeclass="docutils literal"><spanclass="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
+
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># examples of using `_` as a visual delimiter in numbers</span>
779
+
<spanclass="o">>>></span><spanclass="mi">1</span><spanclass="n">_000_000</span><spanclass="c1"># this is nice!</span>
780
+
<spanclass="mi">1000000</span>
781
+
782
+
<spanclass="c1"># this is gross but is permitted</span>
Copy file name to clipboardExpand all lines: docs/_sources/Module2_EssentialsOfPython/Basic_Objects.ipynb.txt
+69-6Lines changed: 69 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -241,6 +241,7 @@
241
241
"\n",
242
242
"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",
243
243
"\n",
244
+
"\n",
244
245
"#### Scientific Notation\n",
245
246
"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",
246
247
"\n",
@@ -254,12 +255,17 @@
254
255
"# scientific notation\n",
255
256
">>> 0.0000001 # seven leading-zeros\n",
256
257
"1e-07\n",
257
-
"```\n",
258
-
"\n",
258
+
"```"
259
+
]
260
+
},
261
+
{
262
+
"cell_type": "markdown",
263
+
"metadata": {},
264
+
"source": [
259
265
"#### Understanding Numerical Precision\n",
260
266
"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",
261
267
"\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",
263
269
"```python\n",
264
270
"# Demonstrating the finite-precision of a float.\n",
265
271
"\n",
@@ -275,7 +281,9 @@
275
281
">>> float(\"1\"*100) # creates a string with 100 1s and makes it a float\n",
276
282
"1.111111111111111e+99\n",
277
283
"```\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",
279
287
"\n",
280
288
"```python\n",
281
289
"# changing a float beyond its precision has no effect \n",
@@ -336,13 +344,28 @@
336
344
">>> complex(2, 3)\n",
337
345
"(2+3j)\n",
338
346
"\n",
347
+
">>> complex(0, 1)**2\n",
348
+
"(-1+0j)\n",
349
+
"\n",
339
350
">>> type(2+3j)\n",
340
351
"complex\n",
341
352
"\n",
342
353
">>> isinstance(2-4j, complex)\n",
343
354
"True\n",
344
355
"```\n",
345
356
"\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",
346
369
"You can access `a` and `b` from `a + bj`, the real and imaginary parts of the complex number, respectively.\n",
347
370
"```python\n",
348
371
"# Accessing the real and imaginary parts of\n",
@@ -381,6 +404,7 @@
381
404
"\n",
382
405
"3\\. Given the function $f(x) = e^{|x - 2|}$, make use of the `math` module to compute $f(-0.2)$.\n",
383
406
"\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",
384
408
"</div>"
385
409
]
386
410
},
@@ -417,6 +441,36 @@
417
441
"- `x **= n` $\\rightarrow$ `x = x ** n`"
418
442
]
419
443
},
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
+
},
420
474
{
421
475
"cell_type": "markdown",
422
476
"metadata": {},
@@ -1005,13 +1059,22 @@
1005
1059
"1.0\n",
1006
1060
"```\n",
1007
1061
"\n",
1062
+
"3\\. Given the function $f(x) = e^{|x - 2|}$, make use of the `math` module to compute $f(-0.2)$.\n",
1008
1063
"```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",
1011
1064
">>> from math import exp\n",
1012
1065
">>> x = -0.2\n",
1013
1066
">>> exp(abs(x - 2))\n",
1014
1067
"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",
0 commit comments