2019-09-19 16:03:10 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"\n",
"# Chapter 1: Elements of a Program"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Do you remember how you first learned to speak in your mother tongue? Probably not. No one's memory goes back that far. Your earliest memory as a child should probably be around the age of three or four years old when you could already say simple things and interact with your environment. Although you did not know any grammar rules yet, other people just understood what you said. Well, most of the time.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"It is intuitively best to take the very mindset of a small child when learning a foreign language. This first chapter introduces simplistic examples, and we accept them as they are *without* knowing any of the \"grammar\" rules yet. Then, we analyze them in parts and slowly build up our understanding.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"Consequently, if parts of this chapter do not make sense right away, let's not worry too much. Besides introducing the basic elements, it also serves as an outlook for what is to come. So, many terms and concepts used here are deconstructed in great detail in the following chapters."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
2019-11-06 11:10:29 +01:00
"## Example: Averaging Even Numbers"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-06 11:10:29 +01:00
"As our introductory example, we want to calculate the *average* of all *evens* in a **list** of numbers: `[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]`.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"While we are used to finding an [analytical solution](https://math.stackexchange.com/questions/935405/what-s-the-difference-between-analytical-and-numerical-approaches-to-problems/935446#935446) in math (i.e., derive some equation with \"pen and paper\"), we solve this task *programmatically* instead.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-06 11:10:29 +01:00
"We start by creating a list called `numbers` that holds all the individual numbers between **brackets** `[` and `]`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
2019-11-06 11:10:29 +01:00
"numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"To verify that something happened in our computer's memory, we **reference** `numbers`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-11-06 11:10:29 +01:00
"[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
2019-09-19 16:03:10 +02:00
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-01 17:47:45 +02:00
"So far, so good. Let's see how the desired **computation** could be expressed as a **sequence of instructions** in Python.\n",
2019-09-19 16:03:10 +02:00
"\n",
"Intuitively, the line `for number in numbers` describes a \"loop\" over all the numbers in the `numbers` list, one at a time.\n",
"\n",
2019-11-20 11:00:24 +01:00
"The `if number % 2 == 0` may look confusing at first sight. Both `%` and `==` must have an unintuitive meaning here. Luckily, the **comment** in the same line after the `#` symbol has the answer: The program does something only for an even `number`.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-06 11:10:29 +01:00
"In particular, it increases `count` by `1` and adds the current `number` onto the [running](https://en.wikipedia.org/wiki/Running_total) `total`. Both `count` and `number` are initially set to `0` and the single `=` symbol reads as \"... is *set* equal to ...\". It could not indicate a mathematical equation as, for example, `count` is generally not equal to `count + 1`.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"Lastly, the `average` is calculated as the ratio of the final **values** of `total` and `count`. Overall, we divide the sum of all even numbers by their count: This is nothing but the definition of an average.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"The lines of code \"within\" the `for` and `if` **statements** are **indented** and **aligned** with multiples of **four spaces**: This shows immediately how the lines relate to each other."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
2019-10-14 14:09:28 +02:00
"count = 0 # initialize variables to keep track of the\n",
"total = 0 # running total and the count of even numbers\n",
2019-09-19 16:03:10 +02:00
"\n",
"for number in numbers:\n",
" if number % 2 == 0: # only look at even numbers\n",
" count = count + 1\n",
" total = total + number\n",
"\n",
"average = total / count"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"We do not see any **output** yet but obtain the value of `average` by referencing it again."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-11-06 11:10:29 +01:00
"7.0"
2019-09-19 16:03:10 +02:00
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Generating Cell Output in a Jupyter Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Note how only two of the previous four code cells generate an **output** while two remained \"silent\" (i.e., there is no \"**Out[...]**\" after running the cell).\n",
"\n",
2019-11-20 11:00:24 +01:00
"By default, Jupyter notebooks show the value of the **expression** in the last line of a code cell only. This output can be suppressed by ending the last line with a semicolon `;`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'I am feeling great :-)'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Hello, World!\"\n",
"\"I am feeling great :-)\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"\"I am invisible!\";"
]
},
2019-09-22 20:24:15 +02:00
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"To visualize something before the end of the cell, we use the built-in [print()](https://docs.python.org/3/library/functions.html#print) **function**. Here, the parentheses `()` indicate that we execute code written somewhere else."
2019-09-22 20:24:15 +02:00
]
},
2019-09-19 16:03:10 +02:00
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World!\n",
"I am feeling great :-)\n"
]
}
],
"source": [
"print(\"Hello, World!\")\n",
"print(\"I am feeling great :-)\")"
]
},
2019-09-22 20:24:15 +02:00
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Outside Jupyter notebooks, the semicolon `;` is used as a **separator** between statements that must otherwise be on a line on their own. However, it is *not* considered good practice to use it as it makes code less readable."
2019-09-22 20:24:15 +02:00
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World!\n",
"I am feeling great :-)\n"
]
}
],
"source": [
"print(\"Hello, World!\"); print(\"I am feeling great :-)\")"
]
},
2019-09-19 16:03:10 +02:00
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## (Arithmetic) Operators"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Python comes with many built-in **[operators](https://docs.python.org/3/reference/lexical_analysis.html#operators)**: They are **tokens** (i.e., \"symbols\") that have a special meaning to the Python interpreter.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"The arithmetic operators either \"operate\" with the number immediately following them (= **unary** operators; e.g., negation) or \"process\" the two numbers \"around\" them (= **binary** operators; e.g., addition).\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"By definition, operators have **no** permanent **side effects** in the computer's memory. Although the code cells in this section do indeed create *new* numbers in memory (e.g., `77 + 13` creates `90`), they are immediately \"forgotten\" as they are not stored in a **variable** like `numbers` or `average` above. We develop this thought further at the end of this chapter when we compare **expressions** with **statements**.\n",
2019-10-01 17:47:45 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"Let's see some examples of operators. We start with the binary `+` and the `-` operators for addition and subtraction. Binary operators mimic what mathematicians call [infix notation](https://en.wikipedia.org/wiki/Infix_notation) and have the expected meaning."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 9,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"90"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 9,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"77 + 13"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 10,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 10,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"101 - 93"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"The `-` operator may be used as a unary operator as well. Then it just flips the sign of a number."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 11,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 11,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"-1"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-06 11:10:29 +01:00
"When we compare the output of the `*` and `/` operators for multiplication and division, we note the subtle *difference* between the `42` and the `42.0`: They are the *same* number represented as a *different* **data type**."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 12,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 12,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 * 21"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 13,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42.0"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 13,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"84 / 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"The so-called **floor division operator** `//` always \"rounds\" to an integer and is thus also called **integer division operator**. It is an example of an arithmetic operator we commonly do not know from high school mathematics."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 14,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 14,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"84 // 2"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 15,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 15,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"85 // 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-07 22:31:06 +02:00
"Even though it appears that the `//` operator **truncates** (i.e., \"cuts off\") the decimals so as to effectively \"rounding\" down (i.e., the `42.5` became `42` in the previous code cell), this is *not* the case: The result is always \"rounded\" towards minus infinity!"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 16,
2019-10-07 22:31:06 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"-43"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"-85 // 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"To obtain the remainder of a division, we use the **modulo operator** `%`."
]
},
{
"cell_type": "code",
"execution_count": 17,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 17,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"85 % 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-06 11:10:29 +01:00
"The remainder is `0` *only if* a number is *divisible* by another.\n",
2019-10-14 14:09:28 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"A popular convention in both, computer science and mathematics, is to abbreviate \"only if\" as **iff**, which is short for \"**[if and only if](https://en.wikipedia.org/wiki/If_and_only_if)**.\" The iff means that a remainder of `0` implies that a number is divisible by another but also that a number divisible by another implies a remainder of `0`. The implication goes in *both* directions!\n",
"\n",
"So, `49` is divisible by `7`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 18,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 18,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"49 % 7"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"Modulo division is also useful if we want to extract the last couple of digits in a large integer."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 19,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 19,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"123 % 10"
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 20,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 20,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"123 % 100"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"The built-in [divmod()](https://docs.python.org/3/library/functions.html#divmod) function combines the integer and modulo divisions into one operation. However, 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."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 21,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(4, 2)"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 21,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divmod(42, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Raising a number to a power is performed with the **exponentiation operator** `**`. It is different from the `^` operator other programming languages may use and that also exists in Python with a *different* meaning."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 22,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 22,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 ** 3"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"The standard [order of precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) from mathematics applies (i.e., [PEMDAS](http://mathworld.wolfram.com/PEMDAS.html) rule) when several operators are combined."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 23,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 23,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 ** 2 * 2 "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-01 17:47:45 +02:00
"Parentheses help avoid confusion and take the role of a **delimiter** here."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 24,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 24,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(3 ** 2) * 2 # same result as before but much clearer code"
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 25,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"81"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 25,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 ** (2 * 2) # different result"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-07 22:31:06 +02:00
"Some programmers also use \"style\" conventions. For example, we might play with the **whitespace**, which is an umbrella term that refers to any non-printable sign like spaces, tabs, or the like. However, parentheses convey a much clearer picture."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 26,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 26,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3**2 * 2 # bad style; it is better to use parentheses here"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"There exist many non-mathematical operators that are introduced throughout this book, together with the concepts they implement. They often come in a form different from the unary and binary mentioned above."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
2019-09-22 20:24:15 +02:00
"## Objects vs. Types vs. Values"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Python is a so-called **object-oriented** language, which is a paradigm of organizing a program's memory.\n",
"\n",
2020-01-28 10:48:37 +01:00
"An **object** may be viewed as a \"bag\" of $0$s and $1$s in a given memory location. The $0$s and $1$s in a bag make up the object's **value**. There exist different **types** of bags: Each type comes with distinct rules how the $0$s and $1$s are interpreted and may be worked with.\n",
2019-09-19 16:03:10 +02:00
"\n",
2020-01-28 10:48:37 +01:00
"So, an object *always* has *three* main characteristics. Let's look at the following examples and work them out."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 27,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"a = 789\n",
"b = 42.0\n",
"c = \"Python rocks\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Identity / \"Memory Location\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"The [id()](https://docs.python.org/3/library/functions.html#id) built-in function shows an object's \"address\" in memory."
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 28,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
2020-01-28 10:48:37 +01:00
"140173037405648"
2019-09-19 16:03:10 +02:00
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 28,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(a)"
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 29,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
2020-01-28 10:48:37 +01:00
"140173037405584"
2019-09-19 16:03:10 +02:00
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 29,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(b)"
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 30,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
2020-01-28 10:48:37 +01:00
"140173037234160"
2019-09-19 16:03:10 +02:00
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 30,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(c)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"These addresses are *not* meaningful for anything other than checking if two variables reference the *same* object. Let's create a second variable `d` and also set it to `789`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 31,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"d = 789"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2020-02-02 16:31:31 +01:00
"`a` and `d` indeed have the same value as is checked with the **equality operator** `==`. The resulting `True` (and the `False` further below) is yet another data type, a so-called **boolean**. We look into them closely in [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb#Boolean-Expressions)."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 32,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 32,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a == d"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"On the contrary, `a` and `d` are different objects as the **identity operator** `is` shows: they are stored at separate addresses in the memory."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-10-07 22:31:06 +02:00
"execution_count": 33,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
2019-10-07 22:31:06 +02:00
"execution_count": 33,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a is d"
]
},
2019-11-06 11:10:29 +01:00
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"If we want to check the opposite case, we use the negated version of the `is` operator."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a is not d"
]
},
2019-09-19 16:03:10 +02:00
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### (Data) Type / \"Behavior\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-09-22 20:24:15 +02:00
"The [type()](https://docs.python.org/3/library/functions.html#type) built-in function shows an object's type. For example, `a` is an integer (i.e., `int`) while `b` is a so-called [floating-point number](https://en.wikipedia.org/wiki/Floating-point_arithmetic) (i.e., `float`)."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 35,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 35,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 36,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 36,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(b)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Different types imply different behaviors for the objects. The `b` object, for example, can be \"asked\" if it could also be interpreted as an `int` with the [is_integer()](https://docs.python.org/3/library/stdtypes.html#float.is_integer) \"functionality\" that comes with every `float` object.\n",
"\n",
2020-01-28 10:48:37 +01:00
"Formally, we call such type-specific functionalities **methods** (to differentiate them from functions) and we formally introduce them in Chapter 10. For now, it suffices to know that we access them using the **dot operator** `.`. Of course, `b` could be converted into an `int`, which the boolean value `True` tells us."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 37,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 37,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.is_integer()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"For an `int` object, this [is_integer()](https://docs.python.org/3/library/stdtypes.html#float.is_integer) check does *not* make sense as we already know it is an `int`: We see the `AttributeError` below as `a` does not even know what `is_integer()` means."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 38,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'int' object has no attribute 'is_integer'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
2019-11-06 11:10:29 +01:00
"\u001b[0;32m<ipython-input-38-7db0a38aefcc>\u001b[0m in \u001b[0;36m<module>\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",
2019-09-19 16:03:10 +02:00
"\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'is_integer'"
]
}
],
"source": [
"a.is_integer()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"The `c` object is a so-called **string** type (i.e., `str`), which is Python's way of representing \"text.\" Strings also come with peculiar behaviors, for example, to convert a text to lower or upper case."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 39,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 39,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(c)"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 40,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'python rocks'"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 40,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.lower()"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 41,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'PYTHON ROCKS'"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 41,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.upper()"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 42,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Python Rocks'"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 42,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.title()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Value"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2020-01-28 10:48:37 +01:00
"Almost trivially, every object also has a value to which it \"evaluates\" when referenced. We think of the value as the **conceptual idea** of what the $0$s and $1$s in the bag mean to *humans* as machines cannot see beyond $0$s and $1$s.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"For built-in data types, Python prints out the object's value as a so-called **[literal](https://docs.python.org/3/reference/lexical_analysis.html#literals)**: This means that we can copy and paste the output back into a code cell to create a *new* object with the *same* value."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 43,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"789"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 43,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 44,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42.0"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 44,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"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."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 45,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Python rocks'"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 45,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2019-09-22 20:24:15 +02:00
"c # we defined c = \"Python rocks\" with double quotes \" above"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Formal vs. Natural Languages"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Just like the language of mathematics is good at expressing relationships among numbers and symbols, any programming language is just a formal language that is good at expressing computations.\n",
"\n",
"Formal languages come with their own \"grammatical rules\" called **syntax**."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Syntax Errors"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-09-22 20:24:15 +02:00
"If we do not follow the rules, the code cannot be **parsed** correctly, i.e., the program does not even start to run but **raises** a **syntax error** indicated as `SyntaxError` in the output. Computers are very dumb in the sense that the slightest syntax error leads to the machine not understanding our code.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"For example, if we wanted to write an accounting program that adds up currencies, we would have to model dollar prices as `float` objects as the dollar symbol cannot be understood by Python."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 46,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "SyntaxError",
2019-11-06 11:10:29 +01:00
"evalue": "invalid syntax (<ipython-input-46-cafa82e54b9c>, line 1)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-11-06 11:10:29 +01:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-46-cafa82e54b9c>\"\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"
2019-09-19 16:03:10 +02:00
]
}
],
"source": [
"3.99 $ + 10.40 $"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Python requires certain symbols at certain places (e.g., a `:` is missing here)."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 47,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "SyntaxError",
2019-11-06 11:10:29 +01:00
"evalue": "invalid syntax (<ipython-input-47-499e4d0d0cbb>, line 1)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-11-06 11:10:29 +01:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-47-499e4d0d0cbb>\"\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"
2019-09-19 16:03:10 +02:00
]
}
],
"source": [
"for number in numbers\n",
" print(number)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Furthermore, it relies on whitespace (i.e., indentation), unlike many other programming languages. The `IndentationError` below is just a particular type of a `SyntaxError`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 48,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "IndentationError",
2019-11-06 11:10:29 +01:00
"evalue": "expected an indented block (<ipython-input-48-19398c5f89de>, line 2)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-11-06 11:10:29 +01:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-48-19398c5f89de>\"\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"
2019-09-19 16:03:10 +02:00
]
}
],
"source": [
"for number in numbers:\n",
"print(number)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Runtime Errors"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Syntax errors are easy to find as the code does not even run in the first place.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-09-22 20:24:15 +02:00
"However, there are also so-called **runtime errors**, often called **exceptions**, that occur whenever otherwise (i.e., syntactically) correct code does not run because of invalid input.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-06 11:10:29 +01:00
"This example does not work because just like in the \"real\" world, Python does not know how to divide by `0`. The syntactically correct code leads to a `ZeroDivisionError`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 49,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
2019-11-06 11:10:29 +01:00
"\u001b[0;32m<ipython-input-49-bc757c3fda29>\u001b[0m in \u001b[0;36m<module>\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",
2019-09-19 16:03:10 +02:00
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"1 / 0"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Semantic Errors"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-07 22:31:06 +02:00
"So-called **semantic errors**, on the contrary, are hard to spot as they do *not* crash the program. The only way to find such errors is to run a program with test input for which we can predict the output. However, testing software is a whole discipline on its own and often very hard to do in practice.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"The cell below copies our first example from above with a \"tiny\" error. How fast could you have spotted it without the comment?"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 50,
2019-09-19 16:03:10 +02:00
"metadata": {
"code_folding": [],
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"count = 0\n",
"total = 0\n",
"\n",
"for number in numbers:\n",
" if number % 2 == 0:\n",
" count = count + 1\n",
" total = total + count # count is wrong here, it should be number\n",
"\n",
"average = total / count"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 51,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-11-06 11:10:29 +01:00
"3.5"
2019-09-19 16:03:10 +02:00
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 51,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Systematically finding errors is called **debugging**. For the history of the term, see this [article](https://en.wikipedia.org/wiki/Debugging)."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Best Practices"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Thus, adhering to just syntax rules is *never* enough. Over time, **best practices** and **style guides** were created to make it less likely for a developer to mess up a program and also to allow \"onboarding\" him as a contributor to an established code base, often called **legacy code**, faster. These rules are not enforced by Python itself: Badly styled code still runs. At the very least, Python programs should be styled according to [PEP 8](https://www.python.org/dev/peps/pep-0008/) and documented \"inline\" (i.e., in the code itself) according to [PEP 257](https://www.python.org/dev/peps/pep-0257/).\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"An easier to read version of PEP 8 is [here](https://pep8.org/). The video below features a well known **[Pythonista](https://en.wiktionary.org/wiki/Pythonista)** talking about the importance of code style."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 52,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAUDBAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIChANCAgOCggIDRUNDhERExMTCAsWGBYSGBASExIBBQUFCAcIDwkJDxUVEBUVFhUVFRUSFRUVFRISEhUVFRUVFRUVFRUVFRIVEhUVFRUVFRUVFRUVFRUVFRUVFRUVFf/AABEIAWgB4AMBIgACEQEDEQH/xAAcAAEAAgMBAQEAAAAAAAAAAAAABgcEBQgDAgH/xABfEAABAwICBQUHDgsGAggFBQABAAIDBBEFEgYHEyExQVFTYZIUFyJxgZHTCCMyNUJSYnJzdKGxs8EVJCUzNIKDorK0wkNjZJOjw9HwJ1RVlKS10uEWJmWVpTY3RFZ1/8QAHAEBAAIDAQEBAAAAAAAAAAAAAAUGAwQHAgEI/8QAQBEAAQIDAwgGBwcEAwEAAAAAAQACAwQRBSExBhITQVFxkcEWYWKBobEUIjRTcpLRIyQyMzWC4QdSsvBCovEV/9oADAMBAAIRAxEAPwDjJEREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREX6innexrOmpu3L6JO9jWdNTduX0SUKmuj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P8AuncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P+6dwUDRTzvY1nTU3bl9EnexrOmpu3L6JKFOj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P8AuncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P+6dwUDRTzvY1nTU3bl9EnexrOmpu3L6JKFOj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P8AuncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P+6dwUDRTzvY1nTU3bl9EnexrOmpu3L6JKFOj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P8AuncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P+6dwUDRTzvY1nTU3bl9EnexrOmpu3L6JKFOj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P8AuncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P+6dwUDRTzvY1nTU3bl9EnexrOmpu3L6JKFOj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P8AuncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P+6dwUDRTzvY1nTU3bl9EnexrOmpu3L6JKFOj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P8AuncFA0U872NZ01N25fRJ3sazpqbty+iShTo9P+6dwUDRTzvY1nTU3bl9EnexrOmpu3L6JKFOj0/7p3BQNFPO9jWdNTduX0Sd7Gs6am7cvokoU6PT/uncFbCIi2F3RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERTTVfoizE5JZJ3HueAtaYmOLXyvcMwDnDeyMDlG8ncCLFa81NMl4RiPwCwTMwyBDMR+AULRdEt0MwoC34PpfGYWl3lefCJ67rT41qxw2YEwiSlfyOheXM8Topcwy/FynrUDDyplnGjgR10B5qHZlDBJo4OHXd9VRyKRaX6H1eG+FIBJASA2oiByXO4NlYd8Lid2+4NwASdyjqsMCPDjMDoZBB1hTUGOyM3OYahERFlWVEUz0Q1eVOIQCoMzKWJxOyzxOkfI0btoGh7bMJuASd9r8LEyin1Qwf2tbO75KKKP+POomYtuUguLXPvFxABN/BRsW15WG4tc68agCeVFUiK5ZdU1AW2bUVjX8khdC6/U5myAI8Vj1quNM9Fp8MlayUiSOS5inYCGuy2zNc032cguN1yLHcTvt9k7Ylpl+ZDdfsIpXcvsrakCYdmNN+wilVoURWLqs0JgrY+7arw487mRQNcWtJYbOfMW2J38GA2tvN7gDanZyHKwjEfh1Yk7FsTc0yWh6R+HmVXSLok6GYURb8H0g6xCxp7Td/0qP45qsoZQTSvkpH77AOM0RPwmSHMP1XDxFQkHKmWeaOBb10qFFQ8oIDjRwI68fJUsi2+lGjdXh0gZUx+C4kRTMJdDLbfZrreC+3uXAHceI3rUKxQorIrQ9hqDrCm4cVsRoc01B1hERe+G0j6iaKCP85M9sTeUAuIbmPwRe56gV6e4NFTgvrnBozisvAcCq69xbSQulykB77hscd9/hyPIANt9uPUpK3VbivPRjqM8t/LaAhXFgOFQ0dPHTwNyxsFvhOd7qR55XuO8leFdpLh8MphmrKaOYENdG+VjXguAIDgTuuHDjzqkRso5mJEIgMuHUTdtKqcW3I8R5EFt24k02lc+Y7g1RQy7GqiMb95YeMcrRuzRPG57eHWLi4CwF0xj2EU9dA6CpjD2O4cjo3ckkbvcvHP5N4JC530jwmShqpqWXeYj4L7WD4jvjkHjbbdyG45FO2PbDZ0FrhR4xGojaFL2XaomgWOFHDgRtC16IinFLoiIiIiIiIiIiIl/wDgPHzeNZWEYfLVzxU8Dc0srsrRwaOVz3n3LGtBJPMOU2CvrQ3Q+lw2NuVolqLeuVD2jOTyhnRx8zR5STvUTadrwpIDOFXHADZtJ1KNtC04coBW8nAfXYFQpwyqtfuWpy++7nmy+fJZYl+I5RuI5QeYjkK6pAC1eOaP0lc0tqYI5d1g8i0jeS7JG2cw+IqDhZWjO+0ZdtB5KJh5R3+uy7qPKi5rRSjT/RCTC5WkEy00ptDKbZg62bZy23Z7AkEWDgDuFiFF1bJeYhx4YiQzUFWOBHZGYHsNQUREWZZURWjq20FoquiZVVYklfI6XK1sr42RtY90YHrZBc45STc8o5t+9xDVbhkjbRCendyOZM6TztnzAjxWUDGyilYUUwnVqDQml1RjrUPFtyXhxDDdW40rS6vFUiilWmOg9XhwMm6emHGeNpBZzbaMklg+ECW85FwFFVLy8zDjsz4bgR1KSgTDIzc+GahERFnWZERERE5hyk2A5STwAHKVItCNEqjFJDlOyp2ECaci9jx2cbfdykG/MBvPIDdmjmi9Fh7LU8Lc9rOmfZ80nxnkXA3nwW2A5AFB2lbkGUOZ+J2wat51KJnrYhyxzRe7YNW8qhqfRzEJRdlDVOHPsJWjzuaFjYjhVTT76inqIAdwdLDIxtzuAD3CxPVddNiwXlVwMkY9kjGvY9pa5rwCxzTxDgdxFlCMyqiF3rQxTqJqohuUUTOvYKb71y6i9axrBLKIjmiEsrYnXzZohIRG7Ny3aGm/WvJXVpqKq2NdnBERF6X1ERERERERFZGoWqtU1kN/zkMUgHJeF5aSOu0w8yrdS7U/UbPF4R00c8P+mZv9kKMtiHpJOIOzXhfyWhajM+VeOqvC/kryxCpEMMszgS2KJ8pAtchjS8gX3XsFi6P43TV8IlpZA9nBw4PY618kjDvY/fwPjFxvX3pC29JVDnp5x543Bc
"text/html": [
"\n",
" <iframe\n",
" width=\"60%\"\n",
" height=\"300\"\n",
" src=\"https://www.youtube.com/embed/Hwckt4J96dI\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
2020-01-28 10:48:37 +01:00
"<IPython.lib.display.YouTubeVideo at 0x7f7c94098310>"
2019-09-19 16:03:10 +02:00
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 52,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import YouTubeVideo\n",
"YouTubeVideo(\"Hwckt4J96dI\", width=\"60%\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2020-02-02 16:31:31 +01:00
"For example, while the above code to calculate the average of the even numbers in `[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]` is correct, a Pythonista would rewrite it in a more \"Pythonic\" way and use the [sum()](https://docs.python.org/3/library/functions.html#sum) and [len()](https://docs.python.org/3/library/functions.html#len) (= \"length\") [built-in functions](https://docs.python.org/3/library/functions.html) (cf., [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb#Built-in-Functions)) as well as a so-called **list comprehension** (cf., [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_00_lecture.ipynb#List-Comprehensions)). Pythonic code runs faster in many cases and is less error-prone."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 53,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
2019-11-06 11:10:29 +01:00
"numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 54,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
2019-09-22 20:24:15 +02:00
"evens = [n for n in numbers if n % 2 == 0] # example of a so-called list comprehension"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 55,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-11-06 11:10:29 +01:00
"[8, 12, 2, 6, 10, 4]"
2019-09-19 16:03:10 +02:00
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 55,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evens"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 56,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"average = sum(evens) / len(evens) # built-in functions are much faster than a for-loop"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 57,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-11-06 11:10:29 +01:00
"7.0"
2019-09-19 16:03:10 +02:00
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 57,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"To get a rough overview of the mindsets of a typical Python programmer, see these rules by an early Python core developer deemed so important that they are included in every Python installation."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 58,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Zen of Python, by Tim Peters\n",
"\n",
"Beautiful is better than ugly.\n",
"Explicit is better than implicit.\n",
"Simple is better than complex.\n",
"Complex is better than complicated.\n",
"Flat is better than nested.\n",
"Sparse is better than dense.\n",
"Readability counts.\n",
"Special cases aren't special enough to break the rules.\n",
"Although practicality beats purity.\n",
"Errors should never pass silently.\n",
"Unless explicitly silenced.\n",
"In the face of ambiguity, refuse the temptation to guess.\n",
"There should be one-- and preferably only one --obvious way to do it.\n",
"Although that way may not be obvious at first unless you're Dutch.\n",
"Now is better than never.\n",
"Although never is often better than *right* now.\n",
"If the implementation is hard to explain, it's a bad idea.\n",
"If the implementation is easy to explain, it may be a good idea.\n",
"Namespaces are one honking great idea -- let's do more of those!\n"
]
}
],
"source": [
"import this"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"### Jupyter Notebook Aspects to keep in Mind"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"#### The Order of Code Cells is arbitrary"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Observe that you can run the code cells in a Jupyter notebook in any arbitrary order.\n",
"\n",
2019-10-14 14:09:28 +02:00
"That means, for example, that a variable defined towards the bottom could accidentally be referenced at the top of the notebook. This happens quickly when we iteratively built a program and go back and forth between cells.\n",
2019-09-19 16:03:10 +02:00
"\n",
"As a good practice, it is recommended to click on \"Kernel\" > \"Restart & Run All\" in the navigation bar once a notebook is finished. That restarts the Python process forgetting any **state** (i.e., all variables) and ensures that the notebook runs top to bottom without any errors the next time it is opened."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"#### Notebooks are linear"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"While this book is built with Jupyter notebooks, it is crucial to understand that \"real\" programs are almost always just \"linear\" (= top to bottom) sequences of instructions but instead may take many different **flows of execution**.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"At the same time, for a beginner's course, it is often easier to code linearly.\n",
2019-09-19 16:03:10 +02:00
"\n",
2020-02-02 16:31:31 +01:00
"In real data science projects, one would probably employ a mixed approach and put reusable code into so-called Python modules (i.e., *.py* files; cf., [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb#Local-Modules-and-Packages)) and then use Jupyter notebooks to build up a linear report or storyline for a business argument to be made."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
2019-09-22 20:24:15 +02:00
"## Variables vs. Names vs. Identifiers"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"**Variables** are created with the **[assignment statement](https://docs.python.org/3/reference/simple_stmts.html#assignment-statements)** `=`, which is *not* an operator, mainly because of its side effect of making a **[name](https://docs.python.org/3/reference/lexical_analysis.html#identifiers)** reference an object in memory.\n",
2019-09-22 20:24:15 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"We read the terms **variable**, **name**, and **identifier** used interchangebly in many Python-related texts. In this book, we adopt the following convention: First, we treat *name* and *identifier* as perfect synonyms but only use the term *name* in the text for clarity. Second, whereas *name* only refers to a string of letters, numbers, and some other symbols, a *variable* means the combination of a *name* and a *reference* to an object in memory."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 59,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"a = 20.0\n",
"b = 789"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"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."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 60,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"20.0"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 60,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-07 22:31:06 +02:00
"A variable may be **re-assigned** as often as we wish. Thereby, we could also assign an object of a *different* type. Because this is allowed, Python is said to be a **dynamically typed** language. On the contrary, a **statically typed** language like C also allows re-assignment but only with objects of the *same* type. This subtle distinction is one reason why Python is slower at execution than C: As it runs a program, it needs to figure out an object's type each time it is referenced. But as mentioned before, this is mitigated with third-party libraries."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 61,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
2019-11-20 11:00:24 +01:00
"a = 20 # this makes a reference an object of a different type"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 62,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 62,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"If we want to re-assign a variable while referencing its \"old\" (i.e., current) object, we may also **update** it using a so-called **[augmented assignment statement](https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements)** (*not* operator), introduced with [PEP 203](https://www.python.org/dev/peps/pep-0203/): The currently mapped object is implicitly inserted as the first operand on the right-hand side."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 63,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"a *= 4 # same as a = a * 4"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 64,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"a //= 2 # same as a = a // 2, \"//\" to retain the integer type"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 65,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"a += 2 # same as a = a + 2"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 66,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 66,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"Variables are **[dereferenced](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement)** (i.e., \"deleted\") with the `del` statement. It does *not* delete the object a variable references but merely removes the variable's name from the \"global list of all names.\""
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 67,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"789"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 67,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 68,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"del b"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"If we refer to an unknown name, a *runtime* error occurs, namely a `NameError`. The `Name` in `NameError` gives a hint as to why we prefer the term *name* over *identifier*: Python uses it more often in its error messages."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 69,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'b' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
2019-11-06 11:10:29 +01:00
"\u001b[0;32m<ipython-input-69-89e6c98d9288>\u001b[0m in \u001b[0;36m<module>\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",
2019-09-19 16:03:10 +02:00
"\u001b[0;31mNameError\u001b[0m: name 'b' is not defined"
]
}
],
"source": [
"b"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2020-01-28 10:48:37 +01:00
"Some variables magically exist when we start a Python process or are added by Jupyter. We may safely ignore the former until Chapter 10 and the latter for good."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 70,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'__main__'"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 70,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"__name__"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"To see all defined names, the built-in function [dir()](https://docs.python.org/3/library/functions.html#dir) is helpful."
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 71,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"['In',\n",
" 'Out',\n",
" 'YouTubeVideo',\n",
" '_',\n",
" '_10',\n",
" '_11',\n",
" '_12',\n",
" '_13',\n",
" '_14',\n",
" '_15',\n",
" '_16',\n",
" '_17',\n",
" '_18',\n",
" '_19',\n",
" '_2',\n",
" '_20',\n",
" '_21',\n",
" '_22',\n",
" '_23',\n",
" '_24',\n",
2019-09-22 20:24:15 +02:00
" '_25',\n",
2019-10-07 22:31:06 +02:00
" '_26',\n",
2019-09-19 16:03:10 +02:00
" '_28',\n",
2019-09-22 20:24:15 +02:00
" '_29',\n",
2019-10-07 22:31:06 +02:00
" '_30',\n",
2019-09-19 16:03:10 +02:00
" '_32',\n",
" '_33',\n",
" '_34',\n",
2019-09-22 20:24:15 +02:00
" '_35',\n",
2019-10-07 22:31:06 +02:00
" '_36',\n",
2019-11-06 11:10:29 +01:00
" '_37',\n",
2019-09-19 16:03:10 +02:00
" '_39',\n",
" '_4',\n",
" '_40',\n",
" '_41',\n",
" '_42',\n",
2019-09-22 20:24:15 +02:00
" '_43',\n",
2019-10-07 22:31:06 +02:00
" '_44',\n",
2019-11-06 11:10:29 +01:00
" '_45',\n",
2019-09-19 16:03:10 +02:00
" '_5',\n",
2019-10-07 22:31:06 +02:00
" '_51',\n",
2019-11-06 11:10:29 +01:00
" '_52',\n",
" '_55',\n",
" '_57',\n",
" '_60',\n",
" '_62',\n",
2019-10-07 22:31:06 +02:00
" '_66',\n",
2019-11-06 11:10:29 +01:00
" '_67',\n",
" '_70',\n",
2019-09-19 16:03:10 +02:00
" '_9',\n",
" '__',\n",
" '___',\n",
" '__builtin__',\n",
" '__builtins__',\n",
" '__doc__',\n",
" '__loader__',\n",
" '__name__',\n",
" '__package__',\n",
" '__spec__',\n",
" '_dh',\n",
" '_i',\n",
" '_i1',\n",
" '_i10',\n",
" '_i11',\n",
" '_i12',\n",
" '_i13',\n",
" '_i14',\n",
" '_i15',\n",
" '_i16',\n",
" '_i17',\n",
" '_i18',\n",
" '_i19',\n",
" '_i2',\n",
" '_i20',\n",
" '_i21',\n",
" '_i22',\n",
" '_i23',\n",
" '_i24',\n",
" '_i25',\n",
" '_i26',\n",
" '_i27',\n",
" '_i28',\n",
" '_i29',\n",
" '_i3',\n",
" '_i30',\n",
" '_i31',\n",
" '_i32',\n",
" '_i33',\n",
" '_i34',\n",
" '_i35',\n",
" '_i36',\n",
" '_i37',\n",
" '_i38',\n",
" '_i39',\n",
" '_i4',\n",
" '_i40',\n",
" '_i41',\n",
" '_i42',\n",
" '_i43',\n",
" '_i44',\n",
" '_i45',\n",
" '_i46',\n",
" '_i47',\n",
" '_i48',\n",
" '_i49',\n",
" '_i5',\n",
" '_i50',\n",
" '_i51',\n",
" '_i52',\n",
" '_i53',\n",
" '_i54',\n",
" '_i55',\n",
" '_i56',\n",
" '_i57',\n",
" '_i58',\n",
" '_i59',\n",
" '_i6',\n",
" '_i60',\n",
" '_i61',\n",
" '_i62',\n",
" '_i63',\n",
" '_i64',\n",
" '_i65',\n",
" '_i66',\n",
" '_i67',\n",
" '_i68',\n",
2019-09-22 20:24:15 +02:00
" '_i69',\n",
2019-09-19 16:03:10 +02:00
" '_i7',\n",
2019-10-07 22:31:06 +02:00
" '_i70',\n",
2019-11-06 11:10:29 +01:00
" '_i71',\n",
2019-09-19 16:03:10 +02:00
" '_i8',\n",
" '_i9',\n",
" '_ih',\n",
" '_ii',\n",
" '_iii',\n",
" '_oh',\n",
" 'a',\n",
" 'average',\n",
" 'c',\n",
" 'count',\n",
" 'd',\n",
" 'evens',\n",
" 'exit',\n",
" 'get_ipython',\n",
" 'number',\n",
" 'numbers',\n",
" 'quit',\n",
" 'this',\n",
" 'total']"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 71,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Who am I? And how many?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"It is *crucial* to understand that *several* variables may reference the *same* object in memory. Not having this in mind may lead to many hard to track down bugs.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"Make `b` reference whatever object `a` is referencing."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 72,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
2019-09-22 20:24:15 +02:00
"b = a"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 73,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 73,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 74,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 74,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"For \"simple\" types like `int` or `float` this never causes troubles.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"Let's \"change the value\" of `a`. To be precise, let's create a *new* `123` object and make `a` reference it."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 75,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"a = 123"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 76,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"123"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 76,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"`b` \"is still the same\" as before. To be precise, `b` still references the *same object* as before."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 77,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 77,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"However, if a variable references an object of a more \"complex\" type (e.g., `list`), \"weird\" things happen."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 78,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"x = [1, 2, 3]"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 79,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 79,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 80,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"y = x"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Let's change the first element of `x`.\n",
"\n",
2020-02-02 16:31:31 +01:00
"[Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_00_lecture.ipynb#The-list-Type) discusses lists in more depth. For now, let's view a `list` object as some sort of **container** that holds an arbitrary number of references to other objects and treat the brackets `[]` attached to it as just another operator, called the **indexing operator**. `x[0]` instructs Python to first follow the reference from the global list of all names to the `x` object. Then, it follows the first reference it finds there to the `1` object. The indexing operator must be an operator as we merely read the first element and do not change anything in memory.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-09-22 20:24:15 +02:00
"Note how Python **begins counting at 0**. This is not the case for many other languages, for example, [MATLAB](https://en.wikipedia.org/wiki/MATLAB), [R](https://en.wikipedia.org/wiki/R_%28programming_language%29), or [Stata](https://en.wikipedia.org/wiki/Stata). To understand why this makes sense, see this short [note](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html) by one of the all-time greats in computer science, the late [Edsger Dijkstra](https://en.wikipedia.org/wiki/Edsger_W._Dijkstra)."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 81,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 81,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[0]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"To change the first entry in the list, we use the assignment statement `=` again. Here, this does *not* create a *new* variable, or overwrite an existing one, but only changes the object which the first element in `x` referenced. As we only change parts of the `x` object, we say that we **mutate** (i.e., \"change\") its **state**. To use the bag analogy from above, we keep the same bag but \"flip\" some of the $0$s into $1$s and some of the $1$s into $0$s."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 82,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"x[0] = 99"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 83,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[99, 2, 3]"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 83,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"The changes made to the object `x` is referencing can also be seen through the `y` variable!"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 84,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[99, 2, 3]"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 84,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"The illustrated difference in behavior has to do with the fact that integers and floats are **immutable** types while lists are **mutable**.\n",
"\n",
2019-11-20 11:00:24 +01:00
"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 integer object and made `a` reference it while the `b` variable is *not* affected.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"In the second case, `x[0] = 99` creates a *new* integer object `99` and merely changes the first reference in the `x` list.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"In general, the assignment statement creates a new name and makes it reference whatever object is on the right-hand side *iff* the left-hand side is a *pure* name (i.e., it contains no operators like the indexing operator in the example). Otherwise, it *mutates* an already existing object. And, we always must expect that the latter might have more than one variable referencing it.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-01 17:47:45 +02:00
"Visualizing what is going on in the memory with a tool like [PythonTutor](http://pythontutor.com/visualize.html#code=x%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20x%0Ax%5B0%5D%20%3D%2099%0Aprint%28y%5B0%5D%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) might be helpful for a beginner."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Naming Conventions"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-09-24 19:44:39 +02:00
"[Phil Karlton](https://skeptics.stackexchange.com/questions/19836/has-phil-karlton-ever-said-there-are-only-two-hard-things-in-computer-science) famously noted during his time at [Netscape](https://en.wikipedia.org/wiki/Netscape):\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-09-24 19:44:39 +02:00
"> \"There are *two* hard problems in computer science: *naming things* and *cache invalidation* ... and *off-by-one* errors.\""
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-07 22:31:06 +02:00
"Variable names may contain upper and lower case letters, numbers, and underscores (i.e., `_`) and be as long as we want them to be. However, they must not begin with a number. Also, they must not be any of Python's built-in **[keywords](https://docs.python.org/3/reference/lexical_analysis.html#keywords)**.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"Variable names should be chosen such that they do not need any more documentation and are self-explanatory. A widespread convention is to use so-called **[snake\\_case](https://en.wikipedia.org/wiki/Snake_case)**: Keep everything lowercase and use underscores to separate words.\n",
2019-09-19 16:03:10 +02:00
"\n",
"See this [link](https://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Python_and_Ruby) for a comparison of different naming conventions."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"#### Good examples"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 85,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"pi = 3.14"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 86,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"answer_to_everything = 42"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 87,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"my_name = \"Alexander\""
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 88,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"work_address = \"Burgplatz 2, Vallendar\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"#### Bad examples"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 89,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"PI = 3.14 # unless used as a \"global\" constant"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 90,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"answerToEverything = 42 # this is a style used in languages like Java"
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 91,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
2019-09-22 20:24:15 +02:00
"name = \"Alexander\" # name of what?"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 92,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"ename": "SyntaxError",
2019-11-06 11:10:29 +01:00
"evalue": "can't assign to operator (<ipython-input-92-dfc191b6a91a>, line 1)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-11-06 11:10:29 +01:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-92-dfc191b6a91a>\"\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"
2019-09-19 16:03:10 +02:00
]
}
],
"source": [
"address@work = \"Burgplatz 2, Vallendar\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"If a variable name collides with a built-in name, we add a trailing underscore."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 93,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"type_ = \"student\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Variables with leading and trailing double underscores, referred to as **dunder** in Python jargon, are used for built-in functionalities. Do *not* use this style for custom variables unless you exactly know what you are doing!"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-11-06 11:10:29 +01:00
"execution_count": 94,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'__main__'"
]
},
2019-11-06 11:10:29 +01:00
"execution_count": 94,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"__name__"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Expressions"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-20 11:00:24 +01:00
"An **[expression](https://docs.python.org/3/reference/expressions.html)** is any syntactically correct *combination* of *variables* and *literals* with *operators*.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-07 22:31:06 +02:00
"In simple words, anything that may be used on the right-hand side of an assignment statement without creating a `SyntaxError` is an expression.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"What we said about individual operators above, namely that they have *no* side effects, should have been put here, to begin with. The code cells in the section on operators above are all expressions!\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-01 17:47:45 +02:00
"The simplest possible expressions contain only one variable or literal."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 95,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"123"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 95,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 96,
2019-10-01 17:47:45 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 96,
2019-10-01 17:47:45 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"42"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For sure, we need to include operators to achieve something useful."
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 97,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"165"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 97,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a + b"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"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."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 98,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"4492125"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 98,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a + b) ** 3"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-01 17:47:45 +02:00
"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."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 99,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 99,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[2]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"When not used as a delimiter, parentheses also constitute an operator, namely the **call operator** `()`. We saw this syntax above when we \"called\" (i.e., executed) built-in functions and methods."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 100,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"104"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 100,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(x)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Operator Overloading"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"Python **overloads** certain operators. For example, you may not only \"add\" numbers but also strings: This is called **string concatenation**."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 101,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"greeting = \"Hi \"\n",
"audience = \"class\""
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 102,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Hi class'"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 102,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"greeting + audience"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Duplicate strings using multiplication."
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 103,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"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 '"
]
},
2020-01-28 10:48:37 +01:00
"execution_count": 103,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b * greeting"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Statements"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"A **[statement](https://docs.python.org/3/reference/simple_stmts.html)** is anything that *changes* the *state of a program* or has another *side effect*. Statements, unlike expressions, do not just evaluate to a value; instead, they create or change values.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"Most notably, of course, are the `=` and `del` statements."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 104,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"a = 123"
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 105,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"del a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"The built-in [print()](https://docs.python.org/3/library/functions.html#print) function is regarded as a \"statement\" as well. It used to be an actual statement in Python 2 and has all the necessary properties. It is a bit of a corner case as expressions are also \"printed\" in a Jupyter notebook when evaluated last in a code cell."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 106,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2019-10-30 11:04:59 +01:00
"I change the state of the computer's display\n"
2019-09-19 16:03:10 +02:00
]
}
],
"source": [
2019-10-30 11:04:59 +01:00
"print(\"I change the state of the computer's display\")"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Comments"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-07 22:31:06 +02:00
"We use the `#` symbol to write comments in plain English right into the code.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-11-20 11:00:24 +01:00
"As a good practice, comments should *not* describe *what* happens (this should be evident by reading the code; otherwise, it is most likely badly written code) but *why* something happens.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-14 14:09:28 +02:00
"Comments may be added either at the end of a line of code, by convention separated with two spaces, or on a line on their own."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 107,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"distance = 891 # in meters\n",
"elapsed_time = 93 # in seconds\n",
"# Calculate the speed in km/h.\n",
"speed = 3.6 * distance / elapsed_time"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-14 14:09:28 +02:00
"But let's think wisely if we need to use a comment.\n",
"The second cell is a lot more Pythonic."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 108,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"seconds = 365 * 24 * 60 * 60 # = seconds in the year"
]
},
{
"cell_type": "code",
2020-01-28 10:48:37 +01:00
"execution_count": 109,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"seconds_per_year = 365 * 24 * 60 * 60"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## TL;DR"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-11-06 11:10:29 +01:00
"We end each chapter with a summary of the main points (i.e., **TL;DR** = \"too long; didn't read\"). The essence in this first chapter is that just as a sentence in a real language like English may be decomposed into its parts (e.g., subject, predicate, and objects), the same may be done with programming languages.\n",
"\n",
2019-09-19 16:03:10 +02:00
"- program\n",
" - **sequence** of **instructions** that specify how to perform a computation (= a \"recipe\")\n",
" - a \"black box\" that processes **inputs** and transforms them into meaningful **outputs** in a *deterministic* way\n",
" - conceptually similar to a mathematical function $f$ that maps some input $x$ to an output $y = f(x)$\n",
"\n",
"\n",
"- input (examples)\n",
2019-11-20 11:00:24 +01:00
" - data from a CSV file\n",
2019-09-19 16:03:10 +02:00
" - text entered on a command line\n",
2019-11-20 11:00:24 +01:00
" - data obtained from a database\n",
2019-10-14 14:09:28 +02:00
" - etc.\n",
2019-09-19 16:03:10 +02:00
"\n",
"\n",
"- output (examples)\n",
" - result of a computation (e.g., statistical summary of a sample dataset)\n",
" - a \"side effect\" (e.g., a transformation of raw input data into cleaned data)\n",
" - a physical \"behavior\" (e.g., a robot moving or a document printed)\n",
2019-10-14 14:09:28 +02:00
" - etc.\n",
2019-09-19 16:03:10 +02:00
"\n",
"\n",
"- objects\n",
2019-10-14 14:09:28 +02:00
" - distinct and well-contained areas/parts of the memory that hold the actual data\n",
2019-09-19 16:03:10 +02:00
" - the concept by which Python manages the memory for us\n",
" - can be classified into objects of the same **type** (i.e., same abstract \"structure\" but different concrete data)\n",
2020-01-28 10:48:37 +01:00
" - built-in objects (incl. **literals**) vs. user-defined objects (cf., Chapter 10)\n",
2019-09-19 16:03:10 +02:00
" - e.g., `1`, `1.0`, and `\"one\"` are three different objects of distinct types that are also literals (i.e., by the way we type them into the command line Python knows what the value and type are)\n",
"\n",
"\n",
"- variables\n",
" - storage of intermediate **state**\n",
2019-11-20 11:00:24 +01:00
" - are **names** referencing **objects** in **memory**\n",
" - e.g., `x = 1` creates the variable `x` that references the object `1`\n",
2019-09-19 16:03:10 +02:00
"\n",
"\n",
"- operators\n",
" - special built-in symbols that perform operations with objects in memory\n",
2019-10-14 14:09:28 +02:00
" - usually, operate with one or two objects\n",
2019-11-20 11:00:24 +01:00
" - e.g., addition `+`, subtraction `-`, multiplication `*`, and division `/` all take two objects, whereas the negation `-` only takes one\n",
2019-09-19 16:03:10 +02:00
"\n",
"\n",
"- expressions\n",
" - **combinations** of **variables** (incl. **literals**) and **operators**\n",
2019-10-14 14:09:28 +02:00
" - do *not* change the involved objects/state of the program\n",
2019-09-19 16:03:10 +02:00
" - evaluate to a **value** (i.e., the \"result\" of the expression, usually a new object)\n",
" - e.g., `x + 2` evaluates to the (new) object `3` and `1 - 1.0` to `0.0`\n",
"\n",
"\n",
"- statements\n",
" - instructions that **\"do\" something** and **have side effects** in memory\n",
2019-11-20 11:00:24 +01:00
" - (re-)assign names to (different) objects, *change* an existing object *in place*, or, more conceptually, *change* the state of the program\n",
2019-10-14 14:09:28 +02:00
" - usually, work with expressions\n",
2019-11-20 11:00:24 +01:00
" - e.g., the assignment statement `=` makes a name reference an object\n",
2019-09-19 16:03:10 +02:00
"\n",
"\n",
"- comments\n",
" - **prose** supporting a **human's understanding** of the program\n",
" - ignored by Python\n",
"\n",
"\n",
2020-02-02 16:31:31 +01:00
"- functions (cf., [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb))\n",
2019-09-19 16:03:10 +02:00
" - named sequences of instructions\n",
" - the smaller parts in a larger program\n",
" - make a program more modular and thus easier to understand\n",
2019-11-20 11:00:24 +01:00
" - include [built-in functions](https://docs.python.org/3/library/functions.html) like [print()](https://docs.python.org/3/library/functions.html#print), [sum()](https://docs.python.org/3/library/functions.html#sum), or [len()](https://docs.python.org/3/library/functions.html#len)\n",
2019-09-19 16:03:10 +02:00
"\n",
"\n",
2020-02-02 16:31:31 +01:00
"- flow control (cf., [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb))\n",
2019-10-01 17:47:45 +02:00
" - expression of **business logic** or an **algorithm**\n",
2019-11-06 11:10:29 +01:00
" - conditional execution of parts of a program (e.g., `if` statements)\n",
2019-10-07 22:31:06 +02:00
" - repetitive execution of parts of a program (e.g., `for`-loops)"
2019-09-19 16:03:10 +02:00
]
2020-01-28 10:48:37 +01:00
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## Further Resources"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"This PyCon 2015 talk by [Ned Batchelder](https://nedbatchelder.com/), a well-known Pythonista and the organizer of the [Python User Group](https://www.meetup.com/bostonpython/) in Boston, summarizes all situations where some sort of assignment is done in Python. The content is intermediate, and, thus, it might be worthwhile to come back to this talk at a later point in time. However, the contents should be known by everyone claiming to be proficient in Python."
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2MBERISGBUYLxoaL2NCOEJjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY//AABEIAWgB4AMBIgACEQEDEQH/xAAbAAEAAgMBAQAAAAAAAAAAAAAAAQMCBAUHBv/EAEgQAAIBAwEEBAwDBQUHBQEAAAABAgMEERIFITFRE0Fh0RUWIjM0VXFzgZGTsQYUMkJSVHKhByNTwfBigpKi0uHiJGOUsvFD/8QAFwEBAQEBAAAAAAAAAAAAAAAAAAECA//EACIRAQACAwACAwEAAwAAAAAAAAABAhESEwMhMVFhQSMysf/aAAwDAQACEQMRAD8A8/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzpRUqiT4AYA2ugh2/MzpWcq89FGlUqS/dgm3/QDSB1PAt76vu/oy7h4FvfV939GXcBywdTwLe+r7v6Mu4eBb31fd/Rl3AcsHU8C3vq+7+jLuHgW99X3f0ZdwHLB1PAt7/AXf0Zdw8C3v8AAXf0ZdwHLB1PAt76vu/oy7h4FvfV939GXcBywdTwLe+r7v6Mu4eBb31fd/Rl3AcsHU8C3vq+7+jLuHgW99X3f0ZdwHLB1PAt76vu/oy7h4Fvf4C7+jLuA5YOp4FvfV939GXcPAt76vu/oy7gOWDqeBb3+Au/oy7h4FvfV939GXcBywdTwLe+r7v6Mu4eBb31fd/Rl3AcsHU8C3v8Bd/Rl3DwLe/wF39GXcBywdTwLe+r7v6Mu4eBb31fd/Rl3AcsHU8C3vq+7+jLuHgW99X3f0ZdwHLB1PAt76vu/oy7h4FvfV939GXcBywdTwLe/wABd/Rl3DwLe+r7v6Mu4Dlg6ngW99X3f0Zdw8C3vq+7+jLuA5YOp4FvfV939GXcPAt76vu/oy7gOWDcqWnRTcKlOcJrjGSaaMeghyYGqDKaUZyS4JmIAAAAAAAAAAAAAAAAAAAAAALKHno/H7FZZQ87H4gbcIOc4wj+qTSXtPWdj7MobKs4W9CKT/bn1zfNnlll6db+9j90ewR4GZGZJAIJBAAkEACQQSAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMZptxaeMPPEyAHI2ts5XFlUqVYU6lSlFyjJpN43trev6HnW1K9vXqUnbQ0qMFF+Qo7/gerXnoVx7qX2Z47PiaqNGr52XtMDOr52XtMAAAAAAAAAAAAAAAAAAAAAAAWUPOorLKHnV8QOjZenW/vY/dHsEeB4/ZenW/vY/dHsEeBmywzPmLTbVzV2+nK4g7C4q1LejTwsxlBLEs8fKakfS1IudKUFJxcotKS4rtOMvwts6FpQpUqcadajKM43EYR6RuLzlvAhC2/EDuLm7pxtY6bbXqSrLpXp/8Abx19W81Kv4iqXewa97CkqUYypqLoXUZSWqSWH5PkvfvWDoeA1K//ADlW+uJ1YxnGk9MF0ertUcvHVnJTL8M0alK6Va7rVKl04a6jjCL8h5W5JIehje/iKrb3NSlRsoVVTrwt253GhucsYwtL3b+JFx+JalCVxJ7OlK3trhUa1VVluzjDSxv4o0rmx2r4Vubm3o1oXMqy6KemlKloWEm5SepblvSOtW2FRrWt7QlVqKN3X6aTWPJeY7l2eSijWv8A8SVLOrfKOzZ1aVjOCrVFWS3SS3pY3vfw/qWeMToq9/PWU7d2tJVsKopucXuXseeouudhUbmntGEqtRK/cXPGPJ044fIzuNi211WuZ15TlG5oKhOPDcm2mu3eQc+H4spu1vKjoU5VLakq2mjcxqRlFvH6ktzXLBc9v11+cp1NnulWoW35inF1U9UeG/k93DeXT2G6uz7izuNoXFanWgoJyjBOKz1YSz8S6psijVu6tec5t1bb8tKO7GnLefbvAbEvbq+2ZQuLuhClOpCMlonlSTSeez2GqvxA2o0/yj/NSvHauj0nDG/VnHDTv4G9sywezrOFt+Zq14U0owdRRzGKWEtyXIqWxrdbbe1FKfSuONGfJ1YS1Y54WANZfiCK2xCwnSo4qVJU4yp3UZyTSz5UEtyftNatt+4uNlX1xCyq0aVCM100a0U3KMsYjufV147DZs/w1RtKlu4XdxKnbVXVpU5acRbznfjL49bL/AlHwRcbO6Wp0ddzbluytUsso1dq/iDwVGDnSpVIqnGctV1GNTfyhjMv6E334gq21e7p0NnyrxtKcatSfTKPkNZ5cdz3E3f4ao3VS6l+buKcbqEY1oQ04lpWE8tZXDgbNTYtGpK+k6tT/wBbRjRnw8lKLWV8x6Gt4Zu6m3aNpb2kZ287eNZylUUZJNpZ+HIz2Pt2W1a9WMbaFOFPKea6dSLTx5UMZX9Sx7Fgr22uqV1WpVKNGNB6dLVSCecPK+xNrsVUNo/nq13WuK3RumnOMFhN5/ZSzw6yDppgJYGCKAAIAAACQUQCQBAAIAJBRAJIAAkAUXnoVx7qX2Z49Piew3noVx7qX2Z49PiaqNGr52XtMDOr52XtMAAAAAAAAAAAAAAAAAAAAAAAWUPOr4lZZQ86viB0bL06397H7o9gjwPH7L06397H7o9gjwM2VmA+DNOd7RhdxtZSl00llR0veufs3fbmjOWLWw3Aate6o2+jpqsYa3pjqeMsyhXhObjGUm1x3NE2Z6fjYBpyvaMa/QOb6TUo6cPflNr4YT+Rfl82Njp+LQVZfMnL5sbnWFgK8vmRl82Nk6/i0FeXzYy+bGx1WAry+Yy+bG51WAry+bGXzY2Xr+LAV5fNjL5jc6rAV5fMZfMbJ1WAry+Yy+Y3Ov4tBVl82MvmNzqtBVl82MvmN16rQVZfNjL5jdOq0FWXzGXzG51WElWXzGXzG51Wgqy+YyxudVoKsvmWFict1tsqvPQrj3Uvszx2fE9hvPQrj3Uvszx6fE6VaaNXzsvaYGdXzsvaYAAAAAAAAAAAAAAAAAAAAAAAsoedXxKyyh51fEDo2Xp1v72P3R7BHgeP2Xp1v72P3R7BHgZlWT4M0p2tvO7VxJf38cJS1vdue7jweeHWbr4M5tXZ/SbUhfdIlKEdKhoymt/Ht37n1b+ZiXK/y2qlGnV064atL1LsZEKFClNzjSpwnN75KKTZXd2ULtQ1VJR05/TGDzn+aLF/Zq9tJW7nojNrMkt6Sed3J9vUYckKzhPaH51yUpKn0cElwWd+d+//AC3mymmtzTXYU29v0VpChKSemChmC0bsY3Y4EWdpG0oqnGc5rLeZyb+7Ekr1KLxhp54byTn7Ps5Ubm5qzeFOeKdPOdEf1P5ybfswb4lJhIC9o4BAEZWM5WOZK3gCvp6XTdDrXSfulhqKwS2g7p1G28+TjsS/yfzINpyjHGWlncssh1IJPy47tz38Civaupc0q8ZQTgmmpw1ZTae7fue7ialPYyi466kZKOEl0eMpKSWd+9+Vx7AOnrjnGpZxnGSOkhu8uO/hv4nPWymqdSn00XGpTUW3T8pNRS3PPDdwK47Kkq1OMp0tKWW1B5b1at2ZNr+oHSp16VRNwqReG1x61/8AhMqsEm9WcLLUd7x7EaD2dKUWqVelFKrKcWqecZUk09+/9W4mns6rTuOljcwcow0xUoPduS/e7ANuN3Rl0OHLFZaoPS8Pdn4bi1Si+DT3ZNGnZXFOFrFVqUlbrGOiflbsfvcjLZtpK2pyc90pPdHOdMF+mPw/zA3QAAAAEggkAQSCiAAQCSCQILSotN1dvGqvPQrj3Uvszx2fE9
"text/html": [
"\n",
" <iframe\n",
" width=\"60%\"\n",
" height=\"300\"\n",
" src=\"https://www.youtube.com/embed/_AEJHKGk9ns\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.YouTubeVideo at 0x7f7c940bc050>"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"YouTubeVideo(\"_AEJHKGk9ns\", width=\"60%\")"
]
2019-09-19 16:03:10 +02:00
}
],
"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",
2020-01-28 10:48:37 +01:00
"version": "3.7.4"
2019-09-19 16:03:10 +02:00
},
"livereveal": {
"auto_select": "code",
"auto_select_fragment": true,
"scroll": true,
"theme": "serif"
},
"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": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "303.333px"
},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
2020-01-28 10:48:37 +01:00
"nbformat_minor": 4
2019-09-19 16:03:10 +02:00
}