From e30f6e0801f9688390734538f73679acb9236a1b Mon Sep 17 00:00:00 2001 From: Guacanole01 <98541203+Guacanole01@users.noreply.github.com> Date: Mon, 3 Feb 2025 04:47:29 +0000 Subject: [PATCH] Completed Exercise 4-13 --- for-loops.ipynb | 282 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 251 insertions(+), 31 deletions(-) diff --git a/for-loops.ipynb b/for-loops.ipynb index d8dc735..4bc2f6d 100644 --- a/for-loops.ipynb +++ b/for-loops.ipynb @@ -22,9 +22,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "odysseus\n", + "neoptolemus\n", + "philoctetes\n" + ] + } + ], "source": [ "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", "\n", @@ -47,11 +57,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Odysseus\n", + "Neoptolemus\n", + "Philoctetes\n" + ] + } + ], "source": [ - "# Hint: Use print(f\"{variable}\")" + "# Hint: Use print(f\"{variable}\")\n", + "for character in cast:\n", + " print(f\"{character.title()}\")" ] }, { @@ -63,9 +85,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", + "And now the loop is done!\n" + ] + } + ], "source": [ "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", "\n", @@ -86,14 +119,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "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 +151,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "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 +185,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "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", @@ -158,9 +226,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "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 +254,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 16, + "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 +275,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 17, + "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 +305,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 4, 5, 6]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "[value+2 for value in range(0, 5)]" ] @@ -229,9 +341,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 4, 5, 6, 7]\n", + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\n", + "[15, 16, 17, 18, 19]\n" + ] + } + ], "source": [ "numbers = list(range(0, 20))\n", "\n", @@ -251,11 +373,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n", + "[2, 3, 4, 5, 6, 7]\n", + "[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n", + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]\n" + ] + } + ], "source": [ - "# Try looping over a slice here" + "# Try looping over a slice here\n", + "numbers2 = list(range(0, 30))\n", + "print(numbers2[2:30])\n", + "\n", + "print(numbers2[2:8])\n", + "\n", + "print(numbers2[20:])\n", + "\n", + "print(numbers2[:-2])\n" ] }, { @@ -278,9 +419,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "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[30], 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", @@ -304,11 +457,78 @@ "\n", "Matthes 2023, p. 67, Exercise 4-13." ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "yucca\n", + "noodles\n", + "pasta\n", + "cornmeal\n", + "burrito\n" + ] + } + ], + "source": [ + "restaurant = (\"yucca\", \"noodles\", \"pasta\", \"cornmeal\", \"burrito\")\n", + "for item in restaurant:\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "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[32], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mrestaurant\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mplantains\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "restaurant[3] = \"plantains\"" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "yucca\n", + "plantains\n", + "pasta\n", + "gnocchi\n", + "burrito\n" + ] + } + ], + "source": [ + "restaurant = (\"yucca\", \"plantains\", \"pasta\", \"gnocchi\", \"burrito\")\n", + "for item in restaurant:\n", + " print(item)" + ] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -322,7 +542,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.1" } }, "nbformat": 4,