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
171 changes: 160 additions & 11 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": 3,
"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": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -63,9 +73,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"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": 6,
"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[6], 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 @@ -278,9 +308,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[11], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m my_tuple \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m)\n\u001b[0;32m----> 3\u001b[0m \u001b[43mmy_tuple\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m5\u001b[39m\n",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"my_tuple = (1, 2, 3)\n",
"\n",
Expand All @@ -304,11 +346,118 @@
"\n",
"Matthes 2023, p. 67, Exercise 4-13."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4-13. Buffet: A buffet-style restaurant offers only five basic foods. Think of five\n",
"simple foods, and store them in a tuple.\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"menu = (\"spring roll\", \"ramen\", \"curry\", \"rice\", \"steak\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Use a for loop to print each food the restaurant offers"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"spring roll\n",
"ramen\n",
"curry\n",
"rice\n",
"steak\n"
]
}
],
"source": [
"for food in menu:\n",
" print(food)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Try to modify one of the items, and make sure that Python rejects the\n",
"change."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[16], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmenu\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnone\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"menu[1] = \"none\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- The restaurant changes its menu, replacing two of the items with different\n",
"foods. Add a line that rewrites the tuple, and then use a for loop to print\n",
"each of the items on the revised menu."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bread\n",
"ramen\n",
"braised pork\n",
"rice\n",
"steak\n"
]
}
],
"source": [
"menu = (\"bread\", \"ramen\", \"braised pork\", \"rice\", \"steak\")\n",
"for food in menu:\n",
" print(food)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -322,7 +471,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down