Streamline previous content
This commit is contained in:
parent
84e08d06f4
commit
250aa09209
7 changed files with 181 additions and 104 deletions
|
|
@ -24,7 +24,7 @@
|
|||
"\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",
|
||||
"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 referenced here will be covered in great detail in following chapters."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -46,7 +46,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"As our introductory example, we want to calculate the average of all even numbers from one through ten.\n",
|
||||
"As our introductory example, we want to calculate the *average* of all *even* numbers from *one* through *ten*.\n",
|
||||
"\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",
|
||||
|
|
@ -75,7 +75,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"To verify that something happened in our computer's memory, we simply **reference** `numbers` and observe that Python indeed knows about."
|
||||
"To verify that something happened in our computer's memory, we simply **reference** `numbers` and observe that Python indeed knows about it."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -110,17 +110,17 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"So far, so good. Let's see how the desired result could be expressed as a **sequence of instructions** in Python.\n",
|
||||
"So far, so good. Let's see how the desired **computation** could be expressed as a **sequence of instructions** in Python.\n",
|
||||
"\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",
|
||||
"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 `=` 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",
|
||||
"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",
|
||||
"\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",
|
||||
"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."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -200,7 +200,7 @@
|
|||
"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",
|
||||
"By default, Jupyter notebooks show the value of a cell's last so-called **expression**. This output can be suppressed by ending the last line with a semicolon `;`."
|
||||
"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 `;`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -249,7 +249,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"To visualize something before the end of the cell, we can use the [print()](https://docs.python.org/3/library/functions.html#print) built-in **function**."
|
||||
"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**."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -327,11 +327,13 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Python comes with basic mathematical operators built in. **[Operators](https://docs.python.org/3/reference/lexical_analysis.html#operators)** are built-in **tokens** that have a special meaning to the Python interpreter. Most operators either \"operate\" with the object immediately following them (= **unary** operators; e.g., negation) or somehow \"process\" the two objects \"around\" them (= **binary** operators; e.g., addition). But we will see some exceptions from that as well.\n",
|
||||
"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",
|
||||
"\n",
|
||||
"By definition, operators have **no** permanent **side effects** in the computer's memory. Although the code cells in this section do indeed lead to *new* objects being created in memory, they are immediately \"forgotten\" as they are not stored in a **variable** (like `numbers` above). We will revisit this idea further below when we compare **expressions** with **statements**.\n",
|
||||
"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",
|
||||
"\n",
|
||||
"Let's see some examples of operators. We start with the binary `+` and the `-` operators for addition and subtraction. Binary operators resemble what mathematicians call [infix notation](https://en.wikipedia.org/wiki/Infix_notation) and have the expected meaning."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -425,7 +427,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"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**."
|
||||
"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**."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -543,7 +545,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"To obtain the remainder of a division, we can use the **modulo operator** `%`."
|
||||
"To obtain the remainder of a division, we use the **modulo operator** `%`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -613,7 +615,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Modulo division can be useful if we, for example, need to get the last couple of digits of a large integer."
|
||||
"Modulo division is also useful if we, for example, need to get the last couple of digits of a large integer."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -672,7 +674,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"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 any more (but a function). Also observe that [divmod()](https://docs.python.org/3/library/functions.html#divmod) returns a \"pair\" of integers."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -707,7 +709,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Raising a number to a power is performed with the **exponentiation operator** `**`. Note that this is different from the `^` operator many other programming languages might use and that also exists in Python with a *different* meaning."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -742,7 +744,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The normal [order of precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) from mathematics applies (i.e., \"PEMDAS\" rule) but parentheses help avoid confusion."
|
||||
"The normal [order of precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) from mathematics applies (i.e., \"PEMDAS\" rule)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -777,7 +779,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The parentheses here serve as a **delimiter**."
|
||||
"Parentheses help avoid confusion and take the role of a **delimiter** here."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -871,7 +873,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"There are plenty more mathematical and non-mathematical operators that are introduced throughout this book together with the concepts they implement or support. Some of these are already shown in the next section."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -949,7 +951,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139940106427216"
|
||||
"140658972730512"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
|
|
@ -973,7 +975,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139940106602344"
|
||||
"140658972907392"
|
||||
]
|
||||
},
|
||||
"execution_count": 28,
|
||||
|
|
@ -997,7 +999,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139940105769456"
|
||||
"140658972586992"
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
|
|
@ -1017,7 +1019,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"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 different objects can of course have the same value."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1041,7 +1043,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"`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."
|
||||
"`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)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1780,7 +1782,7 @@
|
|||
" "
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7f46584be518>"
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7fedb8113c50>"
|
||||
]
|
||||
},
|
||||
"execution_count": 50,
|
||||
|
|
@ -1801,7 +1803,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"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) as well as a so-called **list comprehension** (cf., Chapter 7). Pythonic code runs faster in many cases and is less error prone."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -2003,7 +2005,7 @@
|
|||
"\n",
|
||||
"At the same time, for a beginner's course it is often easier to just code in a linear fashion.\n",
|
||||
"\n",
|
||||
"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) and then use Jupyter notebooks to built up a linear report or story line for a business argument to be made."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -2868,7 +2870,7 @@
|
|||
"\n",
|
||||
"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",
|
||||
"\n",
|
||||
"In the beginning, visualizing 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%0Adel%20x,%20y%0Ax%20%3D%20%5B1,%202,%203%5D%0Ay%20%3D%20x.copy%28%29%0Ax%5B0%5D%20%3D%2099&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) will assist in understanding what is going on."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -2903,7 +2905,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"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 **[keywords](https://docs.python.org/3/reference/lexical_analysis.html#keywords)**.\n",
|
||||
"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",
|
||||
"\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",
|
||||
|
|
@ -3150,7 +3152,7 @@
|
|||
" "
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7f465852de48>"
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7fedb808c518>"
|
||||
]
|
||||
},
|
||||
"execution_count": 93,
|
||||
|
|
@ -3188,7 +3190,7 @@
|
|||
"\n",
|
||||
"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",
|
||||
"\n",
|
||||
"The simplest possible expression contains only one variable (or literal)."
|
||||
"The simplest possible expressions contain only one variable or literal."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -3218,6 +3220,33 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 95,
|
||||
"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,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
|
|
@ -3230,7 +3259,7 @@
|
|||
"165"
|
||||
]
|
||||
},
|
||||
"execution_count": 95,
|
||||
"execution_count": 96,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
|
@ -3247,12 +3276,12 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"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."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 96,
|
||||
"execution_count": 97,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
|
|
@ -3265,7 +3294,7 @@
|
|||
"4492125"
|
||||
]
|
||||
},
|
||||
"execution_count": 96,
|
||||
"execution_count": 97,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
|
@ -3282,12 +3311,12 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Here, the variable `y` is combined with the literal `2` by the indexing operator `[]`. The resulting expression evaluates to the "
|
||||
"Here, the variable `y` is combined with the literal `2` by the indexing operator `[]`. The resulting expression evaluates to the third element in the `y` list."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 97,
|
||||
"execution_count": 98,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
|
|
@ -3300,7 +3329,7 @@
|
|||
"3"
|
||||
]
|
||||
},
|
||||
"execution_count": 97,
|
||||
"execution_count": 98,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
|
@ -3317,12 +3346,12 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"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."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 98,
|
||||
"execution_count": 99,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
|
|
@ -3335,7 +3364,7 @@
|
|||
"104"
|
||||
]
|
||||
},
|
||||
"execution_count": 98,
|
||||
"execution_count": 99,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
|
@ -3368,7 +3397,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 99,
|
||||
"execution_count": 100,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
|
|
@ -3382,7 +3411,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 100,
|
||||
"execution_count": 101,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
|
|
@ -3395,7 +3424,7 @@
|
|||
"'Hi class'"
|
||||
]
|
||||
},
|
||||
"execution_count": 100,
|
||||
"execution_count": 101,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
|
@ -3417,7 +3446,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 101,
|
||||
"execution_count": 102,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
|
|
@ -3430,7 +3459,7 @@
|
|||
"'Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi '"
|
||||
]
|
||||
},
|
||||
"execution_count": 101,
|
||||
"execution_count": 102,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
|
@ -3458,14 +3487,14 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"A **[statement](https://docs.python.org/3/reference/simple_stmts.html)** is anything that changes the state of the program's memory or has some other side effect. Statements do not just evaluate to a value like expressions; instead, they create or change values.\n",
|
||||
"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",
|
||||
"\n",
|
||||
"Most notably of course are the `=` and `del` statements."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 102,
|
||||
"execution_count": 103,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
|
|
@ -3478,7 +3507,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 103,
|
||||
"execution_count": 104,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
|
|
@ -3497,12 +3526,12 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The [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."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 104,
|
||||
"execution_count": 105,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "skip"
|
||||
|
|
@ -3549,7 +3578,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 105,
|
||||
"execution_count": 106,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
|
|
@ -3577,7 +3606,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 106,
|
||||
"execution_count": 107,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
|
|
@ -3590,7 +3619,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 107,
|
||||
"execution_count": 108,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
|
|
@ -3690,14 +3719,14 @@
|
|||
" - ignored by Python\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- functions (cf., Chapter 2)\n",
|
||||
"- functions (cf., [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions.ipynb))\n",
|
||||
" - 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",
|
||||
"- flow control (cf., Chapter 3)\n",
|
||||
" - expression of **logic** or an **algorithm**\n",
|
||||
"- 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",
|
||||
" - conditional execution of a small **branch** within a program (i.e., `if` statements)\n",
|
||||
" - repetitive execution of parts of a program (i.e., `for`-loops and `while`-loops)"
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue