From f7a6a5c37abdc20fa59e7dc3e6ea78890c091148 Mon Sep 17 00:00:00 2001 From: rachelrliu16 Date: Fri, 31 Jan 2025 18:26:16 +0000 Subject: [PATCH] Completed Exercise 4.13 --- for-loops.ipynb | 253 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 228 insertions(+), 25 deletions(-) diff --git a/for-loops.ipynb b/for-loops.ipynb index d8dc735..4546b9e 100644 --- a/for-loops.ipynb +++ b/for-loops.ipynb @@ -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", @@ -38,7 +48,7 @@ "source": [ "Let's break this down:\n", "\n", - "1. `cast = [\"Odysseus\", \"Neoptolemus\", \"Philoctetes\"]` declares a variable and assigns to it the list containing the `str`-names of the main cast of Sophocles' _Philoctetes_.\n", + "1. `cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]` declares a variable and assigns to it the list containing the `str`-names of the main cast of Sophocles' _Philoctetes_.\n", "2. `for character in cast:` opens a `for` loop, assigning each item in `cast` to the variable `character` — which is only defined inside the loop. The name `character` is unimportant — you could us any valid Pyhon variable. But it's good practice to use names that are descriptive of the contents.\n", "3. ` print(character)` prints the `str`-name of each `character`. Notice the indentation!\n", "\n", @@ -47,11 +57,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Odysseus text\n", + "Neoptolemus text\n", + "Philoctetes text\n" + ] + } + ], "source": [ - "# Hint: Use print(f\"{variable}\")" + "# Hint: Use print(f\"{variable}\")\n", + "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", + "\n", + "for character in cast:\n", + " print(f\"{character.title()} text\")" ] }, { @@ -63,9 +87,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", @@ -86,14 +121,25 @@ }, { "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", "for character in cast:\n", - "print(character)\n", + " print(character)\n", "\n", "print(\"And now the loop is done!\")" ] @@ -107,9 +153,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "odysseus\n", + "And now the loop is done!\n", + "neoptolemus\n", + "And now the loop is done!\n", + "philoctetes\n", + "And now the loop is done!\n" + ] + } + ], "source": [ "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", "\n", @@ -128,9 +187,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "odysseus\n", + "neoptolemus\n", + "philoctetes\n", + "philoctetes was played by a great actor!\n" + ] + } + ], "source": [ "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", "\n", @@ -140,6 +210,33 @@ "print(f\"{character} was played by a great actor!\")" ] }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "odysseus\n", + "neoptolemus\n", + "philoctetes\n", + "philoctetes was played by a great actor!\n" + ] + } + ], + "source": [ + "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", + "last_actor_seen = None\n", + "\n", + "for character in cast:\n", + " last_actor_seen = character\n", + " print(character)\n", + "\n", + "print(f\"{character} was played by a great actor!\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -158,9 +255,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 is greater than 0\n", + "2 is greater than 1\n", + "3 is greater than 2\n", + "4 is greater than 3\n" + ] + } + ], "source": [ "for value in range(1, 5):\n", " print(f\"{value} is greater than {value - 1}\")" @@ -175,9 +283,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# We can use the third argument to adjust the size of each step\n", "list(range(0, 10, 2))" @@ -185,9 +304,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Or we can pass `range()` just one argument, and it will start from 0:\n", "list(range(10))" @@ -204,9 +334,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 4, 5, 6]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "[value+2 for value in range(0, 5)]" ] @@ -304,11 +445,73 @@ "\n", "Matthes 2023, p. 67, Exercise 4-13." ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "eggs\n", + "bacon\n", + "juice\n", + "fruit\n", + "pancakes\n" + ] + }, + { + "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[8], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m food \u001b[38;5;129;01min\u001b[39;00m menu:\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(food)\n\u001b[0;32m----> 6\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;124mcheese\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m#produces an error\u001b[39;00m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "#Exercise 4-13\n", + "menu =(\"eggs\",\"bacon\",\"juice\",\"fruit\",\"pancakes\")\n", + "for food in menu:\n", + " print(food)\n", + "\n", + "menu[1] = \"cheese\" #produces an error" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "New Menu:\n", + "eggs\n", + "sausage\n", + "juice\n", + "fruit\n", + "waffles\n" + ] + } + ], + "source": [ + "menu = (\"eggs\",\"sausage\",\"juice\",\"fruit\",\"waffles\")\n", + "print(\"New Menu:\")\n", + "for food in menu:\n", + " print (food)" + ] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -322,7 +525,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.1" } }, "nbformat": 4,