Merge in "release-0.6.8"

Merge branch "release-0.6.8" into "develop"

Summary of the merged in commits:
 * 5f63322: Remove empty lines
 * a9756ea: Add video and streamline content
 * 26983f1: Add video and streamline content
 * 6d2c7ae: Add video and streamline content
 * 6e68175: Add video and streamline content
 * 0151478: Add video and streamline content
 * c126817: Add video and streamline content
 * 4171517: Add video and streamline content
 * 4d336e4: Add video and streamline content
 * 0f5b439: Bump version number
 * 5d7b2c4: Add video links to the README.md
This commit is contained in:
Alexander Hess 2020-03-30 17:45:31 +02:00
commit a49b0307a0
28 changed files with 5501 additions and 10997 deletions

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 0: Start up"
"# Chapter 0: Introduction"
]
},
{
@ -18,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 0](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 0](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -28,13 +30,6 @@
"### Essay Questions "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer the following questions *briefly*!"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -46,7 +41,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -60,7 +55,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -74,7 +69,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -88,7 +83,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -116,7 +111,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -130,7 +125,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -144,7 +139,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -158,21 +153,21 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9**: Python is *free software*. That means it does not cost anything."
"**Q9**: Python is **free software**. That means it does not cost anything."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -186,7 +181,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],
@ -206,7 +201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,

View file

@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 0: Start up"
"# Chapter 0: Introduction"
]
},
{
@ -18,7 +18,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 0](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_00_lecture.ipynb) of the book. Then, work through the exercises below."
"The exercises below assume that you have read [Chapter 0](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_introduction_00_lecture.ipynb) in the book."
]
},
{
@ -94,7 +94,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 1: Elements of a Program"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -29,13 +30,6 @@
"### Essay Questions "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer the following questions *briefly*!"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -47,7 +41,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -61,7 +55,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -75,7 +69,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -89,7 +83,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -103,7 +97,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -117,7 +111,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -145,7 +139,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -159,7 +153,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -173,7 +167,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -187,7 +181,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],
@ -207,7 +201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 1: Elements of a Program"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_content.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells."
"The exercises below assume that you have read [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb) in the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -68,7 +69,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" ..."
" < your answer >"
]
},
{
@ -100,7 +101,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" ..."
" < your answer >"
]
},
{
@ -283,7 +284,7 @@
"source": [
"`for`-loops are extremely versatile in Python. That is different from many other programming languages.\n",
"\n",
"As shown in the first example in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_content.ipynb), we can create a `list` like `numbers` and loop over the numbers in it on a one-by-one basis."
"As shown in the first example in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb#Example:-Averaging-all-even-Numbers-in-a-List), we can create a `list` like `numbers` and loop over the numbers in it on a one-by-one basis."
]
},
{
@ -444,7 +445,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,

File diff suppressed because one or more lines are too long

View file

@ -18,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -28,13 +30,6 @@
"### Essay Questions "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer the following questions *briefly*!"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -46,7 +41,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -60,7 +55,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -74,7 +69,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -88,7 +83,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -102,7 +97,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -116,7 +111,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -130,7 +125,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -158,7 +153,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -172,7 +167,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -186,7 +181,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -200,7 +195,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],
@ -220,7 +215,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,

View file

@ -18,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells."
"The exercises below assume that you have read [Chapter 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb) in the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -66,7 +68,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q2**: Encapsulate the logic into a function `sphere_volume()` that takes one *positional* argument `radius` and one *keyword-only* argument `digits` defaulting to `5`. The volume should be returned as a `float` object under *all* circumstances. Document your work appropriately in a docstring according to [Google's Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md)."
"**Q2**: Encapsulate the logic into a function `sphere_volume()` that takes one *positional* argument `radius` and one *keyword-only* argument `digits` defaulting to `5`. The volume should be returned as a `float` object under *all* circumstances."
]
},
{
@ -76,7 +78,17 @@
"outputs": [],
"source": [
"def sphere_volume(...):\n",
" ..."
" \"\"\"Calculate the volume of a sphere.\n",
"\n",
" Args:\n",
" radius (float): radius of the sphere\n",
" digits (optional, int): number of digits\n",
" for rounding the resulting volume\n",
"\n",
" Returns:\n",
" volume (float)\n",
" \"\"\"\n",
" return ..."
]
},
{
@ -151,7 +163,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -193,7 +205,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],
@ -213,7 +225,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 3: Conditionals & Exceptions"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -29,13 +30,6 @@
"### Essay Questions "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer the following questions *briefly*!"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -47,7 +41,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -61,7 +55,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -75,7 +69,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -89,7 +83,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -103,7 +97,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -129,7 +123,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -159,7 +153,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -173,7 +167,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 3: Conditionals & Exceptions"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells."
"The exercises below assume that you have read [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb) in the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -49,8 +50,28 @@
"metadata": {},
"outputs": [],
"source": [
"def discounted_price(...):\n",
" ..."
"def discounted_price(unit_price, quantity):\n",
" \"\"\"Calculate the price of a line item in an order.\n",
"\n",
" Args:\n",
" unit_price (float): price of one ordered item\n",
" quantity (int): number of items ordered\n",
"\n",
" Returns:\n",
" line_item_price (float)\n",
" \"\"\"\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" return ..."
]
},
{
@ -143,7 +164,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -182,9 +203,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q2.2**: Loop over the `numbers` list and replace numbers for which one of the two (or both) conditions apply with text strings `\"Fizz\"`, `\"Buzz\"`, or `\"FizzBuzz\"` using the indexing operator `[]` and the assignment statement `=`.\n",
"**Q2.2**: Loop over the `numbers` list and *replace* numbers for which one of the two (or both) conditions apply with text strings `\"Fizz\"`, `\"Buzz\"`, or `\"FizzBuzz\"` using the indexing operator `[]` and the assignment statement `=`.\n",
"\n",
"In [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb), we saw that Python starts indexing with `0` as the first element. Keep that in mind.\n",
"In [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb#Who-am-I?-And-how-many?), we saw that Python starts indexing with `0` as the first element. Keep that in mind.\n",
"\n",
"So in each iteration of the `for`-loop, you have to determine an `index` variable as well as check the actual `number` for its divisors.\n",
"\n",
@ -198,6 +219,13 @@
"outputs": [],
"source": [
"for number in numbers:\n",
" ...\n",
"\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ..."
]
},

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 4: Recursion & Looping"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -29,13 +30,6 @@
"### Essay Questions "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer the following questions *briefly*!"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -47,7 +41,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -61,7 +55,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -75,7 +69,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -89,7 +83,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -103,7 +97,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -117,7 +111,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -145,7 +139,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -159,7 +153,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -173,7 +167,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -187,7 +181,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -201,7 +195,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -215,7 +209,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -240,7 +234,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 4: Recursion & Looping"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells. Occasionally, the `...` mean that you have to write more than one line of code."
"The exercises below assume that you have read the \"*Recursion*\" part in [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -95,7 +96,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -109,7 +110,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -123,7 +124,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -137,7 +138,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -151,53 +152,52 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As most likely the first couple of tries will result in *semantic* errors, it is advisable to have some sort of **visualization tool** for the program's output: For example, an online version of the game can be found **[here](https://www.mathsisfun.com/games/towerofhanoi.html)**."
"As most likely the first couple of tries will result in *semantic* errors, it is advisable to have some sort of **visualization tool** for the program's output: For example, an online version of the game can be found [here](https://www.mathsisfun.com/games/towerofhanoi.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's first **generalize** the mathematical relationship from above.\n",
"Let's first **generalize** the mathematical relationship from above and then introduce the variable names used in our `sol()` implementation below.\n",
"\n",
"While the first number of $Sol(\\cdot)$ is the number of `disks` $n$, the second and third \"numbers\" are the **labels** for the three spots. Instead of spots `1`, `2`, and `3`, we could also call them `\"left\"`, `\"center\"`, and `\"right\"` in our Python implementation. When \"passed\" to the $Sol(\\cdot)$ \"function\" they take on the role of an `origin` (= $o$) and `destination` (= $d$) pair.\n",
"Unsurprisingly, the recursive relationship in the video may be generalized into:\n",
"\n",
"So, the expression $Sol(4, 1, 3)$ is the same as $Sol(4, \\text{\"left\"}, \\text{\"right\"})$ and describes the problem of moving a tower consisting of $n = 4$ disks from `origin` `1` / `\"left\"` to `destination` `3` / `right`. As we have seen in the video, we need some `intermediate` (= $i$) spot."
"$Sol(n, o, d) = Sol(n-1, o, i) ~ \\bigoplus ~ Sol(1, o, d) ~ \\bigoplus ~ Sol(n-1, i, d)$\n",
"\n",
"$Sol(\\cdot)$ takes three \"arguments\" $n$, $o$, and $d$ and is defined with *three* references to itself that take modified versions of $n$, $o$, and $d$ in different orders. The middle reference, Sol(1, o, d), constitutes the \"end\" of the recursive definition: It is the problem of solving Towers of Hanoi for a \"tower\" of only one disk.\n",
"\n",
"While the first \"argument\" of $Sol(\\cdot)$ is a number that we refer to as `n_disks` below, the second and third \"arguments\" are merely **labels** for the spots, and we refer to the **roles** they take in a given problem as `origin` and `destination` below. Instead of labeling individual spots with the numbers `1`, `2`, and `3` as in the video, we may also call them `\"left\"`, `\"center\"`, and `\"right\"`. Both ways are equally correct! So, only the first \"argument\" of $Sol(\\cdot)$ is really a number!\n",
"\n",
"As an example, the notation $Sol(4, 1, 3)$ from above can then be \"translated\" into Python as either the function call `sol(4, 1, 3)` or `sol(4, \"left\", \"right\")`. This describes the problem of moving a tower consisting of `n_disks=4` disks from either the `origin=1` spot to the `destination=3` spot or from the `origin=\"left\"` spot to the `destination=\"right\"` spot.\n",
"\n",
"To adhere to the rules, an `intermediate` spot $i$ is needed. In `sol()` below, this is a temporary variable within a function call and *not* a parameter of the function itself."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In summary, the generalized functional relationship can be expressed as:\n",
"In summary, to move a tower consisting of `n_disks` (= $n$) disks from an `origin` (= $o$) to a `destination` (= $d$), three steps must be executed:\n",
"\n",
"$Sol(n, o, d) = Sol(n-1, o, i) ~ \\bigoplus ~ Sol(1, o, d) ~ \\bigoplus ~ Sol(n-1, i, d)$"
"1. Move the tower's topmost `n_disks - 1` (= $n - 1$) disks from the `origin` (= $o$) to an `intermediate` (= $i$) spot (= **Sub-Problem 1**),\n",
"2. move the remaining and largest disk from the `origin` (= $o$) to the `destination` (= $d$), and\n",
"3. move the `n_disks - 1` (= $n - 1$) disks from the `intermediate` (= $i$) spot to the `destination` (= $d$) spot (= **Sub-Problem 2**).\n",
"\n",
"The two sub-problems themselves are solved via the same *recursive* logic."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In words, this means that to move a tower consisting of $n$ disks from an `origin` $o$ to a `destination` $d$, three steps must be executed:\n",
"Write your answers to **Q5** to **Q7** into the skeleton of `sol()` below.\n",
"\n",
"1. Move the topmost $n - 1$ disks of the tower temporarily from $o$ to $i$ (= sub-problem 1)\n",
"2. Move the remaining and largest disk from $o$ to $d$\n",
"3. Move the the $n - 1$ disks from the temporary spot $i$ to $d$ (= sub-problem 2)\n",
"`sol()` takes three arguments `n_disks`, `origin`, and `destination` that mirror $n$, $o$, and $d$ above.\n",
"\n",
"The two sub-problems can be solved via the same recursive logic."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Sol(\\cdot)$ can be written in Python as a function `sol()` that takes three arguments `disks`, `origin`, and `destination` that mirror $n$, $o$, and $d$.\n",
"For now, assume that all arguments to `sol()` are `int` objects! We generalize this into real labels further below in the `hanoi()` function.\n",
"\n",
"Assume that all arguments to `sol()` are `int` objects!\n",
"\n",
"Once completed, `sol()` should print out all the moves in the correct order. With **printing a move**, we mean a line like \"1 -> 3\", short for \"Move the top-most disk from spot 1 to spot 3\".\n",
"\n",
"Write your answers to **Q5** to **Q7** into the subsequent code cell and finalize `sol()`! No need to write a docstring or validate the input here."
"Once completed, `sol()` should *print* out all the moves in the correct order. For example, *print* `\"1 -> 3\"` to mean \"Move the top-most `n_disks - 1` disks from spot `1` to spot `3`.\""
]
},
{
@ -206,48 +206,74 @@
"metadata": {},
"outputs": [],
"source": [
"def sol(disks, origin, destination):\n",
"def sol(n_disks, origin, destination):\n",
" \"\"\"A naive implementation of Towers of Hanoi.\n",
"\n",
" This function prints out the moves to solve a Towers of Hanoi problem.\n",
"\n",
" Args:\n",
" n_disks (int): number of disks in the tower\n",
" origin (int): spot of the tower at the start; 1, 2, or 3\n",
" destination (int): spot of the tower at the end; 1, 2, or 3\n",
" \"\"\"\n",
" # answer to Q5\n",
" # ...\n",
" ...\n",
" ...\n",
"\n",
" # answer to Q6\n",
" # ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" # answer to Q7\n",
" # ..."
" ...\n",
" ...\n",
" ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q5**: What is the `disks` argument when the function reaches its **base case**? Check for the base case with a simple `if` statement and return from the function using the **early exit** pattern!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q6**: If not in the base case, `sol()` needs to determine the `intermediate` spot given concrete `origin` and `destination` arguments. For example, if called with `origin=1` and `destination=2`, `intermediate` must be `3`.\n",
"**Q5**: What is the `n_disks` argument when the function reaches its **base case**? Check for the base case with a simple `if` statement and return from the function using the **early exit** pattern!\n",
"\n",
"Add **one** compound `if` statement to `sol()` that has a branch for **every** possible `origin`-`destination` pair that sets a variable `intermediate` to the correct temporary spot. **How many** branches will there be?"
"Hint: The base case in the Python implementation may be slightly different than the one shown in the generalized mathematical relationship above!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q7**: `sol()` needs to call itself **two more times** with the correct 2-pairs chosen from the three available spots `origin`, `intermediate`, and `destination`.\n",
"**Q6**: If not in the base case, `sol()` determines the `intermediate` spot given concrete `origin` and `destination` arguments. For example, if called with `origin=1` and `destination=2`, `intermediate` must be `3`.\n",
"\n",
"In between the two recursive function calls, write a `print()` statement that prints out from where to where the \"remaining and largest\" disk has to be moved!"
"Add *one* compound `if` statement to `sol()` that has a branch for *every* possible `origin`-`destination`-pair that assigns the correct temporary spot to a variable `intermediate`.\n",
"\n",
"Hint: How many 2-tuples of 3 elements can there be if the order matters?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q8**: Execute the code cells below and confirm that the printed moves are correct!"
"**Q7**: `sol()` calls itself *two* more times with the correct 2-tuples chosen from the three available spots `origin`, `intermediate`, and `destination`.\n",
"\n",
"*In between* the two recursive function calls, use [print()](https://docs.python.org/3/library/functions.html#print) to print out from where to where the \"remaining and largest\" disk has to be moved!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q8**: Execute the code cells below and confirm that the moves are correct!"
]
},
{
@ -297,11 +323,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The previous `sol()` implementation does the job, but the conditional statement needed in unnecessarily tedious. \n",
"The previous `sol()` implementation does the job, but the conditional statement is unnecessarily tedious. \n",
"\n",
"Let's create a more concise `hanoi()` function that, in addition to a positional `disks` argument, takes three keyword-only arguments `origin`, `intermediate`, and `destination` with default values `\"left\"`, `\"center\"`, and `\"right\"`.\n",
"Let's create a concise `hanoi()` function that, in addition to a positional `n_disks` argument, takes three keyword-only arguments `origin`, `intermediate`, and `destination` with default values `\"left\"`, `\"center\"`, and `\"right\"`.\n",
"\n",
"Write your answers to **Q9** and **Q10** into the subsequent code cell and finalize `hanoi()`! No need to write a docstring or validate the input here."
"Write your answers to **Q9** and **Q10** into the subsequent code cell and finalize `hanoi()`!"
]
},
{
@ -310,13 +336,25 @@
"metadata": {},
"outputs": [],
"source": [
"def hanoi(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\"):\n",
"def hanoi(n_disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\"):\n",
" \"\"\"A Pythonic implementation of Towers of Hanoi.\n",
"\n",
" This function prints out the moves to solve a Towers of Hanoi problem.\n",
"\n",
" Args:\n",
" n_disks (int): number of disks in the tower\n",
" origin (str, optional): label for the spot of the tower at the start\n",
" intermediate (str, optional): label for the intermediate spot\n",
" destination (str, optional): label for the spot of the tower at the end\n",
" \"\"\"\n",
" # answer to Q9\n",
" # ...\n",
" ...\n",
" ...\n",
"\n",
" # answer to Q10\n",
" # ..."
" ...\n",
" ...\n",
" ..."
]
},
{
@ -330,18 +368,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q10**: Instead of conditional logic, `hanoi()` calls itself **two times** with the **three** arguments `origin`, `intermediate`, and `destination` passed on in a **different** order.\n",
"**Q10**: Instead of conditional logic, `hanoi()` calls itself *two* times with the *three* arguments `origin`, `intermediate`, and `destination` passed on in a *different* order.\n",
"\n",
"Figure out how the arguments are passed on in the two recursive `hanoi()` calls!\n",
"Figure out how the arguments are passed on in the two recursive `hanoi()` calls and finish `hanoi()`.\n",
"\n",
"Also, write a `print()` statement analogous to the one in `sol()` in between the two recursive function calls. Is it ok to copy and paste it?"
"Hint: Do not forget to use [print()](https://docs.python.org/3/library/functions.html#print) to print out the moves!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q11**: Execute the code cells below and confirm that the printed moves are correct!"
"**Q11**: Execute the code cells below and confirm that the moves are correct!"
]
},
{
@ -384,7 +422,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We could, of course, also use **numeric labels** for the three steps like so."
"We could, of course, also use *numeric* labels for the three steps like so."
]
},
{
@ -400,22 +438,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Passing a Value \"up\" the Recursion Tree"
"#### Passing a Value \"down\" the Recursion Tree"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's say we did not know about the **analytical formula** for the number of **minimal moves** given $n$.\n",
"The above `hanoi()` prints the optimal solution's moves in the correct order but fails to label each move with an order number. This is built in the `hanoi_ordered()` function below by passing on a \"private\" `_offset` argument \"down\" the recursion tree. The leading underscore `_` in the parameter name indicates that it is *not* to be used by the caller of the function. That is also why the parameter is *not* mentioned in the docstring.\n",
"\n",
"In such cases, we could modify a recursive function to return a count value to be passed up the recursion tree.\n",
"\n",
"This is similar to what we do in the recursive versions of `factorial()` and `fibonacci()` in [Chapter 4](https://github.com/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb), where we pass up an intermediate result.\n",
"\n",
"Let's create a `hanoi_moves()` function that follows the same internal logic as `hanoi()`, but, instead of printing out the moves, returns the number of steps done so far in the recursion.\n",
"\n",
"Write your answers to **Q12** to **Q14** into the subsequent code cell and finalize `hanoi_moves()`! No need to write a docstring or validate the input here."
"Write your answers to **Q12** and **Q13** into the subsequent code cell and finalize `hanoi_ordered()`! As the logic gets a bit \"involved,\" `hanoi_ordered()` below is almost finished."
]
},
{
@ -424,166 +456,56 @@
"metadata": {},
"outputs": [],
"source": [
"def hanoi_moves(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\"):\n",
"def hanoi_ordered(n_disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\", _offset=None):\n",
" \"\"\"A Pythonic implementation of Towers of Hanoi.\n",
"\n",
" This function prints out the moves to solve a Towers of Hanoi problem.\n",
" Each move is labeled with an order number.\n",
"\n",
" Args:\n",
" n_disks (int): number of disks in the tower\n",
" origin (str, optional): label for the spot of the tower at the start\n",
" intermediate (str, optional): label for the intermediate spot\n",
" destination (str, optional): label for the spot of the tower at the end\n",
" \"\"\"\n",
" # answer to Q12\n",
" # ...\n",
" ...\n",
" ...\n",
"\n",
" moves = ... # <- answer to Q13\n",
" moves += hanoi_moves(...) # <- answer to Q14 between the ()\n",
" moves += hanoi_moves(...) # <- answer to Q14 between the ()\n",
"\n",
" return moves"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q12**: Copy the base case from `hanoi()`! What count should be returned when it is reached?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q13**: Initialize the variable `moves` with an appropriate count! This is the number of moves that corresponds to **one** recursive function call."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q14**: `moves` is updated with the counts passed up from the two recursive calls.\n",
"\n",
"Complete the two recursive function calls with the same arguments as in `hanoi()`!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q15**: Write a `for`-loop that prints out the **minimum number** of moves needed to solve Towers of Hanoi for any number of `disks` from `1` through `20` to confirm your answer to **Q2**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for ... in ...:\n",
" ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Time Complexity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Observe how quickly the `hanoi_moves()` function slows down for increasing `disks` arguments.\n",
"\n",
"With `disks` in the range from `24` through `26`, the computation time roughly doubles for each increase of `disks` by 1.\n",
"\n",
"**Q16**: Execute the code cells below and see for yourself!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%timeit -n 1 -r 1\n",
"print(\"Number of moves:\", hanoi_moves(24))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%timeit -n 1 -r 1\n",
"print(\"Number of moves:\", hanoi_moves(25))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%timeit -n 1 -r 1\n",
"print(\"Number of moves:\", hanoi_moves(26))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Passing a Value \"down\" the Recursion Tree (Advanced)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above `hanoi()` prints the optimal solution's moves in the correct order but fails to label each move with an order number. This can be built in by passing on one more argument `offset` down the recursion tree. As the logic gets a bit \"involved,\" `hanoi_ordered()` below is almost finished.\n",
"\n",
"Write your answers to **Q17** and **Q18** into the subsequent code cell and finalize `hanoi_ordered()`! No need to write a docstring or validate the input here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def hanoi_ordered(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\", offset=None):\n",
"\n",
" # answer to Q17\n",
" # ...\n",
"\n",
" total = (2 ** disks - 1)\n",
" half = (2 ** (disks - 1) - 1)\n",
" total = (2 ** n_disks - 1)\n",
" half = (2 ** (n_disks - 1) - 1)\n",
" count = total - half\n",
"\n",
" if offset is not None:\n",
" count += offset\n",
" if _offset is not None:\n",
" count += _offset\n",
"\n",
" hanoi_ordered(..., offset=offset) # <- answer to Q18\n",
" # ... <- answer to Q18\n",
" hanoi_ordered(..., offset=count) # <- answer to Q18"
" # answer to Q18\n",
" hanoi_ordered(..., _offset=_offset)\n",
" ...\n",
" hanoi_ordered(..., _offset=count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q17**: Copy the base case from the original `hanoi()`!"
"**Q12**: Copy the base case from the original `hanoi()`!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q18**: Complete the two recursive function calls with the same arguments as in `hanoi()` or `hanoi_moves()`! Do not change the already filled in `offset` arguments!\n",
"**Q13**: Complete the two recursive function calls with the same arguments as in `hanoi()`! Do *not* change the already filled in `offset` arguments!\n",
"\n",
"Then, copy the `print()` statement from `hanoi()` and adjust it to print out `count` as well!"
"Then, adjust the use of [print()](https://docs.python.org/3/library/functions.html#print) from above to print out the moves with their order number!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q19**: Execute the code cells below and confirm that the order numbers are correct!"
"**Q14**: Execute the code cells below and confirm that the order numbers are correct!"
]
},
{
@ -626,7 +548,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Lastly, it is to be mentioned that for problem instances with a small `disks` argument, it is easier to collect all the moves first in a list and then add the order number with the [enumerate()](https://docs.python.org/3/library/functions.html#enumerate) built-in."
"Lastly, it is to be mentioned that for problem instances with a small `n_disks` argument, it is easier to collect all the moves first in a `list` object and then add the order number with the [enumerate()](https://docs.python.org/3/library/functions.html#enumerate) built-in."
]
},
{
@ -640,14 +562,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q20**: Conducting your own research on the internet (max. 15 minutes), what can you say about generalizing the **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem to a setting with **more than three** landing spots?"
"**Q15**: Conducting your own research on the internet, what can you say about generalizing the **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem to a setting with *more than three* landing spots?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 4: Recursion & Looping"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells."
"The exercises below assume that you have read the \"*Looping*\" part in [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -264,7 +265,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" ..."
" < your answer >"
]
},
{

File diff suppressed because one or more lines are too long

View file

@ -4,8 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 5: Bits & Numbers"
"# Chapter 5: Numbers & Bits"
]
},
{
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -47,7 +48,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -61,28 +62,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q3**: Why is it *inefficient* to store `bool` objects in bits resembling a **hexadecimal representation**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q4**: Colors are commonly expressed in the **hexadecimal system** in websites (cf., the [HTML](https://en.wikipedia.org/wiki/HTML) and [CSS](https://en.wikipedia.org/wiki/Cascading_Style_Sheets) formats).\n",
"**Q3**: Colors are commonly expressed in the **hexadecimal system** in websites (cf., the [HTML](https://en.wikipedia.org/wiki/HTML) and [CSS](https://en.wikipedia.org/wiki/Cascading_Style_Sheets) formats).\n",
"\n",
"For example, $#000000$, $#ff9900$, and $#ffffff$ turn out to be black, orange, and white. The six digits are read in *pairs of two* from left to right, and the *three pairs* correspond to the proportions of red, green, and blue mixed together. They reach from $0_{16} = 0_{10}$ for $0$% to $\\text{ff}_{16} = 255_{10}$ for $100$% (cf., this [article](https://en.wikipedia.org/wiki/RGB_color_model) for an in-depth discussion).\n",
"\n",
@ -93,77 +80,77 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q5**: What does it mean for a code fragment to **fail silently**?"
"**Q4**: What does it mean for a code fragment to **fail silently**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q6**: Explain why the mathematical set of all real numbers $\\mathbb{R}$ can only be **approximated** by floating-point numbers on a computer!"
"**Q5**: Explain why the mathematical set of all real numbers $\\mathbb{R}$ can only be **approximated** by floating-point numbers on a computer!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q7**: How do we deal with a `float` object's imprecision if we need to **check for equality**?"
"**Q6**: How do we deal with a `float` object's imprecision if we need to **check for equality**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q8**: What data type, built-in or from the [standard library](https://docs.python.org/3/library/index.html), is best suited to represent the [transcendental numbers](https://en.wikipedia.org/wiki/Transcendental_number) $\\pi$ and $\\text{e}$?"
"**Q7**: What data type, built-in or from the [standard library](https://docs.python.org/3/library/index.html), is best suited to represent the [transcendental numbers](https://en.wikipedia.org/wiki/Transcendental_number) $\\pi$ and $\\text{e}$?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9**: How can **abstract base classes**, for example, as defined in the **numerical tower**, be helpful in enabling **duck typing**?"
"**Q8**: How can **abstract base classes**, for example, as defined in the **numerical tower**, be helpful in enabling **duck typing**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -184,63 +171,63 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q10**: The precision of `int` objects depends on how we choose to represent them in memory. For example, using a **hexadecimal representation** gives us $16^8$ digits whereas with a **binary representation** an `int` object can have *at most* $2^8$ digits."
"**Q9**: The precision of `int` objects depends on how we choose to represent them in memory. For example, using a **hexadecimal representation** gives us $16^8$ digits whereas with a **binary representation** an `int` object can have *at most* $2^8$ digits."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q11**: With the built-in [round()](https://docs.python.org/3/library/functions.html#round) function, we obtain a *precise* representation for any `float` object if we can live with *less than* $15$ digits of precision."
"**Q10**: With the built-in [round()](https://docs.python.org/3/library/functions.html#round) function, we obtain a *precise* representation for any `float` object if we can live with *less than* $15$ digits of precision."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q12**: As most currencies operate with $2$ or $3$ decimals (e.g., EUR $9.99$), the `float` type's limitation of *at most* $15$ digits is *not* a problem in practice."
"**Q11**: As most currencies operate with $2$ or $3$ decimals (e.g., EUR $9.99$), the `float` type's limitation of *at most* $15$ digits is *not* a problem in practice."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q13**: The [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) standard's **special values** provide no benefit in practice as we could always use a **[sentinel](https://en.wikipedia.org/wiki/Sentinel_value)** value (i.e., a \"dummy\"). For example, instead of `nan`, we can always use `0` to indicate a *missing* value."
"**Q12**: The [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) standard's **special values** provide no benefit in practice as we could always use a **[sentinel](https://en.wikipedia.org/wiki/Sentinel_value)** value (i.e., a \"dummy\"). For example, instead of `nan`, we can always use `0` to indicate a *missing* value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q14**: The following code fragment raises an `InvalidOperation` exception. That is an example of code **failing loudly**.\n",
"**Q13**: The following code fragment raises an `InvalidOperation` exception. That is an example of code **failing loudly**.\n",
"```python\n",
"float(\"inf\") + float(\"-inf\")\n",
"```"
@ -250,63 +237,63 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q15**: Python provides a `scientific` type (e.g., `1.23e4`) that is useful mainly to model problems in the domains of physics or astrophysics."
"**Q14**: Python provides a `scientific` type (e.g., `1.23e4`) that is useful mainly to model problems in the domains of physics or astrophysics."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q16**: From a practitioner's point of view, the built-in [format()](https://docs.python.org/3/library/functions.html#format) function does the *same* as the built-in [round()](https://docs.python.org/3/library/functions.html#round) function."
"**Q15**: From a practitioner's point of view, the built-in [format()](https://docs.python.org/3/library/functions.html#format) function does the *same* as the built-in [round()](https://docs.python.org/3/library/functions.html#round) function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q17**: The `Decimal` type from the [decimal](https://docs.python.org/3/library/decimal.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the real numbers $\\mathbb{R}$ *precisely*."
"**Q16**: The `Decimal` type from the [decimal](https://docs.python.org/3/library/decimal.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the real numbers $\\mathbb{R}$ *precisely*."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q18**: The `Fraction` type from the [fractions](https://docs.python.org/3/library/fractions.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the rational numbers $\\mathbb{Q}$ *precisely*."
"**Q17**: The `Fraction` type from the [fractions](https://docs.python.org/3/library/fractions.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the rational numbers $\\mathbb{Q}$ *precisely*."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],

View file

@ -4,8 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 5: Bits & Numbers"
"# Chapter 5: Numbers & Bits"
]
},
{
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells."
"The exercises below assume that you have read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) in the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -80,7 +81,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -94,7 +95,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -110,7 +111,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -238,7 +239,7 @@
"\n",
"Enable **duck typing** by allowing the function to be called with various numeric types as the arguments, in particular, `quantity` may be a non-integer as well: Use an appropriate **abstract base class** from the [numbers](https://docs.python.org/3/library/numbers.html) module in the [standard library](https://docs.python.org/3/library/index.html) to verify the arguments' types and also that they are both positive!\n",
"\n",
"It is considered a *best practice* to only round towards the *end* of the calculations."
"Hint: It is considered a *best practice* to only round towards the *end* of the calculations."
]
},
{
@ -256,8 +257,43 @@
"metadata": {},
"outputs": [],
"source": [
"def discounted_price(...):\n",
" ..."
"def discounted_price(unit_price, quantity):\n",
" \"\"\"Calculate the price of a line item in an order.\n",
"\n",
" Args:\n",
" unit_price (numbers.Real): price of an ordered item; must be positive\n",
" quantity (numbers.Real): number of items ordered; must be positive\n",
"\n",
" Returns:\n",
" line_item_price (decimal.Decimal): precision of 2 decimals\n",
" \"\"\"\n",
" # Basic input validation.\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" # Calculate the final price if only\n",
" # the first discount scheme is applied.\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" # Calculate the final price if only\n",
" # the second discount scheme is applied.\n",
" # \"One in every five\" means we need to figure out\n",
" # how many full groups of five are contained.\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" # Choose the better option for the customer.\n",
" ...\n",
" # Round correctly for pricing purposes.\n",
" return ..."
]
},
{

File diff suppressed because one or more lines are too long

View file

@ -4,8 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 6: Bytes & Text"
"# Chapter 6: Text & Bytes"
]
},
{
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 6](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 6](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -47,7 +48,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -61,7 +62,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -80,7 +81,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -94,7 +95,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -122,7 +123,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -136,7 +137,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -150,7 +151,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -164,7 +165,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 6: Text & Bytes"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 6](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells."
"The exercises below assume that you have read [Chapter 6](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_00_lecture.ipynb) in the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -86,21 +87,25 @@
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" is_palindrome = ... # Q3\n",
" # answer to Q3\n",
" is_palindrome = ...\n",
" if ignore_case:\n",
" ... # Q3\n",
" chars_to_check = # Q1\n",
" ...\n",
" # answer to Q1\n",
" chars_to_check = ...\n",
"\n",
" for forward_index in range(chars_to_check):\n",
" backward_index = ... # Q2\n",
" forward = ... # Q2\n",
" backward = ... # Q2\n",
" # answer to Q2\n",
" backward_index = ...\n",
" forward = ...\n",
" backward = ...\n",
"\n",
" print(forward, \"and\", backward) # added for didactical purposes\n",
"\n",
" if ...: # Q3\n",
" is_palindrome = ... # Q3\n",
" ... # Q3\n",
" # answer to Q3\n",
" if ...:\n",
" is_palindrome = ...\n",
" ...\n",
"\n",
" return is_palindrome"
]
@ -237,18 +242,21 @@
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" is_palindrome = ... # Q5\n",
" # answers from above\n",
" is_palindrome = ...\n",
" if ignore_case:\n",
" ... # Q5\n",
" chars_to_check = # Q5\n",
" ...\n",
" chars_to_check = ...\n",
"\n",
" for ... in ...: #Q6\n",
" # answer to Q6\n",
" for ... in ...:\n",
"\n",
" print(forward, \"and\", backward) # added for didactical purposes\n",
"\n",
" if ...: # Q5\n",
" is_palindrome = ... # Q5\n",
" ... # Q5\n",
" # answers from above\n",
" if ...:\n",
" is_palindrome = ...\n",
" ...\n",
"\n",
" return is_palindrome"
]
@ -337,6 +345,7 @@
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answers from above\n",
" if ignore_case:\n",
" ...\n",
" chars_to_check = ...\n",
@ -346,9 +355,11 @@
" print(forward, \"and\", backward) # added for didactical purposes\n",
"\n",
" if ...:\n",
" ... # Q8\n",
" # answer to Q8\n",
" ...\n",
"\n",
" ... # Q8"
" # answer to Q8\n",
" return ..."
]
},
{
@ -462,7 +473,9 @@
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" ... # Q11\n",
" # answer to Q11\n",
" ...\n",
" # answers from above\n",
" if ignore_case:\n",
" text = ...\n",
" chars_to_check = ...\n",
@ -471,7 +484,7 @@
" if ...:\n",
" ...\n",
"\n",
" ..."
" return ..."
]
},
{
@ -584,19 +597,22 @@
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answers from above\n",
" ...\n",
" if ignore_case:\n",
" ...\n",
" # answer to Q13\n",
" if ignore_symbols:\n",
" for ... in ...: # Q13\n",
" ... # Q13\n",
" for ... in ...:\n",
" ...\n",
" # answers from above\n",
" chars_to_check = ...\n",
"\n",
" for ... in ...:\n",
" if ...:\n",
" ...\n",
"\n",
" ..."
" return ..."
]
},
{
@ -782,6 +798,7 @@
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answers from above\n",
" ...\n",
" if ignore_case:\n",
" ...\n",
@ -789,12 +806,14 @@
" for ... in ...:\n",
" ...\n",
"\n",
" if ...: # Q16\n",
" ... # Q16\n",
" elif ...: # Q16\n",
" ... # Q16\n",
" # answer to Q16\n",
" if ...:\n",
" ...\n",
" elif ...:\n",
" ...\n",
"\n",
" return ... # Q17"
" # answer to Q17\n",
" return ..."
]
},
{
@ -955,7 +974,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 7: Sequential Data"
]
},
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -47,7 +48,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -61,7 +62,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -75,7 +76,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -89,7 +90,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -103,7 +104,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -117,7 +118,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -133,7 +134,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -147,7 +148,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -161,104 +162,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9**: With the [map()](https://docs.python.org/3/library/functions.html#map) and [filter()](https://docs.python.org/3/library/functions.html#filter) built-ins and the [reduce()](https://docs.python.org/3/library/functools.html#functools.reduce) function from the [functools](https://docs.python.org/3/library/functools.html) module in the [standard library](https://docs.python.org/3/library/index.html), we can replace many tedious `for`-loops and `if` statements. What are some advantages of doing so?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q10**: Looking at the `lambda` expression inside [reduce()](https://docs.python.org/3/library/functools.html#functools.reduce) below, what \"simple\" [built-in function](https://docs.python.org/3/library/functions.html) is mimicked here?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"from functools import reduce\n",
"\n",
"numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]\n",
"\n",
"reduce(lambda x, y: x if x > y else y, numbers)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q11**: What is the primary use case of **list comprehensions**? Why do we describe them as **eager**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q12**: **Generator expressions** may replace `list` objects and list comprehensions in many scenarios. When evaluated, they create a **lazy** `generator` object that does *not* **materialize** its elements right away. What do we mean by that? What does it mean for a `generator` object to be **exhausted**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q13**: What does it mean for the **boolean reducers**, the built-in [all()](https://docs.python.org/3/library/functions.html#all) and [any()](https://docs.python.org/3/library/functions.html#any) functions, to follow the **short-circuiting** strategy?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q14**: What is an **iterator**? How does it relate to an **iterable**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -279,84 +183,42 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q15**: `sequence` objects are *not* part of core Python but may be imported from the [standard library](https://docs.python.org/3/library/index.html)."
"**Q9**: `sequence` objects are *not* part of core Python but may be imported from the [standard library](https://docs.python.org/3/library/index.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q16**: The built-in [sort()](https://docs.python.org/3/library/stdtypes.html#list.sort) function takes a *finite* **iterable** as its argument an returns a *new* `list` object. On the contrary, the [sorted()](https://docs.python.org/3/library/functions.html#sorted) method on `list` objects *mutates* them *in place*."
"**Q10**: The built-in [sort()](https://docs.python.org/3/library/stdtypes.html#list.sort) function takes a *finite* **iterable** as its argument an returns a *new* `list` object. On the contrary, the [sorted()](https://docs.python.org/3/library/functions.html#sorted) method on `list` objects *mutates* them *in place*."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q17**: Passing **mutable** objects as arguments to functions is not problematic because functions operate in a **local** scope without affecting the **global** scope."
"**Q11**: Passing **mutable** objects as arguments to functions is not problematic because functions operate in a **local** scope without affecting the **global** scope."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q18**: `lambda` expressions are useful in the context of the **map-filter-reduce** paradigm, where we often do *not* re-use a `function` object more than once."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q19**: Using **generator expressions** in place of **list comprehensions** wherever possible is a good practice as it makes our programs use memory more efficiently."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q20**: Just as **list comprehensions** create `list` objects, **tuple comprehensions** create `tuple` objects."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,3 @@
**Important**: The content is being updated and amended throughout the
spring semester of 2020!
# An Introduction to Python and Programming
The purpose of this repository is to serve as an interactive "book" for a
@ -17,41 +14,56 @@ They can be viewed in a plain web browser with the help of
[nbviewer](https://nbviewer.jupyter.org/):
- *Introduction*: Start up
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_00_lecture.ipynb)
([video](https://www.youtube.com/watch?v=YTU8jaG27Xk)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_02_exercises.ipynb))
- **Part A: Expressing Logic**
- *Chapter 1*: Elements of a Program
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb)
([video](https://www.youtube.com/watch?v=v0lk1Qfaw8Y)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_02_exercises.ipynb))
- *Chapter 2*: Functions & Modularization
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb)
([video](https://www.youtube.com/watch?v=j4Xn8QFysmc)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions_02_exercises.ipynb))
- *Chapter 3*: Conditionals & Exceptions
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb)
([video](https://www.youtube.com/watch?v=aDbblINzuGQ)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals_02_exercises.ipynb))
- *Chapter 4*: Recursion & Looping
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb)
([video](https://www.youtube.com/watch?v=jT6hr4vOJks)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_02_exercises.ipynb))
| [exercises 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_02_exercises.ipynb)
| [exercises 2](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_03_exercises.ipynb))
- **Part B: Managing Data and Memory**
- *Chapter 5*: Bits & Numbers
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb)
- *Chapter 5*: Numbers & Bits
([video](https://www.youtube.com/watch?v=nB00WGCnVjg)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_02_exercises.ipynb))
- *Chapter 6*: Bytes & Text
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_00_lecture.ipynb)
- *Chapter 6*: Text & Bytes
([video](https://www.youtube.com/watch?v=3WTRlgN09sM)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_02_exercises.ipynb)))
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text_02_exercises.ipynb))
- *Chapter 7*: Sequential Data
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_00_lecture.ipynb)
([video](https://www.youtube.com/watch?v=nx2sCDoeC3I)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences_02_exercises.ipynb))
- *Chapter 8*: Mappings & Sets
([lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/08_mappings_00_lecture.ipynb)
- *Chapter 8*: Map, Filter, & Reduce
([video](https://www.youtube.com/watch?v=ePzzq2YBWjY)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/08_mfr_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/08_mfr_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/08_mfr_02_exercises.ipynb))
- *Chapter 9*: Mappings & Sets
([video](https://www.youtube.com/watch?v=vbp0svA35TE)
| [lecture](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/08_mappings_00_lecture.ipynb)
| [review](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/08_mappings_01_review.ipynb)
| [exercises](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/08_mappings_02_exercises.ipynb))
@ -62,11 +74,16 @@ Precise **installation instructions** are either in the [00th notebook](
https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_intro_00_lecture.ipynb)
or further below.
A playlist with all video presentations of the individiual chapters can be found [here](https://www.youtube.com/playlist?list=PL-2JV1G3J10lQ2xokyQowcRJI5jjNfW7f).
Feedback is encouraged and will be incorporated.
Open an issue in the [issues tracker](https://github.com/webartifex/intro-to-python/issues)
or initiate a [pull request](https://help.github.com/en/articles/about-pull-requests)
if you are familiar with the concept.
A "Show HN" post about this introduction was made on [Hacker News](https://news.ycombinator.com/item?id=22669084)
and some ideas for improvement were discussed there.
## Prerequisites

View file

@ -1,6 +1,6 @@
[tool.poetry]
name = "intro-to-python"
version = "0.6.7"
version = "0.6.8"
description = "An introduction to Python and programming for wanna-be data scientists"
authors = ["Alexander Hess <alexander@webartifex.biz>"]
license = "MIT"

View file

@ -43,7 +43,7 @@ def average(numbers, *, scalar=1):
scalar (float, optional): multiplies the average; defaults to 1
Returns:
float: (scaled) average
scaled_average (float)
"""
return _scaled_average(_round_all(numbers), scalar)
@ -57,7 +57,7 @@ def average_evens(numbers, *, scalar=1):
scalar (float, optional): multiplies the average; defaults to 1
Returns:
float: (scaled) average
scaled_average (float)
"""
return _scaled_average([n for n in _round_all(numbers) if n % 2 == 0], scalar)
@ -71,6 +71,6 @@ def average_odds(numbers, *, scalar=1):
scalar (float, optional): multiplies the average; defaults to 1
Returns:
float: (scaled) average
scaled_average (float)
"""
return _scaled_average([n for n in _round_all(numbers) if n % 2 != 0], scalar)