From cf0a8acd1752704956f22026980317bded3ea21d Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sat, 7 Mar 2020 16:40:11 +0100 Subject: [PATCH 1/7] Streamline presentation --- 00_intro_00_lecture.ipynb | 10 +++++----- 00_intro_01_review.ipynb | 2 +- 00_intro_02_exercises.ipynb | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/00_intro_00_lecture.ipynb b/00_intro_00_lecture.ipynb index a8d2237..e53b008 100644 --- a/00_intro_00_lecture.ipynb +++ b/00_intro_00_lecture.ipynb @@ -77,7 +77,7 @@ "cell_type": "markdown", "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "skip" } }, "source": [ @@ -350,7 +350,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Python 3.7.4\n" + "Python 3.7.6\n" ] } ], @@ -472,7 +472,7 @@ "cell_type": "markdown", "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "skip" } }, "source": [ @@ -673,7 +673,7 @@ "cell_type": "markdown", "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "skip" } }, "source": [ @@ -903,7 +903,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "livereveal": { "auto_select": "code", diff --git a/00_intro_01_review.ipynb b/00_intro_01_review.ipynb index f08c6ca..325fc74 100644 --- a/00_intro_01_review.ipynb +++ b/00_intro_01_review.ipynb @@ -206,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "toc": { "base_numbering": 1, diff --git a/00_intro_02_exercises.ipynb b/00_intro_02_exercises.ipynb index 3b394db..b0f53db 100644 --- a/00_intro_02_exercises.ipynb +++ b/00_intro_02_exercises.ipynb @@ -94,7 +94,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "toc": { "base_numbering": 1, From d0a20dfdee35376d869f9257b5e1c29c925d7f24 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sat, 7 Mar 2020 18:37:33 +0100 Subject: [PATCH 2/7] Streamline presentation --- 01_elements_00_lecture.ipynb | 766 ++++++++++++++++++----------------- 1 file changed, 404 insertions(+), 362 deletions(-) diff --git a/01_elements_00_lecture.ipynb b/01_elements_00_lecture.ipynb index ba6e476..f7583ba 100644 --- a/01_elements_00_lecture.ipynb +++ b/01_elements_00_lecture.ipynb @@ -35,7 +35,7 @@ } }, "source": [ - "## Example: Averaging Even Numbers" + "## Example: Averaging all even Numbers in a List" ] }, { @@ -137,7 +137,7 @@ "total = 0 # running total and the count of even numbers\n", "\n", "for number in numbers:\n", - " if number % 2 == 0: # only look at even numbers\n", + " if number % 2 == 0: # only work with even numbers\n", " count = count + 1\n", " total = total + number\n", "\n", @@ -233,7 +233,7 @@ "execution_count": 6, "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "skip" } }, "outputs": [], @@ -257,7 +257,7 @@ "execution_count": 7, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "fragment" } }, "outputs": [ @@ -365,7 +365,7 @@ "execution_count": 10, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -435,7 +435,7 @@ "execution_count": 12, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "fragment" } }, "outputs": [ @@ -459,7 +459,7 @@ "execution_count": 13, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "slide" } }, "outputs": [ @@ -494,7 +494,7 @@ "execution_count": 14, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "fragment" } }, "outputs": [ @@ -518,7 +518,7 @@ "execution_count": 15, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -669,7 +669,7 @@ { "data": { "text/plain": [ - "3" + "9" ] }, "execution_count": 19, @@ -678,7 +678,7 @@ } ], "source": [ - "123 % 10" + "789 % 10" ] }, { @@ -686,14 +686,14 @@ "execution_count": 20, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ - "23" + "89" ] }, "execution_count": 20, @@ -702,7 +702,7 @@ } ], "source": [ - "123 % 100" + "789 % 100" ] }, { @@ -713,7 +713,7 @@ } }, "source": [ - "The built-in [divmod()](https://docs.python.org/3/library/functions.html#divmod) function combines the integer and modulo divisions into one operation. However, grammatically this is *not* an operator but a function. Also, [divmod()](https://docs.python.org/3/library/functions.html#divmod) returns a \"pair\" of integers and not just one." + "The built-in [divmod()](https://docs.python.org/3/library/functions.html#divmod) function combines the integer and modulo divisions into one step. However, grammatically this is *not* an operator but a function. Also, [divmod()](https://docs.python.org/3/library/functions.html#divmod) returns a \"pair\" of integers and not a single one." ] }, { @@ -721,7 +721,7 @@ "execution_count": 21, "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "skip" } }, "outputs": [ @@ -826,7 +826,7 @@ "execution_count": 24, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -842,7 +842,7 @@ } ], "source": [ - "(3 ** 2) * 2 # same result as before but much clearer code" + "(3 ** 2) * 2" ] }, { @@ -850,7 +850,7 @@ "execution_count": 25, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -866,7 +866,7 @@ } ], "source": [ - "3 ** (2 * 2) # different result" + "3 ** (2 * 2)" ] }, { @@ -951,7 +951,7 @@ }, "outputs": [], "source": [ - "a = 789\n", + "a = 42\n", "b = 42.0\n", "c = \"Python rocks\"" ] @@ -1007,14 +1007,14 @@ "execution_count": 29, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ - "139627575613200" + "140361812063056" ] }, "execution_count": 29, @@ -1058,7 +1058,9 @@ } }, "source": [ - "These addresses are *not* meaningful for anything other than checking if two variables reference the *same* object. Let's create a new variable `d` and also set it to `789`." + "These addresses are *not* meaningful for anything other than checking if two variables reference the *same* object.\n", + "\n", + "Obviously, `a` and `b` have the same *value* as revealed by the **equality operator** `==`: We say `a` and `b` \"evaluate equal.\" The resulting `True` - and the `False` further below - is yet another data type, a so-called **boolean**. We look into them in [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb#Boolean-Expressions)." ] }, { @@ -1069,30 +1071,6 @@ "slide_type": "slide" } }, - "outputs": [], - "source": [ - "d = 789" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "`a` and `d` indeed have the *same value* as is checked with the **equality operator** `==`: We say `a` and `d` \"evaluate equal.\" The resulting `True` - and the `False` further below - is yet another data type, a so-called **boolean**. We look into them in [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb#Boolean-Expressions)." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "slideshow": { - "slide_type": "-" - } - }, "outputs": [ { "data": { @@ -1100,13 +1078,13 @@ "True" ] }, - "execution_count": 32, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a == d" + "a == b" ] }, { @@ -1117,15 +1095,15 @@ } }, "source": [ - "On the contrary, `a` and `d` are *different objects* as the **identity operator** `is` shows: They are stored at *different* addresses in the memory." + "On the contrary, `a` and `b` are *different* objects as the **identity operator** `is` shows: They are stored at *different* addresses in the memory." ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 32, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -1135,13 +1113,13 @@ "False" ] }, - "execution_count": 33, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a is d" + "a is b" ] }, { @@ -1157,7 +1135,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 33, "metadata": { "slideshow": { "slide_type": "skip" @@ -1170,13 +1148,13 @@ "True" ] }, - "execution_count": 34, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a is not d" + "a is not b" ] }, { @@ -1203,7 +1181,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 34, "metadata": { "slideshow": { "slide_type": "slide" @@ -1216,7 +1194,7 @@ "int" ] }, - "execution_count": 35, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -1227,10 +1205,10 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 35, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -1240,7 +1218,7 @@ "float" ] }, - "execution_count": 36, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -1257,14 +1235,14 @@ } }, "source": [ - "Different types imply different behaviors for the objects. The `b` object, for example, may be \"asked\" if it is a whole number with the [is_integer()](https://docs.python.org/3/library/stdtypes.html#float.is_integer) \"functionality\" that comes with every `float` object.\n", + "Different types imply different behaviors for the objects. The `b` object, for example, may be \"asked\" if it is a whole number with the [is_integer()](https://docs.python.org/3/library/stdtypes.html#float.is_integer) \"functionality\" that comes with *every* `float` object.\n", "\n", - "Formally, we call such type-specific functionalities **methods** (i.e., as opposed to functions) and we formally introduce them in Chapter 10. For now, it suffices to know that we access them with the **dot operator** `.` on the object. Of course, `b` is a whole number, which the boolean object `True` tells us." + "Formally, we call such type-specific functionalities **methods** (i.e., as opposed to functions) and we look at them in detail in Chapter 10. For now, it suffices to know that we access them with the **dot operator** `.` on the object. Of course, `b` is a whole number, which the boolean object `True` tells us." ] }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 36, "metadata": { "slideshow": { "slide_type": "fragment" @@ -1277,7 +1255,7 @@ "True" ] }, - "execution_count": 37, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -1299,10 +1277,10 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 37, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -1313,7 +1291,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'is_integer'" ] } @@ -1335,7 +1313,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 38, "metadata": { "slideshow": { "slide_type": "slide" @@ -1348,7 +1326,7 @@ "str" ] }, - "execution_count": 39, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -1357,6 +1335,30 @@ "type(c)" ] }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'python rocks'" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c.lower()" + ] + }, { "cell_type": "code", "execution_count": 40, @@ -1365,30 +1367,6 @@ "slide_type": "fragment" } }, - "outputs": [ - { - "data": { - "text/plain": [ - "'python rocks'" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "c.lower()" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "slideshow": { - "slide_type": "-" - } - }, "outputs": [ { "data": { @@ -1396,7 +1374,7 @@ "'PYTHON ROCKS'" ] }, - "execution_count": 41, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -1407,7 +1385,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 41, "metadata": { "slideshow": { "slide_type": "skip" @@ -1420,7 +1398,7 @@ "'Python Rocks'" ] }, - "execution_count": 42, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -1437,7 +1415,7 @@ } }, "source": [ - "### Value / \"Meaning\"" + "### Value / (Semantic) \"Meaning\"" ] }, { @@ -1455,7 +1433,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 42, "metadata": { "slideshow": { "slide_type": "slide" @@ -1465,10 +1443,10 @@ { "data": { "text/plain": [ - "789" + "42" ] }, - "execution_count": 43, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -1479,10 +1457,10 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 43, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -1492,7 +1470,7 @@ "42.0" ] }, - "execution_count": 44, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -1509,12 +1487,12 @@ } }, "source": [ - "In this book, we follow the convention of creating strings with **double quotes** `\"` instead of the **single quotes** `'` to which Python defaults in its *literal* notation for `str` objects. Both types of quotes may be used interchangeably. So, the `\"Python rocks\"` from above and `'Python rocks'` create two objects that evaluate equal (i.e., `\"Python rocks\" == 'Python rocks'`)." + "In this book, we follow the convention of creating strings with **double quotes** `\"` instead of the **single quotes** `'` to which Python defaults in its *literal* notation for `str` objects. Both types of quotes may be used interchangeably. So, the `\"Python rocks\"` from above and `'Python rocks'` below create two objects that evaluate equal (i.e., `\"Python rocks\" == 'Python rocks'`)." ] }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 44, "metadata": { "slideshow": { "slide_type": "-" @@ -1527,7 +1505,7 @@ "'Python rocks'" ] }, - "execution_count": 45, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } @@ -1586,7 +1564,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 45, "metadata": { "slideshow": { "slide_type": "slide" @@ -1595,10 +1573,10 @@ "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 3.99 $ + 10.40 $\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 3.99 $ + 10.40 $\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -1619,7 +1597,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 46, "metadata": { "slideshow": { "slide_type": "slide" @@ -1628,10 +1606,10 @@ "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m for number in numbers\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m for number in numbers\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -1653,7 +1631,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 47, "metadata": { "slideshow": { "slide_type": "slide" @@ -1662,10 +1640,10 @@ "outputs": [ { "ename": "IndentationError", - "evalue": "expected an indented block (, line 2)", + "evalue": "expected an indented block (, line 2)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(number)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(number)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], @@ -1702,7 +1680,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 48, "metadata": { "slideshow": { "slide_type": "slide" @@ -1716,7 +1694,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } @@ -1751,7 +1729,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 49, "metadata": { "code_folding": [], "slideshow": { @@ -1773,7 +1751,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 50, "metadata": { "slideshow": { "slide_type": "-" @@ -1786,7 +1764,7 @@ "3.5" ] }, - "execution_count": 51, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -1832,7 +1810,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 51, "metadata": { "slideshow": { "slide_type": "skip" @@ -1854,10 +1832,10 @@ " " ], "text/plain": [ - "" + "" ] }, - "execution_count": 52, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -1880,7 +1858,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 52, "metadata": { "slideshow": { "slide_type": "slide" @@ -1893,7 +1871,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 53, "metadata": { "slideshow": { "slide_type": "-" @@ -1901,12 +1879,12 @@ }, "outputs": [], "source": [ - "evens = [n for n in numbers if n % 2 == 0] # example of a so-called list comprehension" + "evens = [n for n in numbers if n % 2 == 0] # use of a list comprehension" ] }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 54, "metadata": { "slideshow": { "slide_type": "-" @@ -1919,7 +1897,7 @@ "[8, 12, 2, 6, 10, 4]" ] }, - "execution_count": 55, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -1930,7 +1908,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 55, "metadata": { "slideshow": { "slide_type": "-" @@ -1938,12 +1916,12 @@ }, "outputs": [], "source": [ - "average = sum(evens) / len(evens) # built-in functions are much faster than a for-loop" + "average = sum(evens) / len(evens) # use built-in functions" ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 56, "metadata": { "slideshow": { "slide_type": "-" @@ -1956,7 +1934,7 @@ "7.0" ] }, - "execution_count": 57, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -1978,7 +1956,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 57, "metadata": { "slideshow": { "slide_type": "slide" @@ -2106,7 +2084,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 58, "metadata": { "slideshow": { "slide_type": "slide" @@ -2114,8 +2092,7 @@ }, "outputs": [], "source": [ - "a = 20.0\n", - "b = 789" + "variable = 20.0" ] }, { @@ -2126,12 +2103,12 @@ } }, "source": [ - "When used as a *literal*, a variable evaluates to the value of the object it references. Colloquially, we could say that `a` evaluates to `20.0`, but this would not be an accurate description of what is going on in memory. We see some more colloquialisms in this section but should always relate this to what Python actually does in memory." + "When used as a *literal*, a variable evaluates to the value of the object it references. Colloquially, we could say that `variable` evaluates to `20.0`, but this would not be an accurate description of what is going on in memory. We see some more colloquialisms in this section but should always relate this to what Python actually does in memory." ] }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 59, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2144,13 +2121,13 @@ "20.0" ] }, - "execution_count": 60, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a" + "variable" ] }, { @@ -2166,7 +2143,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 60, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2174,15 +2151,15 @@ }, "outputs": [], "source": [ - "a = 20 # this makes a reference an object of a different type" + "variable = 20" ] }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 61, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2192,13 +2169,13 @@ "20" ] }, - "execution_count": 62, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a" + "variable" ] }, { @@ -2214,7 +2191,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 62, "metadata": { "slideshow": { "slide_type": "slide" @@ -2222,7 +2199,31 @@ }, "outputs": [], "source": [ - "a *= 4 # same as a = a * 4" + "variable *= 4 # same as variable = variable * 4" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "slideshow": { + "slide_type": "-" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "variable" ] }, { @@ -2230,12 +2231,12 @@ "execution_count": 64, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [], "source": [ - "a //= 2 # same as a = a // 2; \"//\" to retain the integer type" + "variable //= 2 # same as variable = variable // 2; \"//\" to retain the integer type" ] }, { @@ -2246,14 +2247,38 @@ "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "40" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "a += 2 # same as a = a + 2" + "variable" ] }, { "cell_type": "code", "execution_count": 66, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "variable += 2 # same as variable = variable + 2" + ] + }, + { + "cell_type": "code", + "execution_count": 67, "metadata": { "slideshow": { "slide_type": "-" @@ -2266,13 +2291,13 @@ "42" ] }, - "execution_count": 66, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a" + "variable" ] }, { @@ -2288,7 +2313,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 68, "metadata": { "slideshow": { "slide_type": "slide" @@ -2298,29 +2323,29 @@ { "data": { "text/plain": [ - "789" + "42" ] }, - "execution_count": 67, + "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "b" + "variable" ] }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 69, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [], "source": [ - "del b" + "del variable" ] }, { @@ -2336,27 +2361,27 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 70, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ { "ename": "NameError", - "evalue": "name 'b' is not defined", + "evalue": "name 'variable' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'b' is not defined" + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mvariable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'variable' is not defined" ] } ], "source": [ - "b" + "variable" ] }, { @@ -2372,7 +2397,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 71, "metadata": { "slideshow": { "slide_type": "skip" @@ -2385,7 +2410,7 @@ "'__main__'" ] }, - "execution_count": 70, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -2407,10 +2432,10 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 72, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "skip" } }, "outputs": [ @@ -2442,12 +2467,13 @@ " '_28',\n", " '_29',\n", " '_30',\n", + " '_31',\n", " '_32',\n", " '_33',\n", " '_34',\n", " '_35',\n", " '_36',\n", - " '_37',\n", + " '_38',\n", " '_39',\n", " '_4',\n", " '_40',\n", @@ -2455,17 +2481,18 @@ " '_42',\n", " '_43',\n", " '_44',\n", - " '_45',\n", " '_5',\n", + " '_50',\n", " '_51',\n", - " '_52',\n", - " '_55',\n", - " '_57',\n", - " '_60',\n", - " '_62',\n", - " '_66',\n", + " '_54',\n", + " '_56',\n", + " '_59',\n", + " '_61',\n", + " '_63',\n", + " '_65',\n", " '_67',\n", - " '_70',\n", + " '_68',\n", + " '_71',\n", " '_9',\n", " '__',\n", " '___',\n", @@ -2547,6 +2574,7 @@ " '_i7',\n", " '_i70',\n", " '_i71',\n", + " '_i72',\n", " '_i8',\n", " '_i9',\n", " '_ih',\n", @@ -2555,9 +2583,9 @@ " '_oh',\n", " 'a',\n", " 'average',\n", + " 'b',\n", " 'c',\n", " 'count',\n", - " 'd',\n", " 'evens',\n", " 'exit',\n", " 'get_ipython',\n", @@ -2568,7 +2596,7 @@ " 'total']" ] }, - "execution_count": 71, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -2603,23 +2631,36 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 73, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], + "source": [ + "a = 42" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 75, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2629,31 +2670,7 @@ "42" ] }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "slideshow": { - "slide_type": "-" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "42" - ] - }, - "execution_count": 74, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -2672,20 +2689,7 @@ "source": [ "For \"simple\" types like `int` or `float` this never causes troubles.\n", "\n", - "Let's \"change the value\" of `a`. To be precise, let's create a *new* `123` object and make `a` reference it." - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "a = 123" + "Let's \"change the value\" of `a`. To be precise, let's create a *new* `87` object and make `a` reference it." ] }, { @@ -2693,17 +2697,30 @@ "execution_count": 76, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "a = 87" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": { + "slideshow": { + "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ - "123" + "87" ] }, - "execution_count": 76, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -2725,7 +2742,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 78, "metadata": { "slideshow": { "slide_type": "-" @@ -2738,7 +2755,7 @@ "42" ] }, - "execution_count": 77, + "execution_count": 78, "metadata": {}, "output_type": "execute_result" } @@ -2760,7 +2777,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 79, "metadata": { "slideshow": { "slide_type": "slide" @@ -2773,7 +2790,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 80, "metadata": { "slideshow": { "slide_type": "skip" @@ -2786,7 +2803,7 @@ "list" ] }, - "execution_count": 79, + "execution_count": 80, "metadata": {}, "output_type": "execute_result" } @@ -2797,10 +2814,10 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 81, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [], @@ -2808,6 +2825,30 @@ "y = x" ] }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y" + ] + }, { "cell_type": "markdown", "metadata": { @@ -2825,10 +2866,10 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 83, "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "slide" } }, "outputs": [ @@ -2838,7 +2879,7 @@ "1" ] }, - "execution_count": 81, + "execution_count": 83, "metadata": {}, "output_type": "execute_result" } @@ -2860,10 +2901,10 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 84, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "fragment" } }, "outputs": [], @@ -2873,10 +2914,10 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 85, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2886,7 +2927,7 @@ "[99, 2, 3]" ] }, - "execution_count": 83, + "execution_count": 85, "metadata": {}, "output_type": "execute_result" } @@ -2908,7 +2949,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 86, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2921,7 +2962,7 @@ "[99, 2, 3]" ] }, - "execution_count": 84, + "execution_count": 86, "metadata": {}, "output_type": "execute_result" } @@ -2940,7 +2981,7 @@ "source": [ "The difference in behavior illustrated in this sub-section has to do with the fact that `int` and `float` objects are **immutable** types while `list` objects are **mutable**.\n", "\n", - "In the first case, an object cannot be changed \"in place\" once it is created in memory. When we assigned `123` to the already existing `a`, we did *not* change the $0$s and $1$s in the object `a` referenced before the assignment but created a new `int` object and made `a` reference it while the `b` variable is *not* affected.\n", + "In the first case, an object cannot be changed \"in place\" once it is created in memory. When we assigned `87` to the already existing `a`, we did *not* change the $0$s and $1$s in the object `a` referenced before the assignment but created a new `int` object and made `a` reference it while the `b` variable is *not* affected.\n", "\n", "In the second case, `x[0] = 99` creates a *new* `int` object `99` and merely changes the first reference in the `x` list.\n", "\n", @@ -3001,7 +3042,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 87, "metadata": { "slideshow": { "slide_type": "slide" @@ -3014,7 +3055,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 88, "metadata": { "slideshow": { "slide_type": "-" @@ -3027,7 +3068,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 89, "metadata": { "slideshow": { "slide_type": "-" @@ -3040,7 +3081,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 90, "metadata": { "slideshow": { "slide_type": "-" @@ -3048,7 +3089,7 @@ }, "outputs": [], "source": [ - "work_address = \"Burgplatz 2, Vallendar\"" + "work_address = \"WHU, Burgplatz 2, Vallendar\"" ] }, { @@ -3064,7 +3105,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 91, "metadata": { "slideshow": { "slide_type": "skip" @@ -3077,7 +3118,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 92, "metadata": { "slideshow": { "slide_type": "skip" @@ -3090,7 +3131,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 93, "metadata": { "slideshow": { "slide_type": "skip" @@ -3103,7 +3144,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 94, "metadata": { "slideshow": { "slide_type": "skip" @@ -3112,15 +3153,15 @@ "outputs": [ { "ename": "SyntaxError", - "evalue": "can't assign to operator (, line 1)", + "evalue": "can't assign to operator (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m address@work = \"Burgplatz 2, Vallendar\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to operator\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m address@work = \"WHU, Burgplatz 2, Vallendar\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to operator\n" ] } ], "source": [ - "address@work = \"Burgplatz 2, Vallendar\"" + "address@work = \"WHU, Burgplatz 2, Vallendar\"" ] }, { @@ -3136,7 +3177,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 95, "metadata": { "slideshow": { "slide_type": "skip" @@ -3160,7 +3201,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 96, "metadata": { "slideshow": { "slide_type": "skip" @@ -3173,7 +3214,7 @@ "'__main__'" ] }, - "execution_count": 94, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -3212,7 +3253,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 97, "metadata": { "slideshow": { "slide_type": "slide" @@ -3222,10 +3263,10 @@ { "data": { "text/plain": [ - "123" + "87" ] }, - "execution_count": 95, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } @@ -3236,7 +3277,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 98, "metadata": { "slideshow": { "slide_type": "fragment" @@ -3249,7 +3290,7 @@ "42" ] }, - "execution_count": 96, + "execution_count": 98, "metadata": {}, "output_type": "execute_result" } @@ -3269,76 +3310,6 @@ "For sure, we need to include operators to achieve something useful." ] }, - { - "cell_type": "code", - "execution_count": 97, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "165" - ] - }, - "execution_count": 97, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a + b" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "The definition of an expression is **recursive**. So, the sub-expression `a + b` is combined with the literal `3` by the operator `**` to form the full expression `(a + b) ** 3`." - ] - }, - { - "cell_type": "code", - "execution_count": 98, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4492125" - ] - }, - "execution_count": 98, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "(a + b) ** 3" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "Here, the variable `y` is combined with the literal `2` by the indexing operator `[]`. The resulting expression evaluates to the third element in the `y` list." - ] - }, { "cell_type": "code", "execution_count": 99, @@ -3351,7 +3322,7 @@ { "data": { "text/plain": [ - "3" + "45" ] }, "execution_count": 99, @@ -3360,7 +3331,77 @@ } ], "source": [ - "y[2]" + "a - 42" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "The definition of an expression is **recursive**. So, the sub-expression `a - 42` is combined with the literal `9` by the operator `//` to form the full expression `(a - 42) // 9`." + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(a - 42) // 9" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "Here, the variable `x` is combined with the literal `2` by the indexing operator `[]`. The resulting expression evaluates to the third element in the `x` list." + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x[2]" ] }, { @@ -3376,10 +3417,10 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 102, "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "skip" } }, "outputs": [ @@ -3389,7 +3430,7 @@ "104" ] }, - "execution_count": 100, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } @@ -3422,7 +3463,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 103, "metadata": { "slideshow": { "slide_type": "slide" @@ -3436,7 +3477,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 104, "metadata": { "slideshow": { "slide_type": "fragment" @@ -3449,7 +3490,7 @@ "'Hi class'" ] }, - "execution_count": 102, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -3471,7 +3512,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 105, "metadata": { "slideshow": { "slide_type": "fragment" @@ -3481,16 +3522,16 @@ { "data": { "text/plain": [ - "'Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi '" + "'Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi '" ] }, - "execution_count": 103, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "b * greeting" + "10 * greeting" ] }, { @@ -3519,7 +3560,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 106, "metadata": { "slideshow": { "slide_type": "slide" @@ -3527,12 +3568,12 @@ }, "outputs": [], "source": [ - "a = 123" + "a = 42" ] }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 107, "metadata": { "slideshow": { "slide_type": "-" @@ -3556,7 +3597,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 108, "metadata": { "slideshow": { "slide_type": "skip" @@ -3603,7 +3644,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 109, "metadata": { "slideshow": { "slide_type": "slide" @@ -3613,6 +3654,7 @@ "source": [ "distance = 891 # in meters\n", "elapsed_time = 93 # in seconds\n", + "\n", "# Calculate the speed in km/h.\n", "speed = 3.6 * distance / elapsed_time" ] @@ -3631,7 +3673,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 110, "metadata": { "slideshow": { "slide_type": "fragment" @@ -3644,10 +3686,10 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 111, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [], @@ -3772,7 +3814,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 112, "metadata": { "slideshow": { "slide_type": "skip" @@ -3794,10 +3836,10 @@ " " ], "text/plain": [ - "" + "" ] }, - "execution_count": 110, + "execution_count": 112, "metadata": {}, "output_type": "execute_result" } @@ -3823,7 +3865,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "livereveal": { "auto_select": "code", From ed3da36fcf616193b9f4825162baea717393836a Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sun, 8 Mar 2020 17:10:41 +0100 Subject: [PATCH 3/7] Add more exercises to chapter 01 --- 01_elements_02_exercises.ipynb | 338 ++++++++++++++++++++++++++++++++- 1 file changed, 332 insertions(+), 6 deletions(-) diff --git a/01_elements_02_exercises.ipynb b/01_elements_02_exercises.ipynb index 64fc68f..3f5026a 100644 --- a/01_elements_02_exercises.ipynb +++ b/01_elements_02_exercises.ipynb @@ -19,7 +19,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Read [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb) of the book. Then, work through the exercises below." + "Read [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_content.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells." ] }, { @@ -33,9 +33,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1**: Read the documentation on the built-in [print()](https://docs.python.org/3/library/functions.html#print) function! How can you use it to print both `greeting` and `audience` *without* concatenating the two strings with the `+` operator?\n", + "**Q1.1**: *Concatenate* `greeting` and `audience` below with the `+` operator and print out the resulting message `\"Hello World\"` with only *one* call of the built-in [print()](https://docs.python.org/3/library/functions.html#print) function!\n", "\n", - "Hint: The `*objects` in the documentation implies that we can insert several *comma-seperated* variables." + "Hint: You may have to \"add\" a space character in between `greeting` and `audience`." ] }, { @@ -61,7 +61,53 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q2**: What does the `sep=\" \"` mean in the documentation? Use it and print out the three names in `first`, `second`, and `third` on one line seperated by *commas* with only *one* call of the [print()](https://docs.python.org/3/library/functions.html#print) function!" + "**Q1.2**: How is your answer to **Q1.1** an example of the concept of **operator overloading**?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q1.3**: Read the documentation on the built-in [print()](https://docs.python.org/3/library/functions.html#print) function! How can you print the above message *without* concatenating `greeting` and `audience` first in *one* call of [print()](https://docs.python.org/3/library/functions.html#print)?\n", + "\n", + "Hint: The `*objects` in the documentation implies that we can put several *expressions* (i.e., variables) separated by commas within the same call of the [print()](https://docs.python.org/3/library/functions.html#print) function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(...)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q1.4**: When using a function, we may only put **expressions** between the parentheses `(` and `)`. What are expressions syntactically?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q1.5**: What does the `sep=\" \"` mean in the documentation on the built-in [print()](https://docs.python.org/3/library/functions.html#print) function? Adjust and use it to print out the three names referenced by `first`, `second`, and `third` on *one* line separated by *commas* with only *one* call of the [print()](https://docs.python.org/3/library/functions.html#print) function!" ] }, { @@ -88,7 +134,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q3**: Lastly, what does the `end=\"\\n\"` mean in the documentation? Use it in the `for`-loop and print the numbers 1 through 10 in just *one* line of output!" + "**Q1.6**: Lastly, what does the `end=\"\\n\"` mean in the documentation? Adjust and use it within the `for`-loop to print the numbers `1` through `10` on *one* line with only *one* call of the [print()](https://docs.python.org/3/library/functions.html#print) function!" ] }, { @@ -100,6 +146,286 @@ "for number in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n", " print(...)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python as a Calculator" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The [volume of a sphere](https://en.wikipedia.org/wiki/Sphere) is defined as $\\frac{4}{3} * \\pi * r^3$.\n", + "\n", + "**Q2.1**: Calculate it for `r = 2.88` and approximate $\\pi$ with `pi = 3.14`!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pi = 3.14\n", + "r = 2.88" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "While Python may be used as a calculator, it behaves a bit differently compared to calculator apps that phones or computers come with and that we are accustomed to.\n", + "\n", + "A major difference is that Python \"forgets\" intermediate results that are not assigned to variables. On the contrary, the calculators we work with outside of programming always keep the last result and allow us to use it as the first input for the next calculation.\n", + "\n", + "One way to keep on working with intermediate results in Python is to write the entire calculation as just *one* big expression that is composed of many sub-expressions representing the individual steps in our overall calculation.\n", + "\n", + "**Q2.2.1**: Given `a` and `b` like below, subtract the smaller `a` from the larger `b`, divide the difference by `9`, and raise the result to the power of `2`! Use operators that preserve the `int` type of the final result! The entire calculations *must* be placed within *one* code cell.\n", + "\n", + "Hint: You may need to group sub-expressions with parentheses `(` and `)`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = 42\n", + "b = 87" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The code cell below contains nothing but a single underscore `_`. In both, a Python command-line prompt and Jupyter notebooks, the variable `_` is automatically updated and always references the object to which the *last* expression executed evaluated to.\n", + "\n", + "**Q2.2.2**: Execute the code cell below! It should evaluate to the *same* result as the previous code cell (i.e., your answer to **Q2.2.1** assuming you go through this notebook in order)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "_" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q2.2.3**: Implement the same overall calculation as in your answer to **Q2.2.1** in several independent steps (i.e., code cells)! Use only *one* operator per code cell!\n", + "\n", + "Hint: You should need *two* more code cells after the `b - a` one immediately below. If you *need* to use parentheses, you must be doing something wrong." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "b - a" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "_ ..." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "_ ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Simple `for`-loops" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`for`-loops are extremely versatile in Python. That is different from many other programming languages.\n", + "\n", + "As shown in the first example in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_content.ipynb), we can create a `list` like `numbers` and loop over the numbers in it on a one-by-one basis." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q3.1**: Fill in the *condition* of the `if` statement such that only numbers divisible by `3` are printed! Adjust the call of the [print()](https://docs.python.org/3/library/functions.html#print) function such that the `for`-loop prints out all the numbers on *one* line of output!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for number in numbers:\n", + " if ...:\n", + " print(...)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Instead of looping over an *existing* object referenced by a variable like `numbers`, we may also create a *new* object within the `for` statement and loop over it directly. For example, below we write out the `list` object as a *literal*.\n", + "\n", + "Generically, the objects contained in a `list` objects are referred to as its **elements**. We reflect that in the name of the *target* variable `element` that is assigned a different number in every iteration of the `for`-loop. While we could use *any* syntactically valid name, it is best to choose one that makes sense in the context (e.g., `number` in `numbers`).\n", + "\n", + "**Q3.2**: Fill in the condition of the `if` statement such that only numbers consisting of *one* digit are printed out! As before, print out all the numbers on *one* line of output!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for element in [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]:\n", + " if ...:\n", + " print(...)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "An easy way to loop over a `list` object in a sorted manner, is to wrap it with the built-in [sorted()](https://docs.python.org/3/library/functions.html#sorted) function.\n", + "\n", + "**Q3.3**: Fill in the condition of the `if` statement such that only odd numbers are printed out! Put all the numbers on *one* line of output!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for number in sorted(numbers):\n", + " if ...:\n", + " print(...)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Whenever we want to loop over numbers representing a [series](https://en.wikipedia.org/wiki/Series_%28mathematics%29) in the mathematical sense (i.e., a rule to calculate the next number from its predecessor), we may be able to use the [range()](https://docs.python.org/3/library/functions.html#func-range) built-in.\n", + "\n", + "For example, to loop over the whole numbers from `0` to `9` (both including) in order, we could write them out in a `list` like below.\n", + "\n", + "**Q3.4**: Fill in the call to the [print()](https://docs.python.org/3/library/functions.html#print) function such that all the numbers are printed on *one* line ouf output!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for number in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:\n", + " print(...)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q3.5**: Read the documentation on the [range()](https://docs.python.org/3/library/functions.html#func-range) built-in! It may be used with either one, two, or three expressions \"passed\" in. What do `start`, `stop`, and `step` mean? Fill in the calls to [range()](https://docs.python.org/3/library/functions.html#func-range) and [print()](https://docs.python.org/3/library/functions.html#print) to mimic the output of **Q3.4**!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for number in range(...):\n", + " print(...)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q3.6**: Fill in the calls to [range()](https://docs.python.org/3/library/functions.html#func-range) and [print()](https://docs.python.org/3/library/functions.html#print) to print out *all* numbers from `1` to `10` (both including)!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for number in range(...):\n", + " print(...)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q3.7**: Fill in the calls to [range()](https://docs.python.org/3/library/functions.html#func-range) and [print()](https://docs.python.org/3/library/functions.html#print) to print out the *even* numbers from `1` to `10` (both including)! Do *not* use an `if` statement to accomplish this!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for number in range(...):\n", + " print(...)" + ] } ], "metadata": { @@ -118,7 +444,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "toc": { "base_numbering": 1, From 4b8aae1f28e9948848e81f7fb6b8744938282951 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sun, 8 Mar 2020 17:46:19 +0100 Subject: [PATCH 4/7] Streamline presentation --- 01_elements_01_review.ipynb | 2 +- 02_functions_00_lecture.ipynb | 360 ++++++++++++++++++-------------- 02_functions_01_review.ipynb | 2 +- 02_functions_02_exercises.ipynb | 4 +- 4 files changed, 211 insertions(+), 157 deletions(-) diff --git a/01_elements_01_review.ipynb b/01_elements_01_review.ipynb index 0ed455c..c17b487 100644 --- a/01_elements_01_review.ipynb +++ b/01_elements_01_review.ipynb @@ -207,7 +207,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "toc": { "base_numbering": 1, diff --git a/02_functions_00_lecture.ipynb b/02_functions_00_lecture.ipynb index 69170d8..c25b194 100644 --- a/02_functions_00_lecture.ipynb +++ b/02_functions_00_lecture.ipynb @@ -134,7 +134,7 @@ { "data": { "text/plain": [ - "140413081843264" + "140451963172416" ] }, "execution_count": 4, @@ -158,7 +158,7 @@ { "data": { "text/plain": [ - "140413081842224" + "140451963171376" ] }, "execution_count": 5, @@ -252,7 +252,7 @@ { "data": { "text/plain": [ - "140413081841824" + "140451963170976" ] }, "execution_count": 8, @@ -630,7 +630,7 @@ "execution_count": 20, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -672,7 +672,7 @@ { "data": { "text/plain": [ - "94916229764288" + "93979750270848" ] }, "execution_count": 21, @@ -696,7 +696,7 @@ { "data": { "text/plain": [ - "94916229768192" + "93979750256352" ] }, "execution_count": 22, @@ -955,7 +955,7 @@ { "data": { "text/plain": [ - "140413021591744" + "140451741882272" ] }, "execution_count": 29, @@ -972,7 +972,7 @@ "execution_count": 30, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -1721,7 +1721,7 @@ } ], "source": [ - "average_evens([40.0, 41.1, 42.2, 43.3, 44.4]) # inputs with predictable result" + "average_evens([40.0, 41.1, 42.2, 43.3, 44.4])" ] }, { @@ -1800,7 +1800,7 @@ "execution_count": 52, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -1961,7 +1961,7 @@ "execution_count": 57, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2042,7 +2042,7 @@ "execution_count": 59, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2343,7 +2343,7 @@ } }, "source": [ - "### Default Arguments" + "### Modularization" ] }, { @@ -2374,9 +2374,19 @@ " numbers = [round(n) for n in numbers]\n", " evens = [n for n in numbers if n % 2 == 0]\n", " average = sum(evens) / len(evens)\n", - " return average\n", - "\n", - "\n", + " return average" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "slideshow": { + "slide_type": "-" + } + }, + "outputs": [], + "source": [ "def scaled_average_evens(numbers, scalar):\n", " \"\"\" ... ... ... \"\"\"\n", " numbers = [round(n) for n in numbers]\n", @@ -2400,7 +2410,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 70, "metadata": { "slideshow": { "slide_type": "slide" @@ -2410,9 +2420,19 @@ "source": [ "def average_evens(numbers):\n", " \"\"\" ... ... ... \"\"\"\n", - " return scaled_average_evens(numbers, scalar=1)\n", - "\n", - "\n", + " return scaled_average_evens(numbers, scalar=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "slideshow": { + "slide_type": "-" + } + }, + "outputs": [], + "source": [ "def scaled_average_evens(numbers, scalar):\n", " \"\"\" ... ... ... \"\"\"\n", " numbers = [round(n) for n in numbers]\n", @@ -2434,7 +2454,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 72, "metadata": { "slideshow": { "slide_type": "skip" @@ -2447,7 +2467,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 73, "metadata": { "slideshow": { "slide_type": "skip" @@ -2458,6 +2478,17 @@ "assert scaled_average_evens(numbers, 2) == 14.0" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Default Arguments" + ] + }, { "cell_type": "markdown", "metadata": { @@ -2471,7 +2502,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 74, "metadata": { "slideshow": { "slide_type": "slide" @@ -2513,7 +2544,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 75, "metadata": { "slideshow": { "slide_type": "slide" @@ -2526,7 +2557,7 @@ "7.0" ] }, - "execution_count": 73, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -2537,10 +2568,10 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 76, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2550,7 +2581,7 @@ "14.0" ] }, - "execution_count": 74, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -2561,10 +2592,10 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 77, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2574,7 +2605,7 @@ "14.0" ] }, - "execution_count": 75, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -2609,7 +2640,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 78, "metadata": { "slideshow": { "slide_type": "slide" @@ -2647,7 +2678,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 79, "metadata": { "slideshow": { "slide_type": "slide" @@ -2660,7 +2691,7 @@ "7.0" ] }, - "execution_count": 77, + "execution_count": 79, "metadata": {}, "output_type": "execute_result" } @@ -2682,10 +2713,10 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 80, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2695,7 +2726,7 @@ "14.0" ] }, - "execution_count": 78, + "execution_count": 80, "metadata": {}, "output_type": "execute_result" } @@ -2717,7 +2748,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 81, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2731,7 +2762,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0maverage_evens\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0maverage_evens\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: average_evens() takes 1 positional argument but 2 were given" ] } @@ -2772,7 +2803,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 82, "metadata": { "slideshow": { "slide_type": "slide" @@ -2785,7 +2816,7 @@ "(x)>" ] }, - "execution_count": 80, + "execution_count": 82, "metadata": {}, "output_type": "execute_result" } @@ -2811,7 +2842,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 83, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2835,7 +2866,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 84, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2848,7 +2879,7 @@ "function" ] }, - "execution_count": 82, + "execution_count": 84, "metadata": {}, "output_type": "execute_result" } @@ -2859,10 +2890,10 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 85, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -2872,7 +2903,7 @@ "True" ] }, - "execution_count": 83, + "execution_count": 85, "metadata": {}, "output_type": "execute_result" } @@ -2894,7 +2925,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 86, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2907,7 +2938,7 @@ "42" ] }, - "execution_count": 84, + "execution_count": 86, "metadata": {}, "output_type": "execute_result" } @@ -2929,7 +2960,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 87, "metadata": { "slideshow": { "slide_type": "skip" @@ -2942,7 +2973,7 @@ "42" ] }, - "execution_count": 85, + "execution_count": 87, "metadata": {}, "output_type": "execute_result" } @@ -3046,7 +3077,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 88, "metadata": { "slideshow": { "slide_type": "slide" @@ -3070,7 +3101,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 89, "metadata": { "slideshow": { "slide_type": "fragment" @@ -3080,10 +3111,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 87, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } @@ -3094,7 +3125,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 90, "metadata": { "slideshow": { "slide_type": "fragment" @@ -3104,10 +3135,10 @@ { "data": { "text/plain": [ - "140413079419024" + "140451956463472" ] }, - "execution_count": 88, + "execution_count": 90, "metadata": {}, "output_type": "execute_result" } @@ -3118,10 +3149,10 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 91, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3131,7 +3162,7 @@ "module" ] }, - "execution_count": 89, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } @@ -3157,10 +3188,10 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 92, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "skip" } }, "outputs": [ @@ -3225,7 +3256,7 @@ " 'trunc']" ] }, - "execution_count": 90, + "execution_count": 92, "metadata": {}, "output_type": "execute_result" } @@ -3247,7 +3278,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 93, "metadata": { "slideshow": { "slide_type": "slide" @@ -3260,7 +3291,7 @@ "3.141592653589793" ] }, - "execution_count": 91, + "execution_count": 93, "metadata": {}, "output_type": "execute_result" } @@ -3271,10 +3302,10 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 94, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3284,7 +3315,7 @@ "2.718281828459045" ] }, - "execution_count": 92, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -3295,7 +3326,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 95, "metadata": { "slideshow": { "slide_type": "slide" @@ -3308,7 +3339,7 @@ "" ] }, - "execution_count": 93, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -3319,10 +3350,10 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 96, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3344,10 +3375,10 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 97, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "slide" } }, "outputs": [ @@ -3357,7 +3388,7 @@ "1.4142135623730951" ] }, - "execution_count": 95, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } @@ -3383,10 +3414,10 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 98, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3396,7 +3427,7 @@ "2.0" ] }, - "execution_count": 96, + "execution_count": 98, "metadata": {}, "output_type": "execute_result" } @@ -3418,10 +3449,10 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 99, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3431,7 +3462,7 @@ "10.0" ] }, - "execution_count": 97, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } @@ -3455,7 +3486,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 100, "metadata": { "slideshow": { "slide_type": "skip" @@ -3468,7 +3499,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 101, "metadata": { "slideshow": { "slide_type": "skip" @@ -3481,7 +3512,7 @@ "4.0" ] }, - "execution_count": 99, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -3514,7 +3545,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 102, "metadata": { "slideshow": { "slide_type": "slide" @@ -3527,20 +3558,20 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 103, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 101, + "execution_count": 103, "metadata": {}, "output_type": "execute_result" } @@ -3562,10 +3593,10 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 104, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "skip" } }, "outputs": [ @@ -3636,7 +3667,7 @@ " 'weibullvariate']" ] }, - "execution_count": 102, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -3658,7 +3689,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 105, "metadata": { "slideshow": { "slide_type": "slide" @@ -3671,7 +3702,7 @@ "" ] }, - "execution_count": 103, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } @@ -3682,10 +3713,10 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 106, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3707,20 +3738,20 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 107, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ - "0.8837282454584094" + "0.09696128509713109" ] }, - "execution_count": 105, + "execution_count": 107, "metadata": {}, "output_type": "execute_result" } @@ -3742,7 +3773,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 108, "metadata": { "slideshow": { "slide_type": "slide" @@ -3752,10 +3783,10 @@ { "data": { "text/plain": [ - ">" + ">" ] }, - "execution_count": 106, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } @@ -3766,10 +3797,10 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 109, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3791,20 +3822,20 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 110, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ - "9" + "3" ] }, - "execution_count": 108, + "execution_count": 110, "metadata": {}, "output_type": "execute_result" } @@ -3828,7 +3859,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 111, "metadata": { "slideshow": { "slide_type": "slide" @@ -3841,7 +3872,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 112, "metadata": { "slideshow": { "slide_type": "fragment" @@ -3854,7 +3885,7 @@ "0.6394267984578837" ] }, - "execution_count": 110, + "execution_count": 112, "metadata": {}, "output_type": "execute_result" } @@ -3865,10 +3896,10 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 113, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [], @@ -3878,10 +3909,10 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 114, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -3891,7 +3922,7 @@ "0.6394267984578837" ] }, - "execution_count": 112, + "execution_count": 114, "metadata": {}, "output_type": "execute_result" } @@ -3956,7 +3987,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 115, "metadata": { "slideshow": { "slide_type": "slide" @@ -3967,7 +3998,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Requirement already satisfied: numpy in /home/webartifex/.pyenv/versions/anaconda3-2019.10/lib/python3.7/site-packages (1.17.2)\n" + "Requirement already satisfied: numpy in /home/webartifex/.pyenv/versions/3.7.6/envs/v-ipp/lib/python3.7/site-packages (1.18.1)\n" ] } ], @@ -3988,7 +4019,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 116, "metadata": { "slideshow": { "slide_type": "slide" @@ -4012,7 +4043,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 117, "metadata": { "slideshow": { "slide_type": "fragment" @@ -4022,10 +4053,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 115, + "execution_count": 117, "metadata": {}, "output_type": "execute_result" } @@ -4047,7 +4078,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 118, "metadata": { "slideshow": { "slide_type": "slide" @@ -4060,7 +4091,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 119, "metadata": { "slideshow": { "slide_type": "fragment" @@ -4073,7 +4104,7 @@ "array([ 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4])" ] }, - "execution_count": 117, + "execution_count": 119, "metadata": {}, "output_type": "execute_result" } @@ -4084,10 +4115,10 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 120, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -4097,7 +4128,7 @@ "numpy.ndarray" ] }, - "execution_count": 118, + "execution_count": 120, "metadata": {}, "output_type": "execute_result" } @@ -4121,7 +4152,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 121, "metadata": { "slideshow": { "slide_type": "slide" @@ -4134,7 +4165,7 @@ "array([14, 22, 16, 10, 6, 24, 4, 12, 18, 20, 2, 8])" ] }, - "execution_count": 119, + "execution_count": 121, "metadata": {}, "output_type": "execute_result" } @@ -4156,7 +4187,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 122, "metadata": { "slideshow": { "slide_type": "fragment" @@ -4169,7 +4200,7 @@ "[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4, 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]" ] }, - "execution_count": 120, + "execution_count": 122, "metadata": {}, "output_type": "execute_result" } @@ -4191,7 +4222,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 123, "metadata": { "slideshow": { "slide_type": "fragment" @@ -4204,7 +4235,7 @@ "78" ] }, - "execution_count": 121, + "execution_count": 123, "metadata": {}, "output_type": "execute_result" } @@ -4215,10 +4246,10 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 124, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -4228,7 +4259,7 @@ "7" ] }, - "execution_count": 122, + "execution_count": 124, "metadata": {}, "output_type": "execute_result" } @@ -4267,7 +4298,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 125, "metadata": { "slideshow": { "slide_type": "slide" @@ -4280,7 +4311,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 126, "metadata": { "slideshow": { "slide_type": "fragment" @@ -4293,7 +4324,7 @@ "" ] }, - "execution_count": 124, + "execution_count": 126, "metadata": {}, "output_type": "execute_result" } @@ -4319,10 +4350,10 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 127, "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "skip" } }, "outputs": [ @@ -4344,7 +4375,7 @@ " 'average_odds']" ] }, - "execution_count": 125, + "execution_count": 127, "metadata": {}, "output_type": "execute_result" } @@ -4366,12 +4397,36 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 128, "metadata": { "slideshow": { "slide_type": "slide" } }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mod.average_evens" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, "outputs": [ { "name": "stdout", @@ -4399,7 +4454,7 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 130, "metadata": { "slideshow": { "slide_type": "slide" @@ -4412,7 +4467,7 @@ "7.0" ] }, - "execution_count": 127, + "execution_count": 130, "metadata": {}, "output_type": "execute_result" } @@ -4423,10 +4478,10 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 131, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -4436,7 +4491,7 @@ "14.0" ] }, - "execution_count": 128, + "execution_count": 131, "metadata": {}, "output_type": "execute_result" } @@ -4501,7 +4556,6 @@ } ], "metadata": { - "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "language": "python", @@ -4517,7 +4571,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "livereveal": { "auto_select": "code", diff --git a/02_functions_01_review.ipynb b/02_functions_01_review.ipynb index f081ec6..3b58b22 100644 --- a/02_functions_01_review.ipynb +++ b/02_functions_01_review.ipynb @@ -220,7 +220,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "toc": { "base_numbering": 1, diff --git a/02_functions_02_exercises.ipynb b/02_functions_02_exercises.ipynb index 00cd198..cc0accd 100644 --- a/02_functions_02_exercises.ipynb +++ b/02_functions_02_exercises.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Read [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb) of the book. Then, work through the exercises below." + "Read [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells." ] }, { @@ -213,7 +213,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.6" }, "toc": { "base_numbering": 1, From 8dea642ce98ca70814f5e80830b10f3439b2ae06 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 10 Mar 2020 15:27:49 +0100 Subject: [PATCH 5/7] Streamline presentation --- 04_iteration_00_lecture.ipynb | 694 ++++++++++++++++---------------- 04_iteration_02_exercises.ipynb | 76 ++-- 2 files changed, 388 insertions(+), 382 deletions(-) diff --git a/04_iteration_00_lecture.ipynb b/04_iteration_00_lecture.ipynb index 0c95c8a..1a72c7b 100644 --- a/04_iteration_00_lecture.ipynb +++ b/04_iteration_00_lecture.ipynb @@ -562,7 +562,7 @@ "execution_count": 15, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -597,7 +597,7 @@ "execution_count": 16, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -613,7 +613,7 @@ } ], "source": [ - "gcd(2, 7919)" + "gcd(7, 7919)" ] }, { @@ -721,7 +721,7 @@ } ], "source": [ - "math.gcd(2, 7919)" + "math.gcd(7, 7919)" ] }, { @@ -859,7 +859,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "49.8 µs ± 20.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" + "37.3 µs ± 1.25 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], @@ -873,7 +873,7 @@ "execution_count": 24, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -881,13 +881,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "1.63 ms ± 14.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" + "11.4 ms ± 144 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%%timeit -n 100\n", - "fibonacci(20)" + "fibonacci(24)" ] }, { @@ -895,7 +895,7 @@ "execution_count": 25, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -903,40 +903,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "202 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" + "3.63 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" ] } ], "source": [ "%%timeit -n 1 -r 1\n", - "fibonacci(30)" + "fibonacci(36)" ] }, { "cell_type": "code", "execution_count": 26, - "metadata": { - "slideshow": { - "slide_type": "-" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.25 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" - ] - } - ], - "source": [ - "%%timeit -n 1 -r 1\n", - "fibonacci(35)" - ] - }, - { - "cell_type": "code", - "execution_count": 27, "metadata": { "slideshow": { "slide_type": "skip" @@ -947,7 +925,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "5.79 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" + "5.93 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" ] } ], @@ -982,7 +960,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 27, "metadata": { "slideshow": { "slide_type": "slide" @@ -997,7 +975,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 28, "metadata": { "slideshow": { "slide_type": "slide" @@ -1011,10 +989,10 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mrun_forever\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\"\"\"Also a pointless function should have a docstring.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mrun_forever\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\"\"\"Also a pointless function should have a docstring.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "... last 1 frames repeated, from the frame below ...\n", - "\u001b[0;32m\u001b[0m in \u001b[0;36mrun_forever\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\"\"\"Also a pointless function should have a docstring.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mrun_forever\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\"\"\"Also a pointless function should have a docstring.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mrun_forever\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mRecursionError\u001b[0m: maximum recursion depth exceeded" ] } @@ -1036,7 +1014,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 29, "metadata": { "slideshow": { "slide_type": "slide" @@ -4010,7 +3988,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcountdown\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcountdown\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mcountdown\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mcountdown\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "... last 1 frames repeated, from the frame below ...\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mcountdown\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mcountdown\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", @@ -4035,7 +4013,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 30, "metadata": { "slideshow": { "slide_type": "slide" @@ -4049,7 +4027,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mfactorial\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# = base case\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "... last 1 frames repeated, from the frame below ...\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mfactorial\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# = base case\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", @@ -4118,7 +4096,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 31, "metadata": { "slideshow": { "slide_type": "slide" @@ -4161,7 +4139,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 32, "metadata": { "slideshow": { "slide_type": "slide" @@ -4174,7 +4152,7 @@ "1" ] }, - "execution_count": 33, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -4185,26 +4163,26 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 33, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ - "3628800" + "6" ] }, - "execution_count": 34, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "factorial(10)" + "factorial(3)" ] }, { @@ -4220,7 +4198,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 34, "metadata": { "slideshow": { "slide_type": "slide" @@ -4234,8 +4212,8 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mfactorial\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 13\u001b[0m \"\"\"\n\u001b[1;32m 14\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is only defined for integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is not defined for negative integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mfactorial\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 13\u001b[0m \"\"\"\n\u001b[1;32m 14\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is only defined for integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is not defined for negative integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: Factorial is only defined for integers" ] } @@ -4246,7 +4224,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 35, "metadata": { "slideshow": { "slide_type": "slide" @@ -4260,8 +4238,8 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m42\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mfactorial\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is only defined for integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is not defined for negative integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# = base case\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfactorial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m42\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mfactorial\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is only defined for integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Factorial is not defined for negative integers\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# = base case\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: Factorial is not defined for negative integers" ] } @@ -4353,7 +4331,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 36, "metadata": { "code_folding": [], "slideshow": { @@ -4381,12 +4359,12 @@ " print(n)\n", " n -= 1\n", "\n", - " print(\"Happy new Year!\") # = base case" + " print(\"Happy new Year!\")" ] }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 37, "metadata": { "slideshow": { "slide_type": "slide" @@ -4445,7 +4423,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 38, "metadata": { "code_folding": [], "slideshow": { @@ -4484,7 +4462,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 39, "metadata": { "slideshow": { "slide_type": "slide" @@ -4497,7 +4475,7 @@ "4" ] }, - "execution_count": 40, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -4508,10 +4486,10 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 40, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -4521,13 +4499,13 @@ "1" ] }, - "execution_count": 41, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "gcd(2, 7919)" + "gcd(7, 7919)" ] }, { @@ -4554,7 +4532,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 41, "metadata": { "slideshow": { "slide_type": "slide" @@ -4565,7 +4543,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "5.14 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" + "5.24 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" ] } ], @@ -4637,7 +4615,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 42, "metadata": { "code_folding": [], "slideshow": { @@ -4669,11 +4647,11 @@ " while n != 1:\n", " print(n, end=\" \")\n", " if n % 2 == 0:\n", - " n //= 2 # //= instead of /= so that n remains an int\n", + " n //= 2 # //= to preserve the int type\n", " else:\n", " n = 3 * n + 1\n", "\n", - " print(n) # = base case" + " print(1)" ] }, { @@ -4689,7 +4667,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 43, "metadata": { "slideshow": { "slide_type": "slide" @@ -4710,10 +4688,10 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 44, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -4731,10 +4709,10 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 45, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -4752,7 +4730,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 46, "metadata": { "slideshow": { "slide_type": "skip" @@ -4799,7 +4777,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 47, "metadata": { "slideshow": { "slide_type": "slide" @@ -4812,10 +4790,10 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 48, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -4829,10 +4807,12 @@ ], "source": [ "index = 0\n", + "\n", "while index < len(elements):\n", " element = elements[index]\n", " print(element, end=\" \")\n", " index += 1\n", + "\n", "del index" ] }, @@ -4849,7 +4829,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 49, "metadata": { "slideshow": { "slide_type": "fragment" @@ -4882,10 +4862,10 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 50, "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "slide" } }, "outputs": [ @@ -4904,7 +4884,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 51, "metadata": { "slideshow": { "slide_type": "fragment" @@ -4917,7 +4897,7 @@ "range" ] }, - "execution_count": 52, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -4939,7 +4919,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 52, "metadata": { "slideshow": { "slide_type": "slide" @@ -4961,7 +4941,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 53, "metadata": { "slideshow": { "slide_type": "fragment" @@ -5019,7 +4999,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 54, "metadata": { "slideshow": { "slide_type": "slide" @@ -5043,7 +5023,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 55, "metadata": { "slideshow": { "slide_type": "fragment" @@ -5056,7 +5036,7 @@ "True" ] }, - "execution_count": 56, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -5067,10 +5047,10 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 56, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -5080,7 +5060,7 @@ "False" ] }, - "execution_count": 57, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -5102,7 +5082,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 57, "metadata": { "slideshow": { "slide_type": "skip" @@ -5115,7 +5095,7 @@ "[0, 1, 2, 3, 4]" ] }, - "execution_count": 58, + "execution_count": 57, "metadata": {}, "output_type": "execute_result" } @@ -5126,7 +5106,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 58, "metadata": { "slideshow": { "slide_type": "skip" @@ -5139,7 +5119,7 @@ "True" ] }, - "execution_count": 59, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -5161,7 +5141,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 59, "metadata": { "slideshow": { "slide_type": "slide" @@ -5194,7 +5174,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 60, "metadata": { "slideshow": { "slide_type": "fragment" @@ -5205,13 +5185,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "0 > Achim 1 > Berthold 2 > Carl 3 > Diedrich 4 > Eckardt " + "1 > Achim 2 > Berthold 3 > Carl 4 > Diedrich 5 > Eckardt " ] } ], "source": [ - "for i, name in enumerate(first_names):\n", - " print(i, name, sep=\" > \", end=\" \")" + "for i, name in enumerate(first_names, start=1):\n", + " print(i, \">\", name, end=\" \")" ] }, { @@ -5227,7 +5207,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 61, "metadata": { "slideshow": { "slide_type": "skip" @@ -5244,7 +5224,7 @@ ], "source": [ "for i, name in enumerate(first_names, start=1):\n", - " print(i, name, sep=\" > \", end=\" \")" + " print(i, \">\", name, end=\" \")" ] }, { @@ -5260,7 +5240,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 62, "metadata": { "slideshow": { "slide_type": "skip" @@ -5273,7 +5253,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 63, "metadata": { "slideshow": { "slide_type": "skip" @@ -5321,7 +5301,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 64, "metadata": { "code_folding": [], "slideshow": { @@ -5351,7 +5331,7 @@ " a = 0\n", " b = 1\n", " print(a, b, sep=\" \", end=\" \") # added for didactical purposes\n", - " for _ in range(i - 1): # the index variable is not needed\n", + " for _ in range(i - 1):\n", " temp = a + b\n", " a = b\n", " b = temp\n", @@ -5362,7 +5342,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 65, "metadata": { "slideshow": { "slide_type": "slide" @@ -5382,7 +5362,7 @@ "144" ] }, - "execution_count": 66, + "execution_count": 65, "metadata": {}, "output_type": "execute_result" } @@ -5415,7 +5395,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 66, "metadata": { "slideshow": { "slide_type": "slide" @@ -5435,7 +5415,7 @@ "218922995834555169026" ] }, - "execution_count": 67, + "execution_count": 66, "metadata": {}, "output_type": "execute_result" } @@ -5468,7 +5448,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 67, "metadata": { "slideshow": { "slide_type": "slide" @@ -5495,7 +5475,7 @@ " raise ValueError(\"Factorial is not defined for negative integers\")\n", "\n", " product = 1 # because 0! = 1\n", - " for i in range(1, n + 1): # loop starts at 1 as 0! is already covered\n", + " for i in range(1, n + 1):\n", " product *= i\n", " print(product, end=\" \") # added for didactical purposes\n", "\n", @@ -5504,12 +5484,43 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 68, "metadata": { "slideshow": { "slide_type": "slide" } }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 6 " + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, "outputs": [ { "name": "stdout", @@ -5541,7 +5552,7 @@ } }, "source": [ - "### The `break` Statement" + "### Customizing `for`- and `while`-loops" ] }, { @@ -5552,7 +5563,7 @@ } }, "source": [ - "The remainder of this chapter introduces more syntactic sugar: None of the language constructs introduced below are needed but contribute to making Python the expressive and easy to read language that it is." + "This section introduces additional syntax to customize `for` and `while` statements in our code even further. They are mostly syntactic sugar in that they do not change how a program runs but make its code more readable. We illustrate them for the `for` statement only. However, everything presented in this section also works for the `while` statement." ] }, { @@ -5563,7 +5574,7 @@ } }, "source": [ - "#### Example: Searching an Iterable" + "#### Example: Is the square of a number in `[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]` greater than `100`?" ] }, { @@ -5574,7 +5585,7 @@ } }, "source": [ - "Let's say we have a list of `numbers` and want to check if the square of at least one of its elements is above a `threshold` of `100`." + "Let's say we have a list of `numbers` and want to check if the square of at least one of its elements is greater than `100`. So, conceptually, we are asking the question if a list of numbers as a whole satisfies a certain condition." ] }, { @@ -5590,19 +5601,6 @@ "numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]" ] }, - { - "cell_type": "code", - "execution_count": 71, - "metadata": { - "slideshow": { - "slide_type": "-" - } - }, - "outputs": [], - "source": [ - "threshold = 100 # is the square of an element in numbers greater than this?" - ] - }, { "cell_type": "markdown", "metadata": { @@ -5611,12 +5609,16 @@ } }, "source": [ - "A first naive implementation could look like this: We loop over *every* element in `numbers` and set an **indicator variable** `is_above`, initialized as `False`, to `True` once we encounter an element satisfying the search condition." + "A first naive implementation could look like this: We loop over *every* element in `numbers` and set an **indicator variable** `is_above`, initialized as `False`, to `True` once we encounter an element satisfying the condition.\n", + "\n", + "This implementation is *inefficient* as even if the *first* element in `numbers` has a square greater than `100`, we loop until the last element: This could take a long time for a big list.\n", + "\n", + "Moreover, we must initialize `is_above` *before* the `for`-loop and write an `if`-`else`-logic *after* it to check for the result. The actual business logic is *not* conveyed in a clear way." ] }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 71, "metadata": { "slideshow": { "slide_type": "fragment" @@ -5627,7 +5629,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "7 11 8 5 3 12 2 6 9 10 1 4 => at least one number's square is above 100\n" + "7 11 8 5 3 12 2 6 9 10 1 4 => at least one number satisfies the condition\n" ] } ], @@ -5636,13 +5638,24 @@ "\n", "for number in numbers:\n", " print(number, end=\" \") # added for didactical purposes\n", - " if number ** 2 > threshold:\n", + " if number ** 2 > 100:\n", " is_above = True\n", "\n", "if is_above:\n", - " print(\"=> at least one number's square is above\", threshold)\n", + " print(\"=> at least one number satisfies the condition\")\n", "else:\n", - " print(\"=> no number's square is above\", threshold)" + " print(\"=> no number satisfies the condition\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### The `break` Statement" ] }, { @@ -5653,11 +5666,74 @@ } }, "source": [ - "This implementation is *inefficient* as even if the *first* element in `numbers` has a square greater than `100`, we loop until the last element: This could take a long time for a big list.\n", + "Python provides the `break` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-break-statement)) that lets us stop a loop prematurely at any iteration. It is yet another means of controlling the flow of execution, and we say that we \"break out of a loop.\"" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7 11 => at least one number satisfies the condition\n" + ] + } + ], + "source": [ + "is_above = False\n", "\n", - "Moreover, we must initialize `is_above` *before* the `for`-loop and write an `if`-`else`-logic *after* it to check for the result. The actual business logic is *not* clear right away.\n", + "for number in numbers:\n", + " print(number, end=\" \") # added for didactical purposes\n", + " if number ** 2 > 100:\n", + " is_above = True\n", + " break\n", "\n", - "Luckily, Python provides the `break` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-break-statement)) that lets us stop the `for`-loop in any iteration of the loop. Conceptually, it is yet another means of controlling the flow of execution." + "if is_above:\n", + " print(\"=> at least one number satisfies the condition\")\n", + "else:\n", + " print(\"=> no number satisfies the condition\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "This is a computational improvement. However, the code still consists of *three* sections: Some initialization *before* the `for`-loop, the loop itself, and some finalizing logic. We prefer to convey the program's idea in *one* compound statement instead." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### The `else`-clause" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "To express the logic in a prettier way, we add an `else`-clause at the end of the `for`-loop (cf., [reference](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)). The `else`-clause is executed *only if* the `for`-loop is *not* stopped with a `break` statement *prematurely* (i.e., *before* reaching the *last* iteration in the loop). The word \"else\" implies a somewhat unintuitive meaning and may have better been named a `then`-clause. In most use cases, however, the `else`-clause logically goes together with some `if` statement in the loop's body.\n", + "\n", + "Overall, the code's expressive power increases. Not many programming languages support an optional `else`-branching for the `for` and `while` statements, which turns out to be very useful in practice." ] }, { @@ -5673,101 +5749,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "7 11 => at least one number's square is above 100\n" - ] - } - ], - "source": [ - "is_above = False\n", - "\n", - "for number in numbers:\n", - " print(number, end=\" \") # added for didactical purposes\n", - " if number ** 2 > threshold:\n", - " is_above = True\n", - " break\n", - "\n", - "if is_above:\n", - " print(\"=> at least one number's square is above\", threshold)\n", - "else:\n", - " print(\"=> no number's square is above\", threshold)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "This is a computational improvement. However, the code still consists of *three* logical sections: Some initialization *before* the `for`-loop, the loop itself, and some finalizing logic. We prefer to convey the program's idea in *one* compound statement instead." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "### The `for`-`else`-Clause" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "To express the logic in a prettier way, we add an `else`-clause at the end of the `for`-loop (cf., [reference](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)). The `else`-clause is executed *iff* the `for`-loop is *not* stopped with a `break` statement **prematurely** (i.e., *before* reaching the *last* iteration in the loop). The word \"else\" implies a somewhat unintuitive meaning and had better been named a `then`-clause. In most scenarios, however, the `else`-clause logically goes together with some `if` statement in the `for`-loop's body.\n", - "\n", - "Overall, the code's expressive power increases. Not many programming languages support a `for`-`else`-branching, which turns out to be very useful in practice." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "#### Example: Searching an Iterable (revisited)" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 11 => at least one number's square is above 100\n" + "7 11 => at least one number satisfies the condition\n" ] } ], "source": [ "for number in numbers:\n", " print(number, end=\" \") # added for didactical purposes\n", - " if number ** 2 > threshold:\n", + " if number ** 2 > 100:\n", " is_above = True\n", " break\n", "else:\n", " is_above = False\n", "\n", "if is_above:\n", - " print(\"=> at least one number's square is above\", threshold)\n", + " print(\"=> at least one number satisfies the condition\")\n", "else:\n", - " print(\"=> no number's square is above\", threshold)" + " print(\"=> no number satisfies the condition\")" ] }, { @@ -5783,7 +5781,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 74, "metadata": { "slideshow": { "slide_type": "slide" @@ -5794,18 +5792,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "7 11 => at least one number's square is above 100\n" + "7 11 => at least one number satisfies the condition\n" ] } ], "source": [ "for number in numbers:\n", " print(number, end=\" \") # added for didactical purposes\n", - " if number ** 2 > threshold:\n", - " print(\"=> at least one number's square is above\", threshold)\n", + " if number ** 2 > 100:\n", + " print(\"=> at least one number satisfies the condition\")\n", " break\n", "else:\n", - " print(\"=> no number's square is above\", threshold)" + " print(\"=> no number satisfies the condition\")" ] }, { @@ -5816,12 +5814,12 @@ } }, "source": [ - "Of course, if we set the `threshold` an element's square has to pass higher, for example, to `200`, we have to loop over all `numbers`. There is *no way* to optimize this **[linear search](https://en.wikipedia.org/wiki/Linear_search)** further, at least as long as we model `numbers` as a `list` object. More advanced data types, however, exist that mitigate that downside." + "Of course, if we choose the number an element's square has to pass to be larger, for example, to `200`, we have to loop over all `numbers`. There is *no way* to optimize this **[linear search](https://en.wikipedia.org/wiki/Linear_search)** further." ] }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 75, "metadata": { "slideshow": { "slide_type": "skip" @@ -5832,31 +5830,29 @@ "name": "stdout", "output_type": "stream", "text": [ - "7 11 8 5 3 12 2 6 9 10 1 4 => no number's square is above 200\n" + "7 11 8 5 3 12 2 6 9 10 1 4 => no number satisfies the condition\n" ] } ], "source": [ - "threshold = 200\n", - "\n", "for number in numbers:\n", " print(number, end=\" \") # added for didactical purposes\n", - " if number ** 2 > threshold:\n", - " print(\"=> at least one number's square is above\", threshold)\n", + " if number ** 2 > 200:\n", + " print(\"=> at least one number satisfies the condition\")\n", " break\n", "else:\n", - " print(\"=> no number's square is above\", threshold)" + " print(\"=> no number satisfies the condition\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "skip" } }, "source": [ - "### The `continue` Statement" + "#### A first Glance at the Map-Filter-Reduce Paradigm" ] }, { @@ -5877,7 +5873,7 @@ "\n", "We study this **map-filter-reduce** paradigm extensively in [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_00_lecture.ipynb#The-Map-Filter-Reduce-Paradigm) after introducing more advanced data types that are needed to work with \"big\" data.\n", "\n", - "Here, we focus on *filtering out* some numbers with a `for`-loop." + "Here, we focus on *filtering out* some numbers in a `for`-loop." ] }, { @@ -5909,7 +5905,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 76, "metadata": { "slideshow": { "slide_type": "slide" @@ -5922,10 +5918,10 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 77, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [ @@ -5933,7 +5929,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "8 > 65 12 > 145 2 > 5 6 > 37 10 > 101 4 > 17 " + "8 -> 65 12 -> 145 2 -> 5 6 -> 37 10 -> 101 4 -> 17 " ] }, { @@ -5942,7 +5938,7 @@ "370" ] }, - "execution_count": 78, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -5953,7 +5949,7 @@ "for number in numbers:\n", " if number % 2 == 0: # only keep even numbers\n", " square = (number ** 2) + 1\n", - " print(number, square, sep=\" > \", end=\" \") # added for didactical purposes\n", + " print(number, \"->\", square, end=\" \") # added for didactical purposes\n", " total += square\n", "\n", "total" @@ -5982,7 +5978,7 @@ } }, "source": [ - "#### Example: Nested Filters" + "#### Example: Several Filters" ] }, { @@ -6004,31 +6000,20 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 78, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [ - { - "data": { - "text/plain": [ - "[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]" - ] - }, - "execution_count": 79, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "numbers" + "numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]" ] }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 79, "metadata": { "slideshow": { "slide_type": "-" @@ -6039,7 +6024,84 @@ "name": "stdout", "output_type": "stream", "text": [ - "8 > 65 12 > 145 4 > 17 " + "8 -> 65 12 -> 145 4 -> 17 " + ] + }, + { + "data": { + "text/plain": [ + "227" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total = 0\n", + "\n", + "for i, number in enumerate(numbers, start=1):\n", + " if i % 3 == 0: # only keep every third number\n", + " if number % 2 == 0: # only keep even numbers\n", + " square = (number ** 2) + 1\n", + " print(number, \"->\", square, end=\" \") # added for didactical purposes \n", + " total += square\n", + "\n", + "total" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "With already three levels of indentation, less horizontal space is available for the actual code block. Of course, one could flatten the two `if` statements with the logical `and` operator, as shown in [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb#The-if-Statement). Then, however, we trade off horizontal space against a more \"complex\" `if` logic, and this is *not* a real improvement." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "#### The `continue` Statement" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "A Pythonista would instead make use of the `continue` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-continue-statement)) that causes a loop to jump into the next iteration skipping the rest of the code block.\n", + "\n", + "The revised code fragment below occupies more vertical space and less horizontal space: A *good* trade-off.\n", + "\n", + "One caveat is that we need to negate the conditions in the `if` statements. Conceptually, we are now filtering \"out\" and not \"in.\"" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8 -> 65 12 -> 145 4 -> 17 " ] }, { @@ -6053,72 +6115,6 @@ "output_type": "execute_result" } ], - "source": [ - "total = 0\n", - "\n", - "for i, number in enumerate(numbers, start=1):\n", - " if i % 3 == 0: # only keep every third number\n", - " if number % 2 == 0: # only keep even numbers\n", - " square = (number ** 2) + 1\n", - " print(number, square, sep=\" > \", end=\" \") # added for didactical purposes \n", - " total += square\n", - "\n", - "total" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "With already three levels of indentation, less horizontal space is available for the actual code block. Of course, one could flatten the two `if` statements with the logical `and` operator, as shown in [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb#The-if-Statement). Then, however, we trade off horizontal space against a more \"complex\" `if` logic, and this is *not* a real improvement.\n", - "\n", - "A Pythonista would instead make use of the `continue` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-continue-statement)) that causes a loop to jump into the next iteration skipping the rest of the code block.\n", - "\n", - "The revised code fragment below occupies more vertical space and less horizontal space: A *good* trade-off." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "skip" - } - }, - "source": [ - "#### Example: Nested Filters (revisited)" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8 > 65 12 > 145 4 > 17 " - ] - }, - { - "data": { - "text/plain": [ - "227" - ] - }, - "execution_count": 81, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ "total = 0\n", "\n", @@ -6129,7 +6125,7 @@ " continue\n", "\n", " square = (number ** 2) + 1\n", - " print(number, square, sep=\" > \", end=\" \") # added for didactical purposes \n", + " print(number, \"->\", square, end=\" \") # added for didactical purposes \n", " total += square\n", "\n", "total" @@ -6143,11 +6139,7 @@ } }, "source": [ - "This is yet another illustration of why programming is an art. The two preceding code cells do the *same* with *identical* time complexity. However, the latter is arguably easier to read for a human, even more so when the business logic grows beyond two nested filters.\n", - "\n", - "The idea behind the `continue` statement is conceptually similar to the early exit pattern we saw above.\n", - "\n", - "Both the `break` and `continue` statements, as well as the optional `else`-clause, are not only supported with `for`-loops but also `while`-loops." + "This is yet another illustration of why programming is an art. The two preceding code cells do the *same* with *identical* time complexity. However, the latter is arguably easier to read for a human, even more so when the business logic grows beyond two filters." ] }, { @@ -6202,7 +6194,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 81, "metadata": { "slideshow": { "slide_type": "slide" @@ -6215,10 +6207,10 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 82, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "fragment" } }, "outputs": [], @@ -6228,7 +6220,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 83, "metadata": { "code_folding": [], "slideshow": { @@ -6240,7 +6232,7 @@ "name": "stdin", "output_type": "stream", "text": [ - "Guess if the coin comes up as heads or tails: Tails\n" + "Guess if the coin comes up as heads or tails: Heads\n" ] }, { @@ -6250,6 +6242,20 @@ "Ooops, it was tails\n" ] }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Guess if the coin comes up as heads or tails: Heads\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ooops, it was heads\n" + ] + }, { "name": "stdin", "output_type": "stream", @@ -6325,7 +6331,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 84, "metadata": { "slideshow": { "slide_type": "slide" @@ -6362,7 +6368,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 85, "metadata": { "slideshow": { "slide_type": "slide" @@ -6400,7 +6406,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 86, "metadata": { "slideshow": { "slide_type": "skip" @@ -6413,7 +6419,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 87, "metadata": { "slideshow": { "slide_type": "slide" diff --git a/04_iteration_02_exercises.ipynb b/04_iteration_02_exercises.ipynb index 5858e2d..ab80178 100644 --- a/04_iteration_02_exercises.ipynb +++ b/04_iteration_02_exercises.ipynb @@ -19,7 +19,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the exercises below." + "Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells. Occasionally, the `...` mean that you have to write more than one line of code." ] }, { @@ -88,7 +88,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.1**: Explain for the $n = 3$ case why it can be solved as a **recursion**!" + "**Q1**: Explain for the $n = 3$ case why it can be solved as a **recursion**!" ] }, { @@ -102,7 +102,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.2**: How does the number of minimal moves needed to solve a problem with three spots and $n$ disks grow as a function of $n$? How does this relate to the answer to **Q1.1**?" + "**Q2**: How does the number of minimal moves needed to solve a problem with three spots and $n$ disks grow as a function of $n$? How does this relate to the answer to **Q1**?" ] }, { @@ -116,7 +116,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.3**: The **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem is of **exponential growth**. What does that mean? What does that imply for large $n$?" + "**Q3**: The **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem is of **exponential growth**. What does that mean? What does that imply for large $n$?" ] }, { @@ -130,7 +130,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.4**: The video introduces the recursive relationship $Sol(4, 1, 3) = Sol(3, 1, 2) ~ \\bigoplus ~ Sol(1, 1, 3) ~ \\bigoplus ~ Sol(3, 2, 3)$. The $\\bigoplus$ is to be interpreted as some sort of \"plus\" operation. How does this \"plus\" operation work? How does this way of expressing the problem relate to the answer to **Q1.1**?" + "**Q4**: The video introduces the recursive relationship $Sol(4, 1, 3) = Sol(3, 1, 2) ~ \\bigoplus ~ Sol(1, 1, 3) ~ \\bigoplus ~ Sol(3, 2, 3)$. The $\\bigoplus$ is to be interpreted as some sort of \"plus\" operation. How does this \"plus\" operation work? How does this way of expressing the problem relate to the answer to **Q1**?" ] }, { @@ -197,7 +197,7 @@ "\n", "Once completed, `sol()` should print out all the moves in the correct order. With **printing a move**, we mean a line like \"1 -> 3\", short for \"Move the top-most disk from spot 1 to spot 3\".\n", "\n", - "Write your answers to **Q1.5** to **Q1.7** into the subsequent code cell and finalize `sol()`! No need to write a docstring or validate the input here." + "Write your answers to **Q5** to **Q7** into the subsequent code cell and finalize `sol()`! No need to write a docstring or validate the input here." ] }, { @@ -208,13 +208,13 @@ "source": [ "def sol(disks, origin, destination):\n", "\n", - " # answer to Q15.5\n", + " # answer to Q5\n", " # ...\n", "\n", - " # answer to Q15.6\n", + " # answer to Q6\n", " # ...\n", "\n", - " # answer to Q15.7\n", + " # answer to Q7\n", " # ..." ] }, @@ -222,14 +222,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.5**: What is the `disks` argument when the function reaches its **base case**? Check for the base case with a simple `if` statement and return from the function using the **early exit** pattern!" + "**Q5**: What is the `disks` argument when the function reaches its **base case**? Check for the base case with a simple `if` statement and return from the function using the **early exit** pattern!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.6**: If not in the base case, `sol()` needs to determine the `intermediate` spot given concrete `origin` and `destination` arguments. For example, if called with `origin=1` and `destination=2`, `intermediate` must be `3`.\n", + "**Q6**: If not in the base case, `sol()` needs to determine the `intermediate` spot given concrete `origin` and `destination` arguments. For example, if called with `origin=1` and `destination=2`, `intermediate` must be `3`.\n", "\n", "Add **one** compound `if` statement to `sol()` that has a branch for **every** possible `origin`-`destination` pair that sets a variable `intermediate` to the correct temporary spot. **How many** branches will there be?" ] @@ -238,7 +238,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.7**: `sol()` needs to call itself **two more times** with the correct 2-pairs chosen from the three available spots `origin`, `intermediate`, and `destination`.\n", + "**Q7**: `sol()` needs to call itself **two more times** with the correct 2-pairs chosen from the three available spots `origin`, `intermediate`, and `destination`.\n", "\n", "In between the two recursive function calls, write a `print()` statement that prints out from where to where the \"remaining and largest\" disk has to be moved!" ] @@ -247,7 +247,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.8**: Execute the code cells below and confirm that the printed moves are correct!" + "**Q8**: Execute the code cells below and confirm that the printed moves are correct!" ] }, { @@ -301,7 +301,7 @@ "\n", "Let's create a more concise `hanoi()` function that, in addition to a positional `disks` argument, takes three keyword-only arguments `origin`, `intermediate`, and `destination` with default values `\"left\"`, `\"center\"`, and `\"right\"`.\n", "\n", - "Write your answers to **Q1.9** and **Q1.10** into the subsequent code cell and finalize `hanoi()`! No need to write a docstring or validate the input here." + "Write your answers to **Q9** and **Q10** into the subsequent code cell and finalize `hanoi()`! No need to write a docstring or validate the input here." ] }, { @@ -312,10 +312,10 @@ "source": [ "def hanoi(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\"):\n", "\n", - " # answer to Q15.9\n", + " # answer to Q9\n", " # ...\n", "\n", - " # answer to Q15.10\n", + " # answer to Q10\n", " # ..." ] }, @@ -323,14 +323,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.9**: Copy the base case from `sol()`!" + "**Q9**: Copy the base case from `sol()`!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.10**: Instead of conditional logic, `hanoi()` calls itself **two times** with the **three** arguments `origin`, `intermediate`, and `destination` passed on in a **different** order.\n", + "**Q10**: Instead of conditional logic, `hanoi()` calls itself **two times** with the **three** arguments `origin`, `intermediate`, and `destination` passed on in a **different** order.\n", "\n", "Figure out how the arguments are passed on in the two recursive `hanoi()` calls!\n", "\n", @@ -341,7 +341,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.11**: Execute the code cells below and confirm that the printed moves are correct!" + "**Q11**: Execute the code cells below and confirm that the printed moves are correct!" ] }, { @@ -415,7 +415,7 @@ "\n", "Let's create a `hanoi_moves()` function that follows the same internal logic as `hanoi()`, but, instead of printing out the moves, returns the number of steps done so far in the recursion.\n", "\n", - "Write your answers to **Q1.12** to **Q1.14** into the subsequent code cell and finalize `hanoi_moves()`! No need to write a docstring or validate the input here." + "Write your answers to **Q12** to **Q14** into the subsequent code cell and finalize `hanoi_moves()`! No need to write a docstring or validate the input here." ] }, { @@ -426,12 +426,12 @@ "source": [ "def hanoi_moves(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\"):\n", "\n", - " # answer to Q15.12\n", + " # answer to Q12\n", " # ...\n", "\n", - " moves = ... # <- answer to Q15.13\n", - " moves += hanoi_moves(...) # <- answer to Q15.14 between the ()\n", - " moves += hanoi_moves(...) # <- answer to Q15.14 between the ()\n", + " moves = ... # <- answer to Q13\n", + " moves += hanoi_moves(...) # <- answer to Q14 between the ()\n", + " moves += hanoi_moves(...) # <- answer to Q14 between the ()\n", "\n", " return moves" ] @@ -440,21 +440,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.12**: Copy the base case from `hanoi()`! What count should be returned when it is reached?" + "**Q12**: Copy the base case from `hanoi()`! What count should be returned when it is reached?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.13**: Initialize the variable `moves` with an appropriate count! This is the number of moves that corresponds to **one** recursive function call." + "**Q13**: Initialize the variable `moves` with an appropriate count! This is the number of moves that corresponds to **one** recursive function call." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.14**: `moves` is updated with the counts passed up from the two recursive calls.\n", + "**Q14**: `moves` is updated with the counts passed up from the two recursive calls.\n", "\n", "Complete the two recursive function calls with the same arguments as in `hanoi()`!" ] @@ -463,7 +463,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.15**: Write a `for`-loop that prints out the **minimum number** of moves needed to solve Towers of Hanoi for any number of `disks` from `1` through `20` to confirm your answer to **Q1.2**." + "**Q15**: Write a `for`-loop that prints out the **minimum number** of moves needed to solve Towers of Hanoi for any number of `disks` from `1` through `20` to confirm your answer to **Q2**." ] }, { @@ -491,7 +491,7 @@ "\n", "With `disks` in the range from `24` through `26`, the computation time roughly doubles for each increase of `disks` by 1.\n", "\n", - "**Q1.16**: Execute the code cells below and see for yourself!" + "**Q16**: Execute the code cells below and see for yourself!" ] }, { @@ -537,7 +537,7 @@ "source": [ "The above `hanoi()` prints the optimal solution's moves in the correct order but fails to label each move with an order number. This can be built in by passing on one more argument `offset` down the recursion tree. As the logic gets a bit \"involved,\" `hanoi_ordered()` below is almost finished.\n", "\n", - "Write your answers to **Q1.17** and **Q1.18** into the subsequent code cell and finalize `hanoi_ordered()`! No need to write a docstring or validate the input here." + "Write your answers to **Q17** and **Q18** into the subsequent code cell and finalize `hanoi_ordered()`! No need to write a docstring or validate the input here." ] }, { @@ -548,7 +548,7 @@ "source": [ "def hanoi_ordered(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\", offset=None):\n", "\n", - " # answer to Q15.17\n", + " # answer to Q17\n", " # ...\n", "\n", " total = (2 ** disks - 1)\n", @@ -558,23 +558,23 @@ " if offset is not None:\n", " count += offset\n", "\n", - " hanoi_ordered(..., offset=offset) # <- answer to Q15.18\n", - " # ... <- answer to Q15.18\n", - " hanoi_ordered(..., offset=count) # <- answer to Q15.18" + " hanoi_ordered(..., offset=offset) # <- answer to Q18\n", + " # ... <- answer to Q18\n", + " hanoi_ordered(..., offset=count) # <- answer to Q18" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.17**: Copy the base case from the original `hanoi()`!" + "**Q17**: Copy the base case from the original `hanoi()`!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.18**: Complete the two recursive function calls with the same arguments as in `hanoi()` or `hanoi_moves()`! Do not change the already filled in `offset` arguments!\n", + "**Q18**: Complete the two recursive function calls with the same arguments as in `hanoi()` or `hanoi_moves()`! Do not change the already filled in `offset` arguments!\n", "\n", "Then, copy the `print()` statement from `hanoi()` and adjust it to print out `count` as well!" ] @@ -583,7 +583,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.19**: Execute the code cells below and confirm that the order numbers are correct!" + "**Q19**: Execute the code cells below and confirm that the order numbers are correct!" ] }, { @@ -640,7 +640,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Q1.20**: Conducting your own research on the internet (max. 15 minutes), what can you say about generalizing the **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem to a setting with **more than three** landing spots?" + "**Q20**: Conducting your own research on the internet (max. 15 minutes), what can you say about generalizing the **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem to a setting with **more than three** landing spots?" ] }, { From c1ad5d8b961f9231c12125a317c4b9ad984f5b8a Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Thu, 12 Mar 2020 00:53:56 +0100 Subject: [PATCH 6/7] Add new exercises set for chapter 04 --- 04_iteration_00_lecture.ipynb | 34 ++- 04_iteration_03_exercises.ipynb | 402 ++++++++++++++++++++++++++++++++ 2 files changed, 430 insertions(+), 6 deletions(-) create mode 100644 04_iteration_03_exercises.ipynb diff --git a/04_iteration_00_lecture.ipynb b/04_iteration_00_lecture.ipynb index 1a72c7b..fb89130 100644 --- a/04_iteration_00_lecture.ipynb +++ b/04_iteration_00_lecture.ipynb @@ -37,6 +37,28 @@ "## Recursion" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "A popular joke among programmers by an unknown author goes like this (cf., [discussion](https://www.quora.com/What-does-the-phrase-in-order-to-understand-recursion-you-must-first-understand-recursion-mean-to-you)):" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "-" + } + }, + "source": [ + "> \"In order to understand **recursion**, you must first understand **recursion**.\"" + ] + }, { "cell_type": "markdown", "metadata": { @@ -859,7 +881,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "37.3 µs ± 1.25 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" + "35.2 µs ± 971 ns per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], @@ -881,7 +903,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "11.4 ms ± 144 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" + "11.2 ms ± 81.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], @@ -903,7 +925,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "3.63 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" + "3.67 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" ] } ], @@ -925,7 +947,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "5.93 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" + "5.94 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" ] } ], @@ -4543,7 +4565,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "5.24 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" + "5.22 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" ] } ], @@ -4824,7 +4846,7 @@ } }, "source": [ - "The `for` statement, on the contrary, makes the actual business logic more apparent by stripping all the **[boilerplate code](https://en.wikipedia.org/wiki/Boilerplate_code)** away." + "The `for` statement, on the contrary, makes the actual business logic more apparent by stripping all the **[boilerplate code](https://en.wikipedia.org/wiki/Boilerplate_code)** away. The variable that is automatically set by Python in each iteration of the loop (i.e., `element` in the example) is called the **target variable**." ] }, { diff --git a/04_iteration_03_exercises.ipynb b/04_iteration_03_exercises.ipynb new file mode 100644 index 0000000..da32407 --- /dev/null +++ b/04_iteration_03_exercises.ipynb @@ -0,0 +1,402 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "# Chapter 4: Recursion & Looping" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Coding Exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Throwing the Dice" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this exercise, you will model the throwing of dice within the context of a guessing game similar to the one shown in the \"*Example: Guessing a Coin Toss*\" section in [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb#Example:-Guessing-a-Coin-Toss).\n", + "\n", + "As the game involves randomness, we import the [random](https://docs.python.org/3/library/random.html) module from the [standard library](https://docs.python.org/3/library/index.html). To follow best practices, we set the random seed as well." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "random.seed(42)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A die has six sides that we labeled with integers `1` to `6` in this exercise. For a fair die, the probability for each side is the same.\n", + "\n", + "**Q1**: Model a `fair_die` as a `list` object!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fair_die = ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q2**: What function from the [random](https://docs.python.org/3/library/random.html) module that we have seen already is useful for modeling a single throw of the `fair_die`? Write a simple expression (i.e., one function call) that draws one of the equally likely sides! Execute the cell a couple of times to \"see\" the probability distribution!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's check if the `fair_die` is indeed fair. To do so, we create a little numerical experiment and throw the `fair_die` `100000` times. We track the six different outcomes in a `list` object called `throws` that we initialize with all `0`s for each outcome.\n", + "\n", + "**Q3**: Complete the `for`-loop below such that it runs `100000` times! In the body, use your answer to **Q2** to simulate a single throw of the `fair_die` and update the corresponding count in `throws`!\n", + "\n", + "Hints: You need to use the indexing operator `[]` and calculate an `index` in each iteration of the loop. Do do not actually need the target variable provided by the `for`-loop and may want to indicate that with an underscore `_`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "throws = [0, 0, 0, 0, 0, 0]\n", + "\n", + "for ... in ...:\n", + " ...\n", + " ...\n", + " ...\n", + "\n", + "throws" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`throws` contains the simulation results as absolute counts.\n", + "\n", + "**Q4**: Complete the `for`-loop below to convert the counts in `throws` to relative frequencies stored in a `list` called `frequencies`! Round the frequencies to three decimals with the built-in [round()](https://docs.python.org/3/library/functions.html#round) function!\n", + "\n", + "Hints: Initialize `frequencies` just as `throws` above. How many iterations does the `for`-loop have? `6` or `100000`? You may want to obtain an `index` variable with the [enumerate()](https://docs.python.org/3/library/functions.html#enumerate) built-in." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "frequencies = [0, 0, 0, 0, 0, 0]\n", + "\n", + "for ... in ...:\n", + " ...\n", + "\n", + "frequencies" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q5**: How could we adapt the `list` object used above to model an `unfair_die` where `1` is as likely as `2`, `2` is twice as likely as `3`, and `3` is twice as likely as `4`, `5`, or `6`, who are all equally likely?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "unfair_die = ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q6**: Copy your solution to **Q2** for the `unfair_die`! Execute the cell a couple of times to \"see\" the probability distribution!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q7**: Copy and adapt your solutions to **Q3** and **Q4** to calculate the `frequencies` for the `unfair_die`!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "throws = [0, 0, 0, 0, 0, 0]\n", + "frequencies = [0, 0, 0, 0, 0, 0]\n", + "\n", + "for ... in ...:\n", + " ...\n", + " ...\n", + " ...\n", + "\n", + "for ... in ...:\n", + " ...\n", + "\n", + "frequencies" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q8**: The built-in [input()](https://docs.python.org/3/library/functions.html#input) allows us to ask the user to enter a `guess`. What is the data type of the object returned by [input()](https://docs.python.org/3/library/functions.html#input)? Assume the user enters the `guess` as a number (i.e., \"1\", \"2\", ...) and not as a text (e.g., \"one\")." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "guess = input(\"Guess the side of the die: \")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "guess" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q9**: Use a built-in constructor to cast `guess` as an `int` object!\n", + "\n", + "Hint: Simply wrap `guess` or `input(\"Guess the side of the die: \")` with the constructor you choose." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q10**: What type of error is raised if `guess` cannot be cast as an `int` object?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q11**: Write a `try` statement that catches the type of error (i.e., your answer to **Q10**) raised if the user's input cannot be cast as an `int` object! Print out some nice error message notifying the user of the bad input!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " ...\n", + "except ...:\n", + " ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q12**: Write a function `get_guess()` that takes a user's input and checks if it is a valid side of the die! The function should *return* either an `int` object between `1` and `6` or `None` if the user enters something invalid.\n", + "\n", + "Hints: You may want to re-use the `try` statement from **Q11**. Instead of printing out an error message, you can also `return` directly from the `except`-clause (i.e., early exit) with `None`. So, the user can make *two* kinds of input errors and maybe you want to model that with two *distinct* `return None` statements. Also, you may want to allow the user to enter leading and trailing whitespace that gets removed without an error message." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_guess():\n", + " \"\"\"Process the user's input.\n", + " \n", + " Returns:\n", + " guess (int / NoneType): either 1, 2, 3, 4, 5 or 6\n", + " if the input can be parsed and None otherwise\n", + " \"\"\"\n", + " ...\n", + "\n", + " # Check if the user entered an integer.\n", + " ...\n", + " ...\n", + " ...\n", + " ...\n", + "\n", + " # Check if the user entered a valid side.\n", + " ...\n", + " ...\n", + " ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q13** Test your function for all *three* cases!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "get_guess()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Q14**: Write an *indefinite* loop where in each iteration a `fair_die` is thrown and the user makes a guess! Print out an error message if the user does not enter something that can be understood as a number between `1` and `6`! The game should continue until the user makes a correct guess." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "...\n", + "...\n", + "...\n", + "\n", + "...\n", + "...\n", + "...\n", + "...\n", + "...\n", + "...\n", + "..." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.4" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": true, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": false, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} From 5108f8446f964359d6d11e33dad77d57ba0fcc8c Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Thu, 12 Mar 2020 01:00:16 +0100 Subject: [PATCH 7/7] Bump version number --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 679d3d7..630bac7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "intro-to-python" -version = "0.6.4" +version = "0.6.5" description = "An introduction to Python and programming for wanna-be data scientists" authors = ["Alexander Hess "] license = "MIT"