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",