Rework the introduction on numpy

- expand section on indexing and slicing in Chapter 1 on arrays
- add section on dimensionality and shapes of arrays
- streamline text in Chapter 1
- include thumbnails in links to scientific libraries
- add explanation on slicing to core Python introduction
  and adjust the list example
- streamline some text and titles in Chapter 0
This commit is contained in:
Alexander Hess 2021-10-04 13:55:07 +02:00
parent 3133540e45
commit 46e8b9e174
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
8 changed files with 1576 additions and 720 deletions

View file

@ -36,7 +36,7 @@
"\n", "\n",
"The syntax to create a `list` are brackets, `[` and `]`, another example of delimiters, listing the individual **elements** of the `list` in between them, separated by commas.\n", "The syntax to create a `list` are brackets, `[` and `]`, another example of delimiters, listing the individual **elements** of the `list` in between them, separated by commas.\n",
"\n", "\n",
"For example, the next code snippet creates a `list` named `numbers` with the numbers `1`, `2`, `3`, and `4` in it." "For example, the next code snippet creates a `list` named `numbers` with the numbers `1`, `2`, `3`, `4`, and `5` in it."
] ]
}, },
{ {
@ -47,7 +47,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[1, 2, 3, 4]" "[1, 2, 3, 4, 5]"
] ]
}, },
"execution_count": 1, "execution_count": 1,
@ -56,7 +56,7 @@
} }
], ],
"source": [ "source": [
"numbers = [1, 2, 3, 4]\n", "numbers = [1, 2, 3, 4, 5]\n",
"\n", "\n",
"numbers" "numbers"
] ]
@ -65,7 +65,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Whenever we use any kind of delimiter, we may break the lines in between them as we wish and add other so-called **whitespace** characters like spaces to format the way the code looks like. So, the following two code cells do *exactly* the same as the previous one, even the `,` after the `4` in the second cell is ignored." "Whenever we use any kind of delimiter, we may break the lines in between them as we wish and add other so-called **whitespace** characters like spaces to format the way the code looks like. So, the following two code cells do *exactly* the same as the previous one, even the `,` after the `5` in the second cell is ignored."
] ]
}, },
{ {
@ -76,7 +76,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[1, 2, 3, 4]" "[1, 2, 3, 4, 5]"
] ]
}, },
"execution_count": 2, "execution_count": 2,
@ -86,7 +86,7 @@
], ],
"source": [ "source": [
"numbers = [\n", "numbers = [\n",
" 1, 2, 3, 4\n", " 1, 2, 3, 4, 5\n",
"]\n", "]\n",
"\n", "\n",
"numbers" "numbers"
@ -100,7 +100,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[1, 2, 3, 4]" "[1, 2, 3, 4, 5]"
] ]
}, },
"execution_count": 3, "execution_count": 3,
@ -114,6 +114,7 @@
" 2,\n", " 2,\n",
" 3,\n", " 3,\n",
" 4,\n", " 4,\n",
" 5,\n",
"]\n", "]\n",
"\n", "\n",
"numbers" "numbers"
@ -135,6 +136,13 @@
"num" "num"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Indexing & Slicing"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -183,7 +191,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"4" "5"
] ]
}, },
"execution_count": 5, "execution_count": 5,
@ -210,7 +218,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"numbers[0] = 4" "numbers[0] = 5"
] ]
}, },
{ {
@ -219,7 +227,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"numbers[3] = 1" "numbers[4] = 1"
] ]
}, },
{ {
@ -230,7 +238,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[4, 2, 3, 1]" "[5, 2, 3, 4, 1]"
] ]
}, },
"execution_count": 8, "execution_count": 8,
@ -255,7 +263,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"numbers[0], numbers[3] = numbers[3], numbers[0]" "numbers[0], numbers[4] = numbers[4], numbers[0]"
] ]
}, },
{ {
@ -266,7 +274,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[1, 2, 3, 4]" "[1, 2, 3, 4, 5]"
] ]
}, },
"execution_count": 10, "execution_count": 10,
@ -278,6 +286,174 @@
"numbers" "numbers"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As a generalization, we may also **slice** out some elements in the `list`. That is done with the `[...]` notation as well. Yet, instead of a single integer index, we now provide a *start* and a *stop* index separated by a `:`. While the element corresponding to the *start* index is included, this is not the case for *stop*.\n",
"\n",
"For example, to slice out the middle three elements, we write the following."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 4]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[1:4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We may combine positive and negative indexes.\n",
"\n",
"So, the following yields the same result."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 4]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[1:-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While ommitting the *start* index makes a slice begin at the first element, ..."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[:-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... leaving out the *stop* index makes a slice go to the last element."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 4, 5]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[1:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Providing a third integer as the *step* value after another `:` makes a slice skip some elements.\n",
"\n",
"For example, `[1:-1:2]` means \"go from the second element (including) to the last element (excluding) and take every second element\" ..."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[1:-1:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... while `[::2]` simply downsamples the `list` by taking every other element."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 3, 5]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numbers[::2]"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -333,21 +509,21 @@
"\n", "\n",
"Many beginners struggle with the term \"loop.\" To visualize the looping behavior of this code, we use the online tool [PythonTutor <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](http://pythontutor.com/visualize.html#code=numbers%20%3D%20%5B1,%202,%203,%204%5D%0A%0Atotal%20%3D%200%0A%0Afor%20number%20in%20numbers%3A%0A%20%20%20%20total%20%3D%20total%20%2B%20number%0A%0Atotal&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false). That tool is helpful for two reasons:\n", "Many beginners struggle with the term \"loop.\" To visualize the looping behavior of this code, we use the online tool [PythonTutor <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](http://pythontutor.com/visualize.html#code=numbers%20%3D%20%5B1,%202,%203,%204%5D%0A%0Atotal%20%3D%200%0A%0Afor%20number%20in%20numbers%3A%0A%20%20%20%20total%20%3D%20total%20%2B%20number%0A%0Atotal&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false). That tool is helpful for two reasons:\n",
"1. It allows us to execute code in \"slow motion\" (i.e., by clicking the \"next\" button on the left side, only the next atomic step of the code snippet is executed).\n", "1. It allows us to execute code in \"slow motion\" (i.e., by clicking the \"next\" button on the left side, only the next atomic step of the code snippet is executed).\n",
"2. It shows what happens inside the computer's memory on the right-hand side (cf., the \"*Thinking like a Computer*\" section further below)." "2. It shows what happens inside the computer's memory on the right-hand side."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 17,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"10" "15"
] ]
}, },
"execution_count": 11, "execution_count": 17,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -370,16 +546,16 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 18,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"10" "15"
] ]
}, },
"execution_count": 12, "execution_count": 18,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -395,16 +571,16 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 19,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"10" "15"
] ]
}, },
"execution_count": 13, "execution_count": 19,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -434,7 +610,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 20,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -443,7 +619,7 @@
"1" "1"
] ]
}, },
"execution_count": 14, "execution_count": 20,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -454,7 +630,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 21,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -463,7 +639,7 @@
"0" "0"
] ]
}, },
"execution_count": 15, "execution_count": 21,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -481,7 +657,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 22,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -490,7 +666,7 @@
"False" "False"
] ]
}, },
"execution_count": 16, "execution_count": 22,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -501,7 +677,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 23,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -510,7 +686,7 @@
"True" "True"
] ]
}, },
"execution_count": 17, "execution_count": 23,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -544,7 +720,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 24,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -553,7 +729,7 @@
"6" "6"
] ]
}, },
"execution_count": 18, "execution_count": 24,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -579,21 +755,21 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"`if` statements may have more than one header line: For example, the code in the `else`-clause's body is only executed if the condition in the `if`-clause is `False`. In the code cell below, we calculate the sum of all even numbers and subtract the sum of all odd numbers. The result is `(2 + 4) - (1 + 3)`, or `-1 + 2 - 3 + 4` resembling the order of the numbers in the `for`-loop." "`if` statements may have more than one header line: For example, the code in the `else`-clause's body is only executed if the condition in the `if`-clause is `False`. In the code cell below, we calculate the sum of all even numbers and subtract the sum of all odd numbers. The result is `(2 + 4) - (1 + 3 + 5)`, or `-1 + 2 - 3 + 4 - 5` resembling the order of the numbers in the `for`-loop."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 25,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"2" "-3"
] ]
}, },
"execution_count": 19, "execution_count": 25,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -621,7 +797,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 26,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -658,7 +834,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 27,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -706,7 +882,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 28,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -747,7 +923,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 29,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -788,7 +964,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 30,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {

View file

@ -29,7 +29,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## User-defined Functions" "## Defining Functions"
] ]
}, },
{ {
@ -151,7 +151,7 @@
"traceback": [ "traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/user/1000/ipykernel_305654/1049141082.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/tmp/user/1000/ipykernel_707190/1049141082.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'result' is not defined" "\u001b[0;31mNameError\u001b[0m: name 'result' is not defined"
] ]
} }
@ -393,7 +393,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Extending Core Python with the Standard Library" "## The Standard Library"
] ]
}, },
{ {
@ -429,7 +429,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"0.44374384200665107" "0.7021021034327006"
] ]
}, },
"execution_count": 16, "execution_count": 16,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

BIN
static/link/to_np.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
static/link/to_pd.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 862 B

BIN
static/link/to_plt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
static/link/to_skl.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 837 B