Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 151 additions & 15 deletions for-loops.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"odysseus\n",
"neoptolemus\n",
"philoctetes\n"
]
}
],
"source": [
"cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n",
"\n",
Expand All @@ -47,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -63,9 +73,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"odysseus\n",
"neoptolemus\n",
"philoctetes\n",
"And now the loop is done!\n"
]
}
],
"source": [
"cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n",
"\n",
Expand All @@ -86,9 +107,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block after 'for' statement on line 3 (3799840020.py, line 4)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[21], line 4\u001b[0;36m\u001b[0m\n\u001b[0;31m print(character)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block after 'for' statement on line 3\n"
]
}
],
"source": [
"cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n",
"\n",
Expand Down Expand Up @@ -291,24 +321,130 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Now with style!\n",
"## Exercises\n",
"\n",
"TL;DR: Follow PEP 8, and use formatters to help enforce good style. Readability of code is half the battle!"
"Matthes 2023, p. 67, Exercise 4-13."
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"## Exercises\n",
"\n",
"Matthes 2023, p. 67, Exercise 4-13."
"buffet_foods = (\"chicken\", \"steak\", \"potatos\", \"salad\", \"pudding\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"menu:\n"
]
}
],
"source": [
"print(\"menu:\")\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"menu:\n",
"chicken\n",
"steak\n",
"potatos\n",
"salad\n",
"pudding\n"
]
}
],
"source": [
"print(\"menu:\")\n",
"for food in buffet_foods:\n",
" print(food)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"# buffet_foods[0] = \"pizza\""
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"old menu:\n",
"chicken\n",
"steak\n",
"potatos\n",
"salad\n",
"pudding\n"
]
}
],
"source": [
"print(\"old menu:\")\n",
"for food in buffet_foods:\n",
" print(food)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"new menu:\n",
"pizza\n",
"steak\n",
"potatos\n",
"salad\n",
"cake\n"
]
}
],
"source": [
"buffet_foods = (\"pizza\", \"steak\", \"potatos\", \"salad\", \"cake\")\n",
"print(\"new menu:\")\n",
"for new_food in buffet_foods:\n",
" print(new_food)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -322,7 +458,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down