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
120 changes: 110 additions & 10 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": 1,
"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,11 +57,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Odysseus text text text\n",
"Neoptolemus text text text\n",
"Philoctetes text text text\n"
]
}
],
"source": [
"# Hint: Use print(f\"{variable}\")"
"# Hint: Use print(f\"{variable}\")\n",
"for character in cast:\n",
" print(character.title() + \" text text text\")"
]
},
{
Expand All @@ -63,9 +85,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"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 Down Expand Up @@ -137,7 +170,7 @@
"for character in cast:\n",
" print(character)\n",
"\n",
"print(f\"{character} was played by a great actor!\")"
"print(f\"{character} was played by a great actor!\")\n"
]
},
{
Expand Down Expand Up @@ -304,11 +337,78 @@
"\n",
"Matthes 2023, p. 67, Exercise 4-13."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"toast\n",
"bagels\n",
"waffles\n",
"eggs\n",
"pancakes\n"
]
}
],
"source": [
"buffet = (\"toast\", \"bagels\", \"waffles\", \"eggs\", \"pancakes\")\n",
"for food in buffet:\n",
" print(food)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mbuffet\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmuffins\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"buffet[0] = \"muffins\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"toast\n",
"oatmeal\n",
"waffles\n",
"cereal\n",
"pancakes\n"
]
}
],
"source": [
"buffet = (\"toast\", \"oatmeal\", \"waffles\", \"cereal\", \"pancakes\")\n",
"for food in buffet:\n",
" print(food)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -322,7 +422,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down