diff --git a/for-loops.ipynb b/for-loops.ipynb index d8dc735..2d6920a 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", @@ -47,11 +57,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Odysseus yes\n", + "Neoptolemus yes\n", + "Philoctetes yes\n" + ] + } + ], "source": [ - "# Hint: Use print(f\"{variable}\")" + "for character in cast:\n", + " print (f\"{character.title()} yes\")" ] }, { @@ -63,9 +84,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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,9 +118,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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[4], 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", @@ -107,9 +148,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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 +182,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "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 +223,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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 +251,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 1, + "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 +272,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 2, + "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 +302,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 4, 5, 6]" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "[value+2 for value in range(0, 5)]" ] @@ -229,9 +338,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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 +370,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "4\n", + "5\n", + "6\n" + ] + } + ], "source": [ - "# Try looping over a slice here" + "# Try looping over a slice here\n", + "my_list = list(range(1,9))\n", + "for item in my_list[2:6]:\n", + " print(item)" ] }, { @@ -278,9 +411,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "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[20], 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 +449,123 @@ "\n", "Matthes 2023, p. 67, Exercise 4-13." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Buffet: A buffet-style restaurant offers only five basic foods. Think of five simple foods, and store them in a tuple." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('bread', 'sushi', 'steak', 'paella', 'yogurt')\n" + ] + } + ], + "source": [ + "my_buffet = (\"bread\", \"sushi\", \"steak\", \"paella\", \"yogurt\")\n", + "print(my_buffet)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Use a for loop to print each food the restaurant offers." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bread\n", + "sushi\n", + "steak\n", + "paella\n", + "yogurt\n" + ] + } + ], + "source": [ + "for food in my_buffet:\n", + " print(food)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Try to modify one of the items, and make sure that Python rejects the change." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "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[24], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_buffet\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;124mmac and cheese\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "my_buffet[3] = \"mac and cheese\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The restaurant changes its menu, replacing two of the items with different foods. Add a line that rewrites the tuple, and then use a for loop to print each of the items on the revised menu." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "cereal\n", + "porridge\n", + "steak\n", + "paella\n", + "yogurt\n" + ] + } + ], + "source": [ + "my_buffet = (\"cereal\", \"porridge\", \"steak\", \"paella\", \"yogurt\")\n", + "for food in my_buffet:\n", + " print(food)" + ] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -322,7 +579,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.1" } }, "nbformat": 4,