diff --git a/.gitignore b/.gitignore index 68bc17f9ff2104a9d7b6777058bb4c343ca72609..d2e32b44492683a3dcf2e732181711df2f69daba 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# vscode +*.code-workspace \ No newline at end of file diff --git a/welcome-and-control-structures.ipynb b/welcome-and-control-structures.ipynb index 2f9d5194f59ddf729636a62dc052679404eedb4a..e3d0ea29a533ca425d3167ba66cef4e9424b8bc0 100644 --- a/welcome-and-control-structures.ipynb +++ b/welcome-and-control-structures.ipynb @@ -23,6 +23,11 @@ "\n", "Before we learn about control structures, let's learn a bit about the Jupyter Notebook data programming environment that we'll be using throughout this course. Jupyter Notebooks allow you to write formatted text descriptions (**Markdown cells**) alongside runnable Python code (**Code cells**). The text you're reading now is an example of a Markdown cell. You can double-click this text to edit it. Feel free to edit the contents of any cells that we give you: your changes only apply to your own copy.\n", "\n", + "JupyterHub has a built-in debugger that enables you to pause the program on any line of code and inspect its state. **But**, we are not going to use it because it's an overkill for this course (and even in actual work sometimes).\n", + "> *“The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.†— Brian Kernighan, “Unix for Beginners†(1979)*\n", + "\n", + " \n", + "\n", "Python code cells, like the one that appears below, can be run by clicking on the cell and then using the â–¶ï¸ play button at the top to run it. The keyboard shortcut **Ctrl + Enter** allows you to run the current cell, which comes in handy if you make some edits to a cell and your fingers are already on your keyboard.\n", "\n", "Try editing the following cell to replace Yuxuan with your own name and then run it using the keyboard shortcut." @@ -30,21 +35,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "abe596fb-ddc1-4955-ac58-43282c4dd720", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello, my name is Yuxuan'" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Hello, my name is Yuxuan\"" ] @@ -59,23 +53,12 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "0aea4f4e-aac5-4c6a-8712-36e5f0198608", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'My favorite course is CSE 163'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Hello, my name is Yuxuan\"\n", "\"My favorite course is CSE 163\"" @@ -133,14 +116,12 @@ "source": [ "## Assignment statements\n", "\n", - "Variables store values. Each Python value is composed of a value and its type. Unlike Java, Python doesn't require you to define the type of a variable. Variables are assigned using **assignment statements**.\n", - "\n", - "JupyterHub has a built-in debugger that enables you to pause the program on any line of code and inspect its state. Let's try it out now by clicking the **Enable Debugger** 🞠(beetle or bug) icon at the top and then setting a **breakpoint** by clicking on the line number that you want to pause before running." + "Variables store values. Each Python value is composed of a value and its type. Unlike Java, Python doesn't require you to define the type of a variable. Variables are assigned using **assignment statements**." ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "aacb2b81-8ce8-405a-8859-96aaf7b1e2ed", "metadata": {}, "outputs": [], @@ -161,21 +142,10 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "f968cc24-a2d4-4da4-8d38-fdf27ca74201", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "y" ] @@ -192,21 +162,10 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "a9fcebca-eaff-488b-ad7f-eda6d092079d", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "False\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "x = 3\n", "print(x < 4) # Is x less than 4?\n", @@ -225,18 +184,10 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "7d1349af-ba5c-412d-8539-0136c4a51a8e", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], + "outputs": [], "source": [ "x = 2.4\n", "y = 1.2\n", @@ -268,20 +219,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "8080a8e3-3179-4aed-a1a8-6ee877907bbd", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x-squared is less than y\n" - ] - } - ], + "outputs": [], "source": [ "if x ** 2 < y:\n", " print(\"x-squared is less than y\")\n", @@ -312,13 +255,13 @@ "Done!\n", "```\n", "\n", - "We can certainly write this out using a lot of print statements, but perhaps we can write a more general solution using loops instead. Lets practice converting this code into a loop." + "We can certainly write this out using a lot of print statements." ] }, { "cell_type": "code", "execution_count": null, - "id": "2b7b4b2e-234a-44ff-8eab-277261ba4119", + "id": "003ed3a5-675c-4837-b1f6-b613680e3b5d", "metadata": { "tags": [] }, @@ -335,14 +278,77 @@ "print(\"Done!\")" ] }, + { + "cell_type": "markdown", + "id": "4753f70a-716a-4f37-a382-02fe0a2adf4e", + "metadata": {}, + "source": [ + "But perhaps we can write a more general solution using loops instead. Let's practice converting this code into a `for` loop." + ] + }, { "cell_type": "code", "execution_count": null, - "id": "9727e6c9", + "id": "0d2a4557-c9ca-4b3d-b696-80d9c1f88663", "metadata": {}, "outputs": [], "source": [ - "# Let's convert the above cell into a loop!\n" + "# Let's write it as a function that takes in n (integer) and counts down to 0 by 10 seconds decrement.\n", + "# remember to document every function you write! using triple quotes ''' for docstrings.\n", + "\n", + "def countdown(n):\n", + " \"\"\"\n", + " your docstring here\n", + " \"\"\"\n", + " pass # your code here" + ] + }, + { + "cell_type": "markdown", + "id": "2a5cc692-25a9-4ca9-b286-a19a57cc1bbe", + "metadata": {}, + "source": [ + "Let's try some different test cases." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cbf9f76e-0eb4-4a3d-9051-528b48c7a90b", + "metadata": {}, + "outputs": [], + "source": [ + "countdown(60)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "732786a0-100e-4b25-b9b4-6431d3527ab6", + "metadata": {}, + "outputs": [], + "source": [ + "countdown(9)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a865bbd7-1338-40d5-b1c9-9ba9ebdc373c", + "metadata": {}, + "outputs": [], + "source": [ + "countdown(0.5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d57f8c5-d803-4a2e-82f2-1359e0ddafe3", + "metadata": {}, + "outputs": [], + "source": [ + "countdown(-1)" ] }, { @@ -418,10 +424,56 @@ { "cell_type": "code", "execution_count": null, - "id": "9480634a", + "id": "0151cc77-a6b6-4566-b1f9-56e754b65c41", + "metadata": {}, + "outputs": [], + "source": [ + "def countdown(n):\n", + " \"\"\"\n", + " your docstring here\n", + " \"\"\"\n", + " pass # your code here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "45343154-fcb3-45ba-9971-330e613f3718", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "countdown(60)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "351a86df-fd50-4a50-9e4d-08f395418524", + "metadata": {}, + "outputs": [], + "source": [ + "countdown(15)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b035334-dc5e-434c-8e9a-86fdb27c0bef", + "metadata": {}, + "outputs": [], + "source": [ + "countdown(-4)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a28180d4-72bc-4a8e-905d-37fc6f86fde4", + "metadata": {}, + "outputs": [], + "source": [ + "countdown(0)" + ] } ], "metadata": { @@ -440,7 +492,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.10" + "version": "3.10.13" } }, "nbformat": 4,