From 979f551ae35268ffbdd4ebf25d438d6bd1d26468 Mon Sep 17 00:00:00 2001 From: chasebeyer Date: Tue, 4 Feb 2025 20:43:26 +0000 Subject: [PATCH] loop --- for-loops.ipynb | 166 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 151 insertions(+), 15 deletions(-) diff --git a/for-loops.ipynb b/for-loops.ipynb index d8dc735..549cec9 100644 --- a/for-loops.ipynb +++ b/for-loops.ipynb @@ -22,9 +22,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "odysseus\n", + "neoptolemus\n", + "philoctetes\n" + ] + } + ], "source": [ "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", "\n", @@ -47,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -63,9 +73,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "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 +107,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "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[21], 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", @@ -291,24 +321,130 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Now with style!\n", + "## Exercises\n", "\n", - "TL;DR: Follow PEP 8, and use formatters to help enforce good style. Readability of code is half the battle!" + "Matthes 2023, p. 67, Exercise 4-13." ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 25, "metadata": {}, + "outputs": [], "source": [ - "## Exercises\n", - "\n", - "Matthes 2023, p. 67, Exercise 4-13." + "buffet_foods = (\"chicken\", \"steak\", \"potatos\", \"salad\", \"pudding\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "menu:\n" + ] + } + ], + "source": [ + "print(\"menu:\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "menu:\n", + "chicken\n", + "steak\n", + "potatos\n", + "salad\n", + "pudding\n" + ] + } + ], + "source": [ + "print(\"menu:\")\n", + "for food in buffet_foods:\n", + " print(food)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "# buffet_foods[0] = \"pizza\"" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "old menu:\n", + "chicken\n", + "steak\n", + "potatos\n", + "salad\n", + "pudding\n" + ] + } + ], + "source": [ + "print(\"old menu:\")\n", + "for food in buffet_foods:\n", + " print(food)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "new menu:\n", + "pizza\n", + "steak\n", + "potatos\n", + "salad\n", + "cake\n" + ] + } + ], + "source": [ + "buffet_foods = (\"pizza\", \"steak\", \"potatos\", \"salad\", \"cake\")\n", + "print(\"new menu:\")\n", + "for new_food in buffet_foods:\n", + " print(new_food)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -322,7 +458,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.1" } }, "nbformat": 4,