intro-to-python/01_elements.ipynb

3781 lines
136 KiB
Text
Raw Normal View History

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": [
"Do you remember how you first learned to speak in your mother tounge? 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",
"\n",
"It is intuitively best to take the very mindset of a small child when learing a foreign language and we do so for learning the Python language as well. This first chapter introduces simplistic examples and we better just 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",
"\n",
2019-10-01 17:47:45 +02:00
"Consequently, if parts of this chapter do not make sense right away, let's not worry too much. Besides introducing some basics that we need to understand, it also serves as an outlook for what is to come. So, many terms and concepts used here will be covered in great detail in following chapters."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Example: Average of a Subset of Numbers"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-01 17:47:45 +02:00
"As our introductory example, we want to calculate the *average* of all *even* numbers from *one* through *ten*.\n",
2019-09-19 16:03:10 +02:00
"\n",
"While we could come up with an [analytical solution](https://math.stackexchange.com/questions/935405/what-s-the-difference-between-analytical-and-numerical-approaches-to-problems/935446#935446) (i.e., derive some equation with \"pen and paper\" from, e.g., one of [Faulhaber's formulas](https://en.wikipedia.org/wiki/Faulhaber%27s_formula)), we instead solve the task programmatically.\n",
"\n",
"We start by creating a **list** called `numbers` that holds all the individual numbers."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-10-01 17:47:45 +02:00
"To verify that something happened in our computer's memory, we simply **reference** `numbers` and observe that Python indeed knows about it."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
"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",
"The `if number % 2 == 0` may look disturbing at first sight. Both the `%` and `==` must have an unintuitive meaning here. Luckily, the **comment** in the same line after the `#` symbol has the answer: The program only does something if the current `number` is even.\n",
"\n",
2019-10-01 17:47:45 +02: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` were 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",
"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 the count of all even numbers, which is exactly what we are looking for.\n",
"\n",
2019-10-01 17:47:45 +02:00
"We also observe how 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": [
"count = 0 # initialize variables to keep track of the sum\n",
"total = 0 # so far and the count of the even numbers\n",
"\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-09-22 20:24:15 +02:00
"We do not see any **output** yet but can obtain the value of `average` by simply 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": [
"6.0"
]
},
"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-10-01 17:47:45 +02:00
"By default, Jupyter notebooks show the value of a cell's last **expression**. 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-10-01 17:47:45 +02:00
"To visualize something before the end of the cell, we use the [print()](https://docs.python.org/3/library/functions.html#print) built-in **function**."
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": [
"Outside Jupyter notebooks, the semicolon `;` is actually used as a **seperator** between several statements that would otherwise have to be on a line on their own. However, it as *not* considered good practice to use it as it makes code less readable."
]
},
{
"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-01 17:47:45 +02:00
"Python comes with many **[operators](https://docs.python.org/3/reference/lexical_analysis.html#operators)** built in: 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-01 17:47:45 +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). But we will see many exceptions from that as well.\n",
2019-09-19 16:03:10 +02:00
"\n",
2019-10-01 17:47:45 +02: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 will continue this thought further below when we compare **expressions** with **statements**.\n",
"\n",
"Let's see some examples of operators. We start with the binary `+` and the `-` operators for addition and subtraction. Binary operators are designed to resemble 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": [
"The `-` operator can be used as a unary operator as well. Then it just flips the sign of a number."
]
},
{
"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-10-01 17:47:45 +02: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$. This is a first illustration of the concept of a **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": [
"The so-called **floor division operator** `//` always rounds down to the next integer and is thus also called **integer division operator**. This is a first example of an operator we commonly do not know from high school mathematics."
]
},
{
"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-01 17:47:45 +02:00
"To obtain the remainder of a division, we use the **modulo operator** `%`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 16,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 16,
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": [
"Note that the remainder is $0$ if a number is divisable by another."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 17,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 17,
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-10-01 17:47:45 +02:00
"Modulo division is also useful if we, for example, need to get the last couple of digits of a large integer."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 18,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 18,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"123 % 10"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 19,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 19,
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-10-01 17:47:45 +02:00
"The [divmod()](https://docs.python.org/3/library/functions.html#divmod) built-in function combines the integer and modulo divisions into one operation. However, this is not an operator but a function. Also observe that [divmod()](https://docs.python.org/3/library/functions.html#divmod) returns a \"pair\" of integers."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 20,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(4, 2)"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 20,
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-01 17:47:45 +02:00
"Raising a number to a power is performed with the **exponentiation operator** `**`. This is different from the `^` operator many other programming languages use and that also exists in Python with a *different* meaning."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 21,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 21,
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-10-01 17:47:45 +02:00
"The normal [order of precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) from mathematics applies (i.e., \"PEMDAS\" rule)."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 22,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 22,
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-09-22 20:24:15 +02:00
"execution_count": 23,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 23,
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-09-22 20:24:15 +02:00
"execution_count": 24,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"81"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 24,
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": [
"Some programmers also use \"style\" conventions. For example, we can 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."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 25,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 25,
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-01 17:47:45 +02:00
"There are many more non-mathematical operators that are introduced throughout this book together with the concepts they implement. Some of these are already shown in the next section."
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",
"An **object** can be viewed as a \"bag\" of $0$s and $1$s in a distinct memory location that not only portrayes the idea of a certain **value** but also has some associated rules as to how this value is treated and may be worked with.\n",
"\n",
"An object *always* has **three** main characteristics. Let's look at the following examples and work them out."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 26,
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-09-22 20:24:15 +02:00
"execution_count": 27,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-10-01 17:47:45 +02:00
"140658972730512"
2019-09-19 16:03:10 +02:00
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 27,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(a)"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 28,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-10-01 17:47:45 +02:00
"140658972907392"
2019-09-19 16:03:10 +02:00
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 28,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(b)"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 29,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
2019-10-01 17:47:45 +02:00
"140658972586992"
2019-09-19 16:03:10 +02:00
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 29,
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-10-01 17:47:45 +02:00
"These addresses are really not that meaningful for anything other than checking if two variables actually **point** at the same object. This may be helpful as, for example, different objects in memory may of course have the same value."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 30,
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": [
2019-10-01 17:47:45 +02:00
"`a` and `d` indeed have the same value as can be checked with the **equality operator** `==`. The resulting `True` (and the `False` below) is yet another data type, a so-called **boolean**. We will look into that closely in [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals.ipynb)."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 31,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 31,
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": [
"On the contrary, `a` and `d` are different objects as can be seen with the **identity operator** `is`: they are stored at seperate addresses in the memory."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 32,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 32,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a is d"
]
},
{
"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-09-22 20:24:15 +02:00
"execution_count": 33,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 33,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 34,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 34,
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",
"Formally, we call such type-specific functionalities **methods** (to differentiate them from functions) and we will eventually fully introduce them when we talk about object-orientation in Chapter 10. For now, it suffices to know that we can access them using the **dot operator** `.`. Of course `b` could be converted into an `int`, which the boolean value `True` tells us.\n",
"\n",
"Also note how the `.` operator is neiter a unary nor a binary operator as specified above."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 35,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 35,
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": [
"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 know it is an `int` to begin with. This is why we see the `AttributeError` below as `a` does not even know what `is_integer()` means."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 36,
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-09-22 20:24:15 +02:00
"\u001b[0;32m<ipython-input-36-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-09-22 20:24:15 +02:00
"The `c` object is a so-called **string** type (i.e., `str`), which we can view as Python's way of representing \"text\". Strings also come with their own behaviors, for example, to convert a text to lower or upper case."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 37,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 37,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(c)"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 38,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'python rocks'"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 38,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.lower()"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 39,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'PYTHON ROCKS'"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 39,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.upper()"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 40,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Python Rocks'"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 40,
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": [
"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 memory mean to us humans. A machine does not really see beyond the $0$s and $1$s. At least, not yet.\n",
"\n",
"For built-in data types, Python prints out the object's value in a so-called **[literal](https://docs.python.org/3/reference/lexical_analysis.html#literals)** notation. This basically means that we can just copy & paste the output back into a code cell to create a *new* object with the *same* value."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 41,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"789"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 41,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 42,
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": 42,
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-09-22 20:24:15 +02: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 output for `str` objects. Both types of quotes can be used interchangebly."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 43,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Python rocks'"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 43,
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",
"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 read by Python."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 44,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "SyntaxError",
2019-09-22 20:24:15 +02:00
"evalue": "invalid syntax (<ipython-input-44-cafa82e54b9c>, line 1)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-09-22 20:24:15 +02:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-44-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": [
"Python requires certain symbols at certain places (e.g., a `:` is missing here) ..."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 45,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "SyntaxError",
2019-09-22 20:24:15 +02:00
"evalue": "invalid syntax (<ipython-input-45-499e4d0d0cbb>, line 1)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-09-22 20:24:15 +02:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-45-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-09-22 20:24:15 +02:00
"... and relies on whitespace (i.e., indentation) unlike many other programming languages. An `IndentationError` is just a special type of a `SyntaxError`."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 46,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "IndentationError",
2019-09-22 20:24:15 +02:00
"evalue": "expected an indented block (<ipython-input-46-19398c5f89de>, line 2)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-09-22 20:24:15 +02:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-46-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": [
"Syntax errors as above are easy to find as the code will not even run to begin with.\n",
"\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",
"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`."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 47,
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-09-22 20:24:15 +02:00
"\u001b[0;32m<ipython-input-47-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-09-22 20:24:15 +02:00
"So-called **semantic errors**, on the contrary, can be very 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 know the answer already and can thus check 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-09-22 20:24:15 +02:00
"The cell below copies our introductory 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-09-22 20:24:15 +02:00
"execution_count": 48,
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-09-22 20:24:15 +02:00
"execution_count": 49,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 49,
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-09-22 20:24:15 +02:00
"Finding errors in a systematic way 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-09-22 20:24:15 +02:00
"Adhering to just syntax rules is therefore *never* enough. Over time, **best practices** and common **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 and un-readable code will still run. 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",
"An easier to read version of PEP 8 can be found [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."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 50,
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": [
2019-10-01 17:47:45 +02:00
"<IPython.lib.display.YouTubeVideo at 0x7fedb8113c50>"
2019-09-19 16:03:10 +02:00
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 50,
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": [
2019-10-01 17:47:45 +02:00
"For example, while the above code to calculate the average of the even numbers from 1 through 10 is correct, a Pythonista would re-write 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 (cf., [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions.ipynb)) as well as a so-called **list comprehension** (cf., Chapter 7). Pythonic code runs faster in many cases and is less error prone."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 51,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 52,
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-09-22 20:24:15 +02:00
"execution_count": 53,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6, 8, 10]"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 53,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evens"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 54,
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-09-22 20:24:15 +02:00
"execution_count": 55,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"6.0"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 55,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"To get a rough overview of the mindsets of a typical Python programmer, check these rules by an early Python core developer that are deemed so important that they are actually included in every Python installation."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 56,
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",
"That means, for example, that a variable defined towards the bottom could accidently be referenced at the top of the notebook. This happens easily if we iteratively built a program and go back and forth between cells.\n",
"\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": [
"While this book is built with Jupyter notebooks, it is important to understand that \"real\" programs are almost always just \"linear\" (= top to bottom) sequences of instructions but instead can take many different **flows of execution**.\n",
"\n",
"At the same time, for a beginner's course it is often easier to just code in a linear fashion.\n",
"\n",
2019-10-01 17:47:45 +02:00
"In real data science projects one would probably employ a mixed approach and put re-usable 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.ipynb)) and then use Jupyter notebooks to built up a linear report or story line 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-09-22 20:24:15 +02: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)** point to an object in memory.\n",
"\n",
"We will 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* refers to the combination of a *name* and a *pointer* to some object in memory."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 57,
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-09-22 20:24:15 +02:00
"When referenced, a variable evaluates to the value of the object it points to. Colloquially, we could say that `a` evaluates to `20.0` here but this would not be a full description of what is really going on in memory. We will see some more colloquial jargons 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-09-22 20:24:15 +02:00
"execution_count": 58,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"20.0"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 58,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"A variable can 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 can be mitigated with third-party libraries."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 59,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
2019-09-22 20:24:15 +02:00
"a = 20 # this makes a point to an object of a different type"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 60,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
2019-09-22 20:24:15 +02: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-09-22 20:24:15 +02:00
"If we want to re-assign a variable while referencing its \"old\" (i.e., current) object, we can also **update** it using a so-called **[augmented assignment statement](https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements)** (*not* operator), originally introduced with [PEP 203](https://www.python.org/dev/peps/pep-0203/). This implicitly inserts the currently mapped object as the first operand on the right-hand side."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 61,
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-09-22 20:24:15 +02:00
"execution_count": 62,
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-09-22 20:24:15 +02:00
"execution_count": 63,
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-09-22 20:24:15 +02:00
"execution_count": 64,
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": 64,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Variables can be **[de-referenced](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement)** (i.e., \"deleted\") with the `del` statement. This does *not* delete the object to which a variable points to. It 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-09-22 20:24:15 +02:00
"execution_count": 65,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"789"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 65,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 66,
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": [
"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 just uses it more often in its error messages."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 67,
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-09-22 20:24:15 +02:00
"\u001b[0;32m<ipython-input-67-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": [
"Some variables magically exist when we start a Python process or are added by Jupyter. We can safely ignore the former until Chapter 10 and the latter for good."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 68,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'__main__'"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 68,
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-09-22 20:24:15 +02:00
"execution_count": 69,
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-09-19 16:03:10 +02:00
" '_27',\n",
" '_28',\n",
2019-09-22 20:24:15 +02:00
" '_29',\n",
2019-09-19 16:03:10 +02:00
" '_31',\n",
" '_32',\n",
" '_33',\n",
" '_34',\n",
2019-09-22 20:24:15 +02:00
" '_35',\n",
2019-09-19 16:03:10 +02:00
" '_37',\n",
" '_38',\n",
" '_39',\n",
" '_4',\n",
" '_40',\n",
" '_41',\n",
" '_42',\n",
2019-09-22 20:24:15 +02:00
" '_43',\n",
2019-09-19 16:03:10 +02:00
" '_49',\n",
" '_5',\n",
2019-09-22 20:24:15 +02:00
" '_50',\n",
" '_53',\n",
" '_55',\n",
" '_58',\n",
" '_60',\n",
2019-09-19 16:03:10 +02:00
" '_64',\n",
2019-09-22 20:24:15 +02:00
" '_65',\n",
" '_68',\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",
" '_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-09-22 20:24:15 +02:00
"execution_count": 69,
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": [
"It is important to understand that *several* variables can point to the *same* object in memory. This can be counter-intuitive in the beginning and lead to many hard to track down bugs.\n",
"\n",
2019-09-22 20:24:15 +02:00
"This makes `b` point to whatever object `a` is pointing to."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 70,
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-09-22 20:24:15 +02:00
"execution_count": 71,
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": 71,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 72,
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": 72,
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-09-22 20:24:15 +02:00
"For \"simple\" types like `int` or `float` this will never cause troubles.\n",
2019-09-19 16:03:10 +02:00
"\n",
"Let's \"change the value\" of `a`. Really, let's create a *new* `123` object and make `a` point to it."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 73,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"a = 123"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 74,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"123"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 74,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"`b` \"is still the same\" as before. Really, `b` still points to the same object as before."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 75,
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": 75,
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-09-22 20:24:15 +02:00
"However, if a variable points to an object of a more \"complex\" type (e.g., `list`), \"weird\" things can happen."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 76,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"x = [1, 2, 3]"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 77,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 77,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 78,
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",
"Chapter 7 discusses lists in more depth. For now, let's just view a `list` object as some sort of **container** that holds an arbitrary number of pointers 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 pointer from the global list of all names to the `x` object. Then, it follows the first pointer 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-09-22 20:24:15 +02:00
"execution_count": 79,
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": 79,
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": [
"To change the first entry in the list, we use the assignment statement `=` again. Here, this does actually *not* create a *new* variable (or overwrite an existing one) but only changes the object to which the first pointer in the `x` list points to. 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-09-22 20:24:15 +02:00
"execution_count": 80,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"x[0] = 99"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 81,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[99, 2, 3]"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 81,
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-09-22 20:24:15 +02:00
"The changes made to the object `x` is pointing to can also be seen through the `y` variable!"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 82,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[99, 2, 3]"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 82,
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",
"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 actually did not change the $0$s and $1$s in the object `a` pointed to before the assignment but created a new integer object and made `a` point to it while the `b` variable is *not* affected.\n",
"\n",
"In the second case, `x[0] = 99` creates a *new* integer object `99` and merely changes the first pointer in the `x` list.\n",
"\n",
2019-09-22 20:24:15 +02:00
"In general, the assignment statement creates (or overwrites) a variable and makes it point to whatever object is on the right-hand side *only if* the left-hand side is a *pure* name (i.e., it contains no operators like the indexing operator in the example). Otherwise, it mutates some already existing object. And we always have to expect that the latter might have more than one variable pointing at 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": [
"[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",
"> \"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-01 17:47:45 +02:00
"Variable names may contain upper and lower case letters, numbers, and underscores (\"\\_\") 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",
"Variable names are usually chosen such that they do not need any more documentation and are self-explanatory. A very common convention is to use so-called **[snake\\_case](https://en.wikipedia.org/wiki/Snake_case)**: Keep everything lowercase and use underscores to seperate words.\n",
"\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-09-22 20:24:15 +02:00
"execution_count": 83,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"pi = 3.14"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 84,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"answer_to_everything = 42"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 85,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"my_name = \"Alexander\""
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 86,
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-09-22 20:24:15 +02:00
"execution_count": 87,
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-09-22 20:24:15 +02:00
"execution_count": 88,
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-09-22 20:24:15 +02:00
"execution_count": 89,
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-09-22 20:24:15 +02:00
"execution_count": 90,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"ename": "SyntaxError",
2019-09-22 20:24:15 +02:00
"evalue": "can't assign to operator (<ipython-input-90-dfc191b6a91a>, line 1)",
2019-09-19 16:03:10 +02:00
"output_type": "error",
"traceback": [
2019-09-22 20:24:15 +02:00
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-90-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": [
"If a variable name collides with a built-in name, just add a trailing underscore."
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 91,
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": [
"Variables with leading and trailing double underscores, referred to as **dunder** in Python jargon, are used for important built-in functionalities. Do *not* use this style for custom variables unless you know exactly what you are doing!"
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 92,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'__main__'"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 92,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"__name__"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"### The big Picture"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"This PyCon 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 variable assignment is done in Python. The content is intermediate and therefore it is ok if you do not understand everything at this point. However, the contents should be known by everyone claiming to be proficient in Python."
2019-09-19 16:03:10 +02:00
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 93,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"image/jpeg": "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
"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": [
2019-10-01 17:47:45 +02:00
"<IPython.lib.display.YouTubeVideo at 0x7fedb808c518>"
2019-09-19 16:03:10 +02:00
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 93,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import YouTubeVideo\n",
"YouTubeVideo(\"_AEJHKGk9ns\", width=\"60%\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Expressions"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
2019-09-22 20:24:15 +02: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",
"In simple words, anything that can be used on the right-hand side of an assignment statement without creating a `SyntaxError` is an expression.\n",
"\n",
2019-09-22 20:24:15 +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 examples in the section on operators above were actually 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",
2019-09-22 20:24:15 +02:00
"execution_count": 94,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"123"
]
},
2019-09-22 20:24:15 +02:00
"execution_count": 94,
2019-09-19 16:03:10 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
2019-09-22 20:24:15 +02:00
"execution_count": 95,
2019-10-01 17:47:45 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 95,
"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",
"execution_count": 96,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"165"
]
},
2019-10-01 17:47:45 +02:00
"execution_count": 96,
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-01 17:47:45 +02:00
"The definition of an expression is **recursive**. So, here 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",
2019-10-01 17:47:45 +02:00
"execution_count": 97,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"4492125"
]
},
2019-10-01 17:47:45 +02:00
"execution_count": 97,
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",
2019-10-01 17:47:45 +02:00
"execution_count": 98,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
2019-10-01 17:47:45 +02:00
"execution_count": 98,
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-01 17:47:45 +02:00
"When not used as a delimiter, parentheses also constitute an operator, namely the **call operator** `()`. We have seen 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",
2019-10-01 17:47:45 +02:00
"execution_count": 99,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"104"
]
},
2019-10-01 17:47:45 +02:00
"execution_count": 99,
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": [
"Python **overloads** certain operators. For example you can not only add numbers but also strings. This is called **string concatenation**."
]
},
{
"cell_type": "code",
2019-10-01 17:47:45 +02:00
"execution_count": 100,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"greeting = \"Hi \"\n",
"audience = \"class\""
]
},
{
"cell_type": "code",
2019-10-01 17:47:45 +02:00
"execution_count": 101,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Hi class'"
]
},
2019-10-01 17:47:45 +02:00
"execution_count": 101,
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",
2019-10-01 17:47:45 +02:00
"execution_count": 102,
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 '"
]
},
2019-10-01 17:47:45 +02:00
"execution_count": 102,
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-01 17:47:45 +02:00
"A **[statement](https://docs.python.org/3/reference/simple_stmts.html)** is anything that changes the state of a program or has some other side effect. Statements do not just evaluate to a value like expressions; instead, they create or change values.\n",
2019-09-19 16:03:10 +02:00
"\n",
"Most notably of course are the `=` and `del` statements."
]
},
{
"cell_type": "code",
2019-10-01 17:47:45 +02:00
"execution_count": 103,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"a = 123"
]
},
{
"cell_type": "code",
2019-10-01 17:47:45 +02:00
"execution_count": 104,
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-01 17:47:45 +02:00
"The built-in [print()](https://docs.python.org/3/library/functions.html#print) function is regarded a \"statement\" as well. In fact, it used to be a real 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",
2019-10-01 17:47:45 +02:00
"execution_count": 105,
2019-09-19 16:03:10 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I change the display of the computer\n"
]
}
],
"source": [
"print(\"I change the display of the computer\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Comments"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"We can use the `#` symbol to write comments in plain English right into the code.\n",
"\n",
"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",
"\n",
"Comments can be either added at the end of a line of code (by convention seperated with two spaces) or be on a line on their own."
]
},
{
"cell_type": "code",
2019-10-01 17:47:45 +02:00
"execution_count": 106,
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": [
"But let's think wisely if we really need to use a comment.\n",
"The second cell is a lot more \"Pythonic\"."
]
},
{
"cell_type": "code",
2019-10-01 17:47:45 +02:00
"execution_count": 107,
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",
2019-10-01 17:47:45 +02:00
"execution_count": 108,
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": [
"We end each chapter with a summary of the main points. The essence in this first chapter is that just like a sentence in a real language like English can be decomposed into its parts (subject, predicate, objects, ...) the same can be done with programming languages."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"- 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",
" - (numeric) data from a CSV file\n",
" - text entered on a command line\n",
" - (relational) data obtained from a database\n",
" - ...\n",
"\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",
" - ...\n",
"\n",
"\n",
"- objects\n",
" - distinct and well-contained areas / parts of the memory that hold the actual data\n",
" - 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",
" - built-in objects (incl. **literals**) vs. user-defined objects (cf., Chapter 8)\n",
" - 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",
" - **names** that point to **objects** in **memory**\n",
" - e.g., `x = 1` creates the variable `x` that points to the object `1`\n",
"\n",
"\n",
"- operators\n",
" - special built-in symbols that perform operations with objects in memory\n",
" - usually operate with one or two objects\n",
" - e.g., addition `+`, subtraction `-`, multiplication `*`, and division `/` all take two objects whereas the negation `-` only takes one\n",
"\n",
"\n",
"- expressions\n",
" - **combinations** of **variables** (incl. **literals**) and **operators**\n",
" - do *not* change the involved objects / state of the program\n",
" - 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",
" - re-map names to different objects and *change* the state of the program\n",
" - usually work with expressions\n",
" - e.g., the assignment statement `=` makes a name point to an object\n",
"\n",
"\n",
"- comments\n",
" - **prose** supporting a **human's understanding** of the program\n",
" - ignored by Python\n",
"\n",
"\n",
2019-10-01 17:47:45 +02:00
"- functions (cf., [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions.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",
"\n",
"\n",
2019-10-01 17:47:45 +02:00
"- flow control (cf., [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals.ipynb))\n",
" - expression of **business logic** or an **algorithm**\n",
2019-09-22 20:24:15 +02:00
" - conditional execution of a small **branch** within a program (i.e., `if` statements)\n",
2019-09-19 16:03:10 +02:00
" - repetitive execution of parts of a program (i.e., `for`-loops and `while`-loops)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"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,
"nbformat_minor": 2
}