diff --git a/.gitignore b/.gitignore index b8d386f4..5283b0d5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ +# TMP storage for solutions +tutorials/solutions/ + + # jupyter-book dir _build diff --git a/scripts/split_off_solutions.ipynb b/scripts/split_off_solutions.ipynb new file mode 100644 index 00000000..4853f442 --- /dev/null +++ b/scripts/split_off_solutions.ipynb @@ -0,0 +1,135 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "16eab062-5094-47b9-97cb-4ee9ac3acd44", + "metadata": {}, + "source": [ + "# Create a version of a tutorial without solutions" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "c37c81f5-0a4a-4e54-b5e1-a70847eafe17", + "metadata": {}, + "outputs": [], + "source": [ + "from copy import deepcopy\n", + "import nbformat\n", + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "df2d36ea-de2e-427e-b040-985378f42f4a", + "metadata": {}, + "outputs": [], + "source": [ + "def has_solution(cell):\n", + " \"\"\"Return True if cell is marked as containing an exercise solution.\"\"\"\n", + " cell_text = cell[\"source\"].replace(\" \", \"\").lower()\n", + " first_line = cell_text.split(\"\\n\")[0]\n", + " return (\n", + " cell_text.startswith(\"#@titlesolution\")\n", + " or \"to_remove\" in first_line\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "36437d86-8afe-4a09-9616-aaba4ba3591e", + "metadata": {}, + "outputs": [], + "source": [ + "# TODO: import \n", + "# def extract_solutions(nb, nb_dir, nb_name):\n", + "# from process_notebooks.py" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "364235e7-9426-44e0-bc35-9e9e21578202", + "metadata": {}, + "outputs": [], + "source": [ + "SOLUTIONS_DIR = \"/Users/ivan/Projects/Minireference/STATSbook/noBSstatsnotebooks/tutorials/solutions\"\n", + "\n", + "def split_off_solutions(srcfilename: str, destfilename: str):\n", + " \"\"\"Load the notebook, adjust headings in markdown cells, and write back.\"\"\"\n", + " with open(srcfilename, 'r', encoding='utf-8') as inf:\n", + " nb = nbformat.read(inf, as_version=4)\n", + "\n", + " for cell in nb.cells:\n", + " if cell.cell_type == 'code' and has_solution(cell):\n", + " cell_source = cell[\"source\"]\n", + "\n", + " cell_source = cell_source.replace(\"@titlesolution\", \"\")\n", + " title = cell_source.splitlines()[0].strip()\n", + " filename = title.replace(\"# \",\"\").replace(\" \",\"_\") + \".py\"\n", + " with open(os.path.join(SOLUTIONS_DIR,filename), \"w\") as solf:\n", + " solf.write(cell_source)\n", + "\n", + " # clear cell\n", + " cell['source'] = \"\"\n", + " if \"outputID\" in cell[\"metadata\"]:\n", + " del cell[\"metadata\"][\"outputId\"]\n", + " if \"outputs\" in cell:\n", + " cell[\"outputs\"] = []\n", + " if \"execution_count\" in cell:\n", + " del cell[\"execution_count\"]\n", + "\n", + " with open(destfilename, 'w', encoding='utf-8') as outf:\n", + " nbformat.write(nb, outf)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "18d69778-a915-4e55-8205-6af438db6a79", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# Example usage\n", + "\n", + "split_off_solutions(\n", + " '../tutorials/python_tutorial.ipynb',\n", + " '../tutorials/python_tutorial2.ipynb'\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3400317d-5016-4dc1-a8aa-70b09f374b5d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tutorials/cut_material.ipynb b/tutorials/cut_material.ipynb index 6902cbbe..3dd309e0 100644 --- a/tutorials/cut_material.ipynb +++ b/tutorials/cut_material.ipynb @@ -120,10 +120,37 @@ "# score, type(score)" ] }, + { + "cell_type": "markdown", + "id": "a838d3b7-b675-40cd-958d-2d7103f9be99", + "metadata": {}, + "source": [ + "Python is a \"civilized\" language\n", + " \n", + " We'll now learn about some of these tools including, \"doc strings\" (help menus) and the different ways at learning\n", + " \n", + " at what attributes and methods are available to use.\n", + "\n", + " \n", + "We'll now learn about some of these tools including,\n", + "\n", + "Above all, Python has a culture of being beginner friendly so \n", + "\n", + "This combination of tools allows programmers to answer common questions about Python objects and functions without leaving the JupyterLab environment. Basically, in Python all the necessary info is accessible directly in the coding environment.\n", + "For example, at the end of this section you'll be able to answer the following questions on your own:\n", + "\n", + "- How many and what type of arguments does the function `print` expect?\n", + "- What kind of optional, keyword arguments does the function `print` accept?\n", + "- What attributes and methods does the Python object `obj` have?\n", + "- What variables and functions are defined in the current namespace?\n", + "\n", + "More than 50% of any programmer's time is spent looking at help info and trying to understand the variables, functions, objects, and methods they are working with, so it's important for you to learn these meta-skills." + ] + }, { "cell_type": "code", "execution_count": null, - "id": "04701230-d46d-4219-973c-57ffcf99ce31", + "id": "af0d6704-8a21-4ff1-abde-fd49307bd349", "metadata": {}, "outputs": [], "source": [] @@ -131,31 +158,353 @@ { "cell_type": "code", "execution_count": null, - "id": "af0d6704-8a21-4ff1-abde-fd49307bd349", + "id": "367f79df-7641-4f9e-9113-7233cd16b8ff", "metadata": {}, "outputs": [], "source": [] }, + { + "cell_type": "markdown", + "id": "4e5327fb-7ab9-4df9-950c-d366770896ef", + "metadata": {}, + "source": [ + "You can also add longer, multi-line comments using triple-quoted text." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "8ef94548-70bc-406c-8752-180ebb7f7730", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nThis is a longer comment,\\nwhich is written on two lines.\\n'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + "This is a longer comment,\n", + "which is written on two lines.\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "id": "5a016467-88b9-45c0-b0d4-56dbf20e84d5", + "metadata": {}, + "source": [ + "The doc-strings we talked about earlier,\n", + "were created by this kind of multi-line strings included in the source code of the functions `abs`, `len`, `sum`, `print`, etc." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5d2df7ae-b1c1-4e45-a702-a247530ad39b", + "metadata": {}, + "outputs": [], + "source": [ + "#### More exceptions " + ] + }, { "cell_type": "code", "execution_count": null, - "id": "367f79df-7641-4f9e-9113-7233cd16b8ff", + "id": "9bb07b04-3ac7-49ed-8394-ee40e542262e", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# ValueError\n", + "# int(\"zz\")\n", + "\n", + "# ZeroDivisionError\n", + "# 5/0\n", + "\n", + "# KeyError\n", + "# d = {}\n", + "# d[\"zz\"]\n", + "\n", + "# ImportError\n", + "# from math import zz\n", + "\n", + "# AttributeError\n", + "# \"hello\".zz" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f5b2242-eb07-4126-af42-6d45fc0b18d7", "metadata": {}, "outputs": [], "source": [] }, + { + "cell_type": "markdown", + "id": "4f61d173-211a-46fc-b3a4-56a3a42c2576", + "metadata": {}, + "source": [ + "### Example: learning about the `print` function\n", + "\n", + "Let's say you're interested to know the options available for the function `print`,\n", + "which we use to print Python expressions." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "c7189390-1a06-401b-97e8-220dc6330218", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# put cursor in the middle of function and press SHIFT+TAB\n", + "print" + ] + }, + { + "cell_type": "markdown", + "id": "4aea108b-ba15-4680-90f3-a4fa7c53484b", + "metadata": {}, + "source": [ + "You know this function accepts a variable and prints it,\n", + "but what other keywords arguments does it take?" + ] + }, + { + "cell_type": "markdown", + "id": "363b01d6-fa14-4cea-a47e-fed55bff8121", + "metadata": {}, + "source": [ + "Use the help() function on `print`" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "82c46a76-f668-41c5-b862-d1a1ec875880", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function print in module builtins:\n", + "\n", + "print(...)\n", + " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", + " \n", + " Prints the values to a stream, or to sys.stdout by default.\n", + " Optional keyword arguments:\n", + " file: a file-like object (stream); defaults to the current sys.stdout.\n", + " sep: string inserted between values, default a space.\n", + " end: string appended after the last value, default a newline.\n", + " flush: whether to forcibly flush the stream.\n", + "\n" + ] + } + ], + "source": [ + "help(print)" + ] + }, + { + "cell_type": "markdown", + "id": "84b9be1b-e1a9-4905-8bc0-0ec24e35b08f", + "metadata": {}, + "source": [ + "Reading the doc string of the `print` function suggests,\n", + "we see `...` for the type of inputs accepted,\n", + "which means `print` accepts multiple arguments.\n", + "\n", + "Here is an example that prints a string, an integer, a floating point number, and a list on the same line." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "8c0e323b-9c6f-431d-bdb8-aab3f6f2e13a", + "metadata": {}, + "outputs": [], + "source": [ + "# print?" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "45cfd3e3-17e8-4c22-9e72-83156264060c", + "metadata": {}, + "outputs": [], + "source": [ + "# print(print.__doc__)" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "5d2df7ae-b1c1-4e45-a702-a247530ad39b", + "id": "9fe10d9b-21d7-48a5-a690-46acbf9f8816", "metadata": {}, "outputs": [], "source": [] }, + { + "cell_type": "markdown", + "id": "e454f38f-296e-4a61-a4b5-fb267385924f", + "metadata": {}, + "source": [ + "#### Application: changing the separator when printing multiple values\n", + "\n", + "We can choose a different separator between arguments of the `print` function\n", + "by specifying the value for the keyword argument `sep`." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "a6cecf99-87b2-404b-83fa-c1f07e566153", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2.3\n" + ] + } + ], + "source": [ + "x = 3\n", + "y = 2.3\n", + "print(x, y)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "89fa112d-42ff-4766-ba0a-44baa991b400", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 --- 2.3\n" + ] + } + ], + "source": [ + "print(x, y, sep=\" --- \")" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "9bb07b04-3ac7-49ed-8394-ee40e542262e", + "id": "7511a44a-d17b-4231-b733-4f3edaab350d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "8791431f-d0e2-49ab-af16-8f9bd5156264", + "metadata": {}, + "source": [ + "### Question 2: Sample standard deviation\n", + "\n", + "The formula for the sample standard seviation of a list of numbers is:\n", + "$$\n", + " \\text{std}(\\textbf{x}) = s\n", + " = \\sqrt{ \\tfrac{1}{n-1}\\sum_{i=1}^n (x_i-\\overline{x})^2 }\n", + " = \\sqrt{ \\tfrac{1}{n-1}\\left[ (x_1-\\overline{x})^2 + (x_2-\\overline{x})^2 + \\cdots + (x_n-\\overline{x})^2\\right]}.\n", + "$$\n", + "\n", + "Note the division is by $(n-1)$ and not $n$. Strange, no? You'll have to wait until stats to see why this is the case.\n", + "\n", + "Write `compute_std(numbers)`: computes the sample standard deviation" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "5e3a7f23-d2fa-40df-929e-6d76cf06d221", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "29.011491975882016" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import math\n", + "\n", + "def std(numbers):\n", + " \"\"\"\n", + " Computes the sample standard deviation (square root of the sample variance)\n", + " using a for loop.\n", + " \"\"\"\n", + " avg = mean(numbers) \n", + " total = 0\n", + " for number in numbers:\n", + " total = total + (number-avg)**2\n", + " var = total/(len(numbers)-1) \n", + " return math.sqrt(var)\n", + "\n", + "numbers = list(range(0,100))\n", + "std(numbers)" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "0f6a10fe-caff-4242-bcd8-402bf742f2e5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "29.011491975882016" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# compare to known good function...\n", + "import statistics\n", + "statistics.stdev(numbers)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b987984-255e-4cc1-af07-818cd41c21db", "metadata": {}, "outputs": [], "source": [] @@ -163,7 +512,47 @@ { "cell_type": "code", "execution_count": null, - "id": "3f5b2242-eb07-4126-af42-6d45fc0b18d7", + "id": "b86a4f62-c68b-4932-83f1-07296ab7813e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c644731-2e37-426a-baac-31f0673b9f22", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "485dc9ca-c2d8-41b8-877c-a0ba643aeef0", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f7acda1b-78e2-4ae6-be65-c23ce03a3b40", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93699e42-d851-4468-953f-8446119d38d9", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "127f22ab-614f-491b-9337-b0216dd4cb24", "metadata": {}, "outputs": [], "source": [] diff --git a/tutorials/python_tutorial.ipynb b/tutorials/python_tutorial.ipynb index 1bd6c808..c0bc36b6 100644 --- a/tutorials/python_tutorial.ipynb +++ b/tutorials/python_tutorial.ipynb @@ -155,7 +155,7 @@ "id": "bef4b786-c7ab-4a5c-822a-0dea036071c1", "metadata": {}, "source": [ - "### Code cells contain Python commands\n", + "## Code cells contain Python commands\n", "\n", "The Python command prompt is where you enter Python commands.\n", "Each of the code cells in this notebook is a command prompt that allows you to enter Python commands and \"run\" them by pressing **SHIFT+ENTER**,\n", @@ -253,7 +253,7 @@ "id": "7cc40c9c-1d3d-4afe-b3d4-24e770ee36b1", "metadata": {}, "source": [ - "#### Your turn to try this!\n", + "## Your turn to try this!\n", "\n", "Try typing in an expression involving numbers and operations in the following cell, then run it by pressing **SHIFT+ENTER** or by using the Play button." ] @@ -607,7 +607,7 @@ } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 1\n", "y = (1+4)**2 - 3\n", "y" ] @@ -728,7 +728,7 @@ } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 2\n", "weight_in_kg = 107\n", "weight_in_lb = 2.2 * weight_in_kg\n", "weight_in_lb" @@ -774,7 +774,7 @@ } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 3\n", "cost = 57.00\n", "taxes = 0.10 * cost\n", "total = cost + taxes\n", @@ -830,7 +830,7 @@ } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 4\n", "C = 20\n", "F = (C * 9/5) + 32\n", "F" @@ -1029,53 +1029,70 @@ "id": "cfe62b3a-2d80-4b26-aab5-d1518d22b1e1", "metadata": {}, "source": [ - "**Exercise**: Display the contents and the type of the other variables we defiend above." + "**Exercise 5**: Display the contents and the type of the other variables we defiend above." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "089a1e45-5d10-43a0-a66f-d06492af0b6f", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "Ellipsis" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "... # replace ... with your answer" + ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 24, "id": "6fe84d2f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "list" + "bool" ] }, - "execution_count": 23, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "#@titlesolution\n", - "above_the_average\n", - "type(above_the_average)\n", + "#@titlesolution Exercise 5\n", + "scores\n", + "type(scores)\n", "\n", "message\n", "type(message)\n", "\n", - "scores\n", - "type(scores)" + "above_the_average\n", + "type(above_the_average)" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "5605f5be-457b-4c0c-8cb5-f332508c9896", + "cell_type": "markdown", + "id": "500eaad4-2296-4ac0-aacf-9cb82f2c5084", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "### Python objects\n", + "\n", + "The value of every variable in Python is an *object*. The function `type` tells you what kind of object it is. The term \"object\" is used in programming to describe variables that are not simple containers for data but have additional information and *affordances* (what you can do with them) attached to them. For example, a `str`ing object knows how to convert itself to capital letters.\n", + "\n", + "Python is an object-oriented language, which means everything in Python is an object. The `int`egers, `float`ing point numbers, `list`s, `str`ings, and `bool`ean are examples of different types of objects. We'll use the generic variable name `obj` to refer to an object of any type." + ] }, { "cell_type": "markdown", @@ -1084,19 +1101,8 @@ "source": [ "# Getting comfortable with Python\n", "\n", - "Python is a \"civilized\" language,\n", - "which means it provides lots of help tools to make learning the language easy for beginners. We'll now learn about some of these tools including,\n", - "\"doc strings\" (help menus) and introspection tools for looking at what attributes and methods are available to use.\n", - "\n", - "This combination of tools allows programmers to answer common questions about Python objects and functions without leaving the JupyterLab environment. Basically, in Python all the necessary info is accessible directly in the coding environment.\n", - "For example, at the end of this section you'll be able to answer the following questions on your own:\n", - "\n", - "- How many and what type of arguments does the function `print` expect?\n", - "- What kind of optional, keyword arguments does the function `print` accept?\n", - "- What attributes and methods does the Python object `obj` have?\n", - "- What variables and functions are defined in the current namespace?\n", - "\n", - "More than 50% of any programmer's time is spent looking at help info and trying to understand the variables, functions, objects, and methods they are working with, so it's important for you to learn these meta-skills." + "The JupyterLab environment provides lots of tools to make learning Python easy for beginners,\n", + "which include documentation (help menus) and interactive ways to explore the properties of variables." ] }, { @@ -1108,17 +1114,16 @@ "source": [ "## Showing the help info\n", "\n", - "Every Python object has a \"doc string\" associated with it, that provides the helpful information about the object.\n", - "There are three equivalent ways to view these docstring of any Python object `obj` (value, variable, function, module, etc.):\n", + "Every Python object has a \"doc string\" (documentation string) associated with it, which provides the help info about this object.\n", + "There are three equivalent ways to view the docstring of any Python object `obj` (value, variable, function, module, etc.):\n", "\n", "- `help(obj)`: prints the docstring of the Python object `obj`\n", - "- `obj?`\n", - "- SHIFT + TAB: while cursor on top of Python variable or function \n", + "- `obj?`: shortcut for `help(obj)`\n", + "- predd **SHIFT+TAB**: while the cursor is on top of Python variable or function inside a code block\n", "\n", "\n", - "There are also other methods for getting more detailed,\n", - "as part of the menu `obj??`, `%psource obj`, `%pdef obj`,\n", - "but you won't need this for now." + "There are also other methods for getting more detailed info about an object like\n", + "`obj??`, `%psource obj`, `%pdef obj`, but we won't need these for now." ] }, { @@ -1126,41 +1131,34 @@ "id": "a003d0e2-f4ff-42c2-a8d1-e534f118cd4f", "metadata": {}, "source": [ - "### Example: learning about the `print` function\n", + "### Example: learning about the `abs` function\n", "\n", - "Let's say you're interested to know the options available for the function `print`,\n", - "which we use to print Python expressions." + "Let's say you're reading some Python code written by a friend,\n", + "and they use the function `abs` in their code.\n", + "Suppose you've never seen this function before,\n", + "and you have no idea what it does." ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 25, "id": "14bcb38d-ba4a-46fe-8d48-4a635a674aaa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "3" ] }, - "execution_count": 24, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# put cursor in the middle of function and press SHIFT+TAB\n", - "print" - ] - }, - { - "cell_type": "markdown", - "id": "5880d053-54ba-454d-bf30-857d4bf0282b", - "metadata": {}, - "source": [ - "You know this function accepts a variable and prints it,\n", - "but what other keywords arguments does it take?" + "abs(-3)" ] }, { @@ -1168,12 +1166,12 @@ "id": "f8dcf334-2304-48d6-a59b-11ddc66764c3", "metadata": {}, "source": [ - "Use the help() function on `print`" + "We can also obtain the same information using the `help()` function on `abs`." ] }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 26, "id": "b9330d95-c29e-4fa6-a176-328808ab74a1", "metadata": {}, "outputs": [ @@ -1181,23 +1179,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "Help on built-in function print in module builtins:\n", + "Help on built-in function abs in module builtins:\n", "\n", - "print(...)\n", - " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", - " \n", - " Prints the values to a stream, or to sys.stdout by default.\n", - " Optional keyword arguments:\n", - " file: a file-like object (stream); defaults to the current sys.stdout.\n", - " sep: string inserted between values, default a space.\n", - " end: string appended after the last value, default a newline.\n", - " flush: whether to forcibly flush the stream.\n", + "abs(x, /)\n", + " Return the absolute value of the argument.\n", "\n" ] } ], "source": [ - "help(print)" + "help(abs)" ] }, { @@ -1205,21 +1196,21 @@ "id": "4d4e163f-a569-4c67-a7ff-e878fdbf5a42", "metadata": {}, "source": [ - "Reading the doc string of the `print` function suggests,\n", - "we see `...` for the type of inputs accepted,\n", - "which means `print` accepts multiple arguments.\n", + "The help menu tells you that `abs(x)` is the absolute value function,\n", + "which is written $|x|$ in math notation, and defined as\n", "\n", - "Here is an example that prints a string, an integer, a floating point number, and a list on the same line." + "$$\n", + " |x| = \\begin{cases} x & \\text{if } x \\geq 0 \\\\ -x & \\text{if } x < 0 \\end{cases}\n", + "$$\n" ] }, { - "cell_type": "code", - "execution_count": 26, - "id": "183134f0-0639-4e71-988e-623887f393a2", + "cell_type": "markdown", + "id": "bfaa403c-d3f2-4d2e-bf18-fb348ae07c51", "metadata": {}, - "outputs": [], "source": [ - "# print?" + "We refer to the help menu associated with an object as its \"doc string\",\n", + "since the information is stored as `obj.__doc__`." ] }, { @@ -1227,82 +1218,28 @@ "execution_count": 27, "id": "ed5f3657-7b0a-4f88-b0b0-c6e8c40d2991", "metadata": {}, - "outputs": [], - "source": [ - "# print(print.__doc__)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f3fed3c6-09af-4e2c-98fc-95143cfb11c6", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "ca994386-bf1d-4116-a8c5-ac48168f23b5", - "metadata": {}, - "source": [ - "#### Application: changing the separator when printing multiple values\n", - "\n", - "We can choose a different separator between arguments of the `print` function\n", - "by specifying the value for the keyword argument `sep`." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "id": "f6ed0b7c-1dad-42c1-8f93-ef4a674d8689", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3 2.3\n" - ] - } - ], - "source": [ - "x = 3\n", - "y = 2.3\n", - "print(x, y)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "id": "c205cfcb-a425-4fd9-90e8-ab3c2c18e702", - "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "3 --- 2.3\n" - ] + "data": { + "text/plain": [ + "'Return the absolute value of the argument.'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print(x, y, sep=\" --- \")" + "abs.__doc__" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "73f5ce60-305d-4d11-9e96-c1f1cc054840", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "9504e23c-9492-4a7b-89c8-6ddee256cfb5", "metadata": {}, "source": [ - "#### Exercises" + "### Exercises" ] }, { @@ -1310,12 +1247,20 @@ "id": "bb0d917e-cf94-4a29-8b17-3476e5953274", "metadata": {}, "source": [ - "**Exercise 1**: print the doc-string of the function `len`." + "**Exercise 6**: Display the doc-string of the function `len`." ] }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, + "id": "e42dc37e-57c5-4690-8ee5-88e0fefa6fe4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 28, "id": "fae81d0d-5ae9-4fc5-8c55-6b761e3e0372", "metadata": {}, "outputs": [ @@ -1332,7 +1277,7 @@ } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 6\n", "help(len)" ] }, @@ -1341,12 +1286,12 @@ "id": "6bf8cec7-ee28-42e1-9661-a7f5aba7df06", "metadata": {}, "source": [ - "**Exercise 2**: print the doc-string of the function `sum`." + "**Exercise 7**: Display the doc-string of the function `sum`." ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 29, "id": "6556a136-c3e8-4cdd-b7aa-78f3bc046c39", "metadata": {}, "outputs": [ @@ -1367,7 +1312,7 @@ } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 7\n", "help(sum)" ] }, @@ -1376,31 +1321,40 @@ "id": "dcb2ebbe-aeb0-4077-8cd1-4035faeed858", "metadata": {}, "source": [ - "**Exercise 3**:\n", - "print the doc strings of other functions we've seen so far: `int`, `float`, `type`, etc." + "**Exercise 8**:\n", + "Display the doc string of for the function `print`." ] }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 30, "id": "c14009f5-a885-456a-9db4-adad29d11c46", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function print in module builtins:\n", + "\n", + "print(...)\n", + " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", + " \n", + " Prints the values to a stream, or to sys.stdout by default.\n", + " Optional keyword arguments:\n", + " file: a file-like object (stream); defaults to the current sys.stdout.\n", + " sep: string inserted between values, default a space.\n", + " end: string appended after the last value, default a newline.\n", + " flush: whether to forcibly flush the stream.\n", + "\n" + ] + } + ], "source": [ - "#@titlesolution\n", - "# help(int)\n", - "# help(float)\n", - "# help(type)" + "#@titlesolution Exercise 8\n", + "help(print)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "093bbe81-6b82-43de-bf64-a09a6bfbfba8", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "48c28226-6c60-4d02-bcb4-aece2ecfa92b", @@ -1416,7 +1370,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 31, "id": "020ea4fd-c984-4723-8d5e-f9969430d631", "metadata": {}, "outputs": [], @@ -1424,60 +1378,19 @@ "# this is a comment" ] }, - { - "cell_type": "markdown", - "id": "d06c1ac5-6725-4603-8566-96be555aeda8", - "metadata": {}, - "source": [ - "You can also add longer, multi-line comments using triple-quoted text." - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "id": "d4b8d166-5d2c-4351-919f-a1eb29b86864", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'\\nThis is a longer comment,\\nwhich is written on two lines.\\n'" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"\"\"\n", - "This is a longer comment,\n", - "which is written on two lines.\n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "id": "c9206f4f-fd04-47f2-9b71-46a4ef7522c1", - "metadata": {}, - "source": [ - "The docstrings we talked about earlier,\n", - "are exactly this kind of multi-line strings included in the source code of the functions `len`, `print`, etc." - ] - }, { "cell_type": "markdown", "id": "1ef8ed01-26ee-4b1e-a8a6-7a8c3b7d4402", "metadata": {}, "source": [ - "**Exercise 4**: replace the `...` in the code block with comments that\n", + "**Exercise 9**: replace the `...` in the code block with comments that\n", "explain the calculation \"adding 10\\% tax to a purchase that costs \\$57\"\n", "that is being computer." ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 32, "id": "a2c68579-c5a8-491d-9d22-9cb0f0bba584", "metadata": {}, "outputs": [ @@ -1487,7 +1400,7 @@ "62.7" ] }, - "execution_count": 35, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -1501,7 +1414,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 33, "id": "7e05651d-bd9f-4501-9a9b-5a39d266b3e4", "metadata": {}, "outputs": [ @@ -1511,13 +1424,13 @@ "62.7" ] }, - "execution_count": 36, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 9\n", "cost = 57.00 # price before taxes\n", "taxes = 0.10 * cost # 10% taxes = 0.1 times price\n", "total = cost + taxes # add price + taxes and store the result in total\n", @@ -1567,7 +1480,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 34, "id": "6a218312-8d28-4a7b-8b25-84bd4862cd4d", "metadata": {}, "outputs": [ @@ -1577,7 +1490,7 @@ "int" ] }, - "execution_count": 37, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -1590,7 +1503,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 35, "id": "5cb8b99c-b5b6-47db-91ce-57ab878f0a11", "metadata": {}, "outputs": [ @@ -1608,7 +1521,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 36, "id": "efd453a8-36b4-43c0-9ccf-d2efbcdf573b", "metadata": {}, "outputs": [ @@ -1618,7 +1531,7 @@ "'3'" ] }, - "execution_count": 39, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -1627,14 +1540,6 @@ "repr(obj)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "c6b4748d-caab-46ab-88d1-ff2f25004895", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "d29a3972-1345-4259-bc0c-8d20ef0b98c8", @@ -1652,7 +1557,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 37, "id": "1b84eed3-8910-4bf4-9a03-a9d3c1aeae49", "metadata": {}, "outputs": [], @@ -1663,33 +1568,18 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 38, "id": "d0bed95d-0a8d-407a-9ae5-9b98020689a2", "metadata": {}, "outputs": [], "source": [ + "# Examples of useful str methods\n", "# message.upper()\n", "# message.lower()\n", "# message.split()\n", "# message.replace(\"everyone\", \"world\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "f6a27b46-5374-4892-94a1-03332c3e7c07", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5e0e680a-57e0-46ac-bd10-53b8e138d5dc", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "296abfc8-919f-4173-b43b-6320ea6b0dd3", @@ -1718,7 +1608,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 39, "id": "3e496261-e0b7-47ec-a8ca-652c2c60f1f4", "metadata": {}, "outputs": [], @@ -1726,22 +1616,6 @@ "# %whos" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "623ac254-9b87-46c9-ac15-3e99cb469dfb", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3a0558bf-74c5-4317-ac7e-f0f338a22369", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "dcc58756-3cae-4794-a770-00b2ffc291b3", @@ -1751,12 +1625,13 @@ "source": [ "## Python error messages\n", "\n", - "Sometimes the command you evaluate will cause an error,\n", + "Sometimes the code you evaluate will cause an error\n", "and Python will print an error message describing the problem it encountered.\n", - "You need to be mentally prepared for these errors, since they can be very discouraging to see.\n", + "You need to be mentally prepared for these errors,\n", + "since they happen a lot and can be very discouraging at first.\n", "The computer doesn't like what you entered.\n", "The output is a big red box,\n", - "that tells you your input is [REJECTED](https://ballislife.com/wp-content/uploads/2014/10/bil-dorsey.jpg)!\n", + "that tells you your input was REJECTED!\n", "\n", "Examples of errors include `SyntaxError`, `ValueError`, etc.\n", "The error messages look scary,\n", @@ -1772,22 +1647,14 @@ "id": "e0b49cc1-1e8d-4bdd-ba5a-9fbc994e2848", "metadata": {}, "source": [ - "Let's look at an example expression that Python cannot compute,\n", + "We'll now look at an example expression that Python cannot compute,\n", "so it raises an exception.\n", - "The code cell below shows an example error that occurs when you ask Pyhton to computer a math expressions that doesn't make sense." - ] - }, - { - "cell_type": "markdown", - "id": "0833813d-acda-41d6-bab8-2179120003be", - "metadata": {}, - "source": [ - "Let's look at an example of an error:" + "The code cell below shows an example error that occurs when you ask Python to compute a math expressions that doesn't make sense." ] }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 40, "id": "99b808a8", "metadata": { "tags": [ @@ -1802,7 +1669,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[43], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m3\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", + "Cell \u001b[0;32mIn[40], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m3\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } @@ -1823,17 +1690,17 @@ "or tried to compute something impossible,\n", "like dividing a number by zero as in the above example.\n", "\n", - "You get an error since you're trying to compute an expression that contains a division by zero. Python tell you a `ZeroDivisionError: division by zero` has occurred.\n", + "You get an error since you're trying to compute an expression that contains a division by zero. Python tells you that a `ZeroDivisionError: division by zero` has occurred.\n", "Indeed it's not possible to divide a number by zero.\n", "\n", - "The way to read these red messages is to focus on the name of the **explanation message that gets printed on the last line**. The error message tells you what you need to fix. The solution will be obvious for typos and syntax errors, but for more complicated situations, you may need to search online to find what the problems is.\n", + "The way to read these red messages is to focus on the name of the **explanation message that gets printed on the last line**. The error message tells you what you need to fix. The solution will be obvious for typos and syntax errors, but for more complicated situations, you may need to search online to find what the problem is.\n", "\n", "In our example, we can fix the `3/0` by replacing it with `3/1`." ] }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 41, "id": "fcfeecb6-3362-493b-a014-769d5a316c0e", "metadata": {}, "outputs": [ @@ -1843,7 +1710,7 @@ "3.0" ] }, - "execution_count": 44, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -1859,17 +1726,19 @@ "tags": [] }, "source": [ - "Here is a list of the most common error messages you are likely to encounter:\n", + "The three most common error messages you are likely to encounter are:\n", "\n", "- `SyntaxError`: you typed in something wrong (usually missing \" ] or some other punctuation)\n", "- `NameError`: Raised when a variable is not found in local or global scope.\n", - "- `KeyError`: Raised when a key is not found in a dictionary.\n", - "- `TypeError`: Raised when a function or operation is applied to an object of incorrect type.\t\n", - "- `ValueError`: Raised when a function gets an argument of correct type but improper value.\n", - "- `ZeroDivisionError`: Raised when the second operand of division or modulo operation is zero.\n", - "- later on we'll also run into: `ImportError` and `AttributeError`\n", + "- `TypeError`: Raised when a function or operation is applied to an object of incorrect type.\n", "\n", - "There are many more error types. You can see a complete list by typing in the command `*Error?`." + "Later on we'll also run into,\n", + "`KeyError` when a key is not found in a dictionary,\n", + "`ValueError` when a function gets an argument of correct type but improper value,\n", + "`ImportError` when importing modules,\n", + "and `AttributeError` when working with objects.\n", + "There are many more error types.\n", + "You can see a complete list by typing in the command `*Error?`." ] }, { @@ -1879,61 +1748,41 @@ "tags": [] }, "source": [ - "**Exercise**: let's break Python! 🔨🔨🔨🔨🔨🔨 \n", - "Try typing in Python commands that would causes one of the above errors." + "**Exercise 10**: let's break Python! 🔨🔨🔨🔨🔨🔨 \n", + "Try typing in Python commands that causes each of these exceptions:\n", + " \n", + " 1. `SyntaxError` (hint: write an incomplete list expression)\n", + " 2. `NameError` (hint: try referring to a variable that doesn't exist)\n", + " 3. `TypeError` (hint: use the `abs` function on a non-number)" ] }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 42, "id": "9aebce93-4124-4918-8f69-a69c457e3c9e", "metadata": {}, "outputs": [], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 10\n", "\n", - "# SyntaxError\n", + "# SyntaxError if you forget to close the [ when creating a list\n", "# [1\n", "\n", - "# NameError\n", - "# zz + 3\n", - "\n", - "# TypeError\n", - "# sum(3)\n", + "# SyntaxError when using a space\n", + "# 3 4\n", "\n", - "# ValueError\n", - "# int(\"zz\")\n", "\n", - "# ZeroDivisionError\n", - "# 5/0\n", + "# NameError when referring to a variable that doesn't exist\n", + "# zz + 3\n", "\n", - "# KeyError\n", - "# d = {}\n", - "# d[\"zz\"]\n", "\n", - "# ImportError\n", - "# from math import zz\n", + "# TypeError computing the absolute value of a string\n", + "# abs(\"zz\")\n", "\n", - "# AttributeError\n", - "# \"hello\".zz" + "# TypeError computing the sum of a single number\n", + "# sum(3)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "ad657513-bb32-4c26-bcde-2c9aaa7cb63a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "64cfa87a-71d7-4ab7-a726-b71738be036a", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "0aab73fc-76a0-4122-ab29-ebc84490b374", @@ -1947,7 +1796,8 @@ "\n", "- main page https://docs.python.org/3/\n", "- tutorial https://docs.python.org/3/tutorial/index.html\n", - "- example (built in functions and types from Lessons 1 through 3)\n", + "- examples:\n", + " - abs https://docs.python.org/3/library/functions.html#abs\n", " - len https://docs.python.org/3/library/functions.html#len\n", " - type https://docs.python.org/3/library/functions.html#type\n", " - print https://docs.python.org/3/library/functions.html#print \n", @@ -1962,220 +1812,448 @@ "I encourage you to browse the site to familiarize yourself with the information that is available.\n", "\n", "Usually when you do a google search, the official Python docs will show up on the first page of results.\n", - "Make sure to prefer reading the official documentation instead of other \"learn Python\" websites\n", + "Make sure to read the official documentation instead of other \"learn Python\" websites\n", "(currently the first few google search results that show up point to SEO-optimized, spammy, advertisementfull, websites \n", " which are inferior to the official documentation).\n", "Always prefer the official docs (even if it appears lower in the list of results on the page).\n", - "Stack overflow discussions are also a good place to find answers to common Python questions." + "[Stack overflow](https://stackoverflow.com/questions/tagged/python) discussions are also a good place to find answers to common Python questions." ] }, { - "cell_type": "code", - "execution_count": null, - "id": "9d3eeda6-1fd0-4751-9b4c-6dbcc9e0ee44", + "cell_type": "markdown", + "id": "edf53b4c-aaba-48c9-9a04-a573ec525949", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "\n", + "# Functions\n", + "\n", + "Functions!\n", + "Finally we get to the good stuff!\n", + "Functions are an important building block in programming,\n", + "since they allow you to encapsulate any multi-step program or procedure as reusable piece of functionality. You can think of functions as reusable chunks of code that can be defined once, and used multiple times by \"calling\" them with different arguments.\n", + "\n", + "\n", + "Let's do a quick review the concepts of a function in mathematics,\n", + "since the Python syntax for functions is inspired by math functions.\n", + "The convention in math to call function $f$,\n", + "denote the inputs of the function $x$, and its outputs $y$:\n", + "\n", + "$$ y = f(x) = \\text{some expression involving } x $$\n", + "\n", + "Note we defined the function $f$ by simply writing some expression we need to compute, that depends on the input $x$. For example $f(x) = 2x+3$." + ] }, { - "cell_type": "code", - "execution_count": null, - "id": "f79106be-94a3-4246-a188-69e0498b60ab", + "cell_type": "markdown", + "id": "4a597cb4-750b-4a28-940c-ef39ec3fe89b", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "## Python functions\n", + "\n", + "Functions in Python are similar to functions in math: \n", + "a Python function takes certain inputs and produces certain outputs.\n", + "We define the function called `f` using the following syntax:\n", + "\n", + "```Python\n", + "def f(x):\n", + " \n", + " return y\n", + "```\n", + "\n", + "There are a lot of things going on in this code example,\n", + "so let's go over the code line-by-line and explain all the new elements of syntax:\n", + "\n", + "- The Python keyword `def` is used to declare a function definition.\n", + "- Next we see the name of the function `f`\n", + "- Next we must specify the inputs of the function (a.k.a. arguments) inside parentheses.\n", + " In this example, the function `f`,\n", + " it takes only a single argument `x`.\n", + "- The colon `:` indicates the beginning of a code block,\n", + " which contains the the function body.\n", + " The function body consists of one or more lines of code,\n", + " indented by four spaces,\n", + " just like the other types of code blocks we have seen.\n", + "- The final line of the function `f`,\n", + " we use the `return` statement to indicate the output of the function.\n", + " The return statement is usually the last line in the function body.\n", + " \n" + ] }, { - "cell_type": "code", - "execution_count": null, - "id": "f834828d-3726-4682-88ab-24b557b952b7", + "cell_type": "markdown", + "id": "bc6cecdd-23b2-4a2d-8aed-4efd0beddb80", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "#### Example 1\n", + "\n", + "A first example of a simple math-like function. The function is called `f`,\n", + "takes numbers as inputs, and produces numbers as outputs:" + ] }, { - "cell_type": "markdown", - "id": "e0752b0b-2d68-4a03-8a9f-539f566a1e17", + "cell_type": "code", + "execution_count": 43, + "id": "6d4e3530", "metadata": {}, + "outputs": [], "source": [ - "# Lists and for loops\n" + "def f(x):\n", + " return 2*x + 3" ] }, { "cell_type": "markdown", - "id": "499f5589-9568-43a3-ba6c-ed43b3df568e", + "id": "2d867494-881b-48f9-8a7e-6531f272f16d", "metadata": {}, "source": [ - "## Lists\n", - "\n", - "To create a list:\n", - "- start with an opening square bracket `[` ,\n", - "- then put the elements of the list separated by commas `,`\n", - "- finally close the square bracket `]`\n", - "\n", - "For example, `scores` is a list of `int`s that we've seen in several examples before." + "To call the function `f`,\n", + "we use the function name,\n", + "the pass in the argument(s) of the function in parentheses." ] }, { "cell_type": "code", - "execution_count": 46, - "id": "f76410af", + "execution_count": 44, + "id": "680f7ed9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[61, 79, 98, 72]" + "23" ] }, - "execution_count": 46, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "scores = [61, 79, 98, 72]\n", - "scores" + "f(10)" ] }, { "cell_type": "markdown", - "id": "cba309bc-7968-4f12-a0e1-83a1cfe57a72", + "id": "cc1b3bc1-9947-4d62-a702-da823d14e93f", "metadata": {}, "source": [ - "A list container has a length,\n", - "which you can obtain by calling the `len` function." + "### Example 2" ] }, { "cell_type": "code", - "execution_count": 47, - "id": "3847db1a", + "execution_count": 45, + "id": "42d6774a", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 47, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "len(scores)" + "# TODO\n" ] }, { "cell_type": "markdown", - "id": "a17d4c4c-67ac-43dc-8772-7a9a7b9cc48b", + "id": "f0a3cf14-dc17-498b-9e51-6d2d23b58410", "metadata": {}, "source": [ - "You check if a list contains a certain element using the `in` operator:" + "## Statistics functions\n", + "\n", + "Your turn to play with lists now!\n", + "Complete the code required to implement the functions `mean` and `std` below." + ] + }, + { + "cell_type": "markdown", + "id": "9df04daf-4d51-49f5-a5db-ce7c2aece223", + "metadata": {}, + "source": [ + "### Computing the mean\n", + "\n", + "The formula for the mean of a list of numbers $[x_1, x_2, \\ldots, x_n]$ is:\n", + "$$\n", + " \\text{mean} = \\overline{x}\n", + " = \\frac{1}{n}\\sum_{i=1}^n x_i\n", + " = \\tfrac{1}{n} \\left[ x_1 + x_2 + \\cdots + x_n \\right].\n", + "$$\n", + "\n", + "\n", + "Write the function `mean(numbers)`: a function that computes the mean of a list of numbers" ] }, { "cell_type": "code", - "execution_count": 48, - "id": "c10641d1-0b67-4848-81a2-03a3f7fafb39", + "execution_count": 46, + "id": "a2752b71", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "100.5" ] }, - "execution_count": 48, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "98 in scores" + "def mean(numbers):\n", + " \"\"\"\n", + " Computes the mean of the `numbers` list using a for loop.\n", + " \"\"\"\n", + " total = 0\n", + " for number in numbers:\n", + " total = total + number\n", + " return total / len(numbers) \n", + "\n", + "\n", + "mean([100,101])" ] }, { "cell_type": "code", "execution_count": null, - "id": "a1150b3a-50c8-4461-8002-66122aec7792", + "id": "898cdb91", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e7cd103d-861c-4be0-acc4-db1bf6d890f1", + "id": "6b6b939c-7e65-4e3c-a9c0-7255e5b13592", "metadata": {}, "source": [ - "### List access syntax\n", + "#### Exercise 12\n", "\n", - "Elements of a the list are accessed using the square brackets `[]`,\n", - "where `` is the the `0`-based index of the element we want to access:\n", - "- The first element has index `0`.\n", - "- The second element has index `1`.\n", - "- The last element has index equal to the length of the list minus one." + "Write a Python function called `temp_C_to_F` that converts C to F" ] }, { "cell_type": "code", - "execution_count": 49, - "id": "ae78304a", + "execution_count": 47, + "id": "cbdb8946-991b-4851-b9fd-043ebf245d92", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "61" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "# first element in the list scores\n", - "scores[0]" + "def temp_C_to_F(temp_C):\n", + " \"\"\"\n", + " Convert the temprate `temp_C` to Farenheit.\n", + " \"\"\"\n", + " pass # replace `pass` with your code" ] }, { "cell_type": "code", - "execution_count": 50, - "id": "a89f88b9-88ce-4836-a78a-a4fda0c8762a", + "execution_count": 48, + "id": "78964c7e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "79" + "100.03999999999999" ] }, - "execution_count": 50, + "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# second element in the list scores\n", - "scores[1]" + "#@titlesolution Exercise 12\n", + "def temp_C_to_F(temp_C):\n", + " \"\"\"\n", + " Convert the temprate `temp_C` to Farenheit.\n", + " \"\"\"\n", + " temp_F = (temp_C * 9/5) + 32\n", + " return temp_F\n", + "\n", + "temp_C_to_F(37.8)" ] }, { - "cell_type": "code", - "execution_count": 51, - "id": "518f8065-6447-4398-bc39-d65cd5b3536c", + "cell_type": "markdown", + "id": "e0752b0b-2d68-4a03-8a9f-539f566a1e17", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "72" - ] - }, - "execution_count": 51, + "source": [ + "# Lists and for loops\n" + ] + }, + { + "cell_type": "markdown", + "id": "499f5589-9568-43a3-ba6c-ed43b3df568e", + "metadata": {}, + "source": [ + "## Lists\n", + "\n", + "To create a list:\n", + "- start with an opening square bracket `[` ,\n", + "- then put the elements of the list separated by commas `,`\n", + "- finally close the square bracket `]`\n", + "\n", + "For example, `scores` is a list of `int`s that we've seen in several examples before." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "f76410af", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[61, 79, 98, 72]" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scores = [61, 79, 98, 72]\n", + "scores" + ] + }, + { + "cell_type": "markdown", + "id": "cba309bc-7968-4f12-a0e1-83a1cfe57a72", + "metadata": {}, + "source": [ + "A list container has a length,\n", + "which you can obtain by calling the `len` function." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "3847db1a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(scores)" + ] + }, + { + "cell_type": "markdown", + "id": "a17d4c4c-67ac-43dc-8772-7a9a7b9cc48b", + "metadata": {}, + "source": [ + "You check if a list contains a certain element using the `in` operator:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "c10641d1-0b67-4848-81a2-03a3f7fafb39", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "98 in scores" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1150b3a-50c8-4461-8002-66122aec7792", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "e7cd103d-861c-4be0-acc4-db1bf6d890f1", + "metadata": {}, + "source": [ + "### List access syntax\n", + "\n", + "Elements of a the list are accessed using the square brackets `[]`,\n", + "where `` is the the `0`-based index of the element we want to access:\n", + "- The first element has index `0`.\n", + "- The second element has index `1`.\n", + "- The last element has index equal to the length of the list minus one." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "ae78304a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "61" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# first element in the list scores\n", + "scores[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "a89f88b9-88ce-4836-a78a-a4fda0c8762a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "79" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# second element in the list scores\n", + "scores[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "518f8065-6447-4398-bc39-d65cd5b3536c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "72" + ] + }, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -2195,7 +2273,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 55, "id": "6722a21a-f1bb-407f-a978-b8dba5ab838e", "metadata": {}, "outputs": [ @@ -2205,7 +2283,7 @@ "72" ] }, - "execution_count": 52, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -2239,7 +2317,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 56, "id": "a6110de9", "metadata": {}, "outputs": [ @@ -2249,7 +2327,7 @@ "[61, 79, 98]" ] }, - "execution_count": 53, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -2303,7 +2381,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 57, "id": "feb2d894", "metadata": {}, "outputs": [ @@ -2313,7 +2391,7 @@ "[61, 72, 79, 98]" ] }, - "execution_count": 54, + "execution_count": 57, "metadata": {}, "output_type": "execute_result" } @@ -2334,7 +2412,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 58, "id": "780efcce", "metadata": {}, "outputs": [ @@ -2344,7 +2422,7 @@ "[61, 72, 79, 98, 22]" ] }, - "execution_count": 55, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -2364,7 +2442,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 59, "id": "15a74b2b", "metadata": {}, "outputs": [ @@ -2374,7 +2452,7 @@ "22" ] }, - "execution_count": 56, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -2402,7 +2480,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 60, "id": "407ec209", "metadata": {}, "outputs": [ @@ -2412,7 +2490,7 @@ "[98, 79, 72, 61]" ] }, - "execution_count": 57, + "execution_count": 60, "metadata": {}, "output_type": "execute_result" } @@ -2446,7 +2524,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 61, "id": "3a2e521a", "metadata": {}, "outputs": [], @@ -2467,7 +2545,7 @@ "id": "304cffa3-cfad-415b-8639-47f85d51bade", "metadata": {}, "source": [ - "**Exercise**: The default behaviour of the method `.sort()` is to \n", + "**Exercise 11**: The default behaviour of the method `.sort()` is to \n", "sort the elements in *increasing* order.\n", "Suppose you want sort the elements in *decreasing* order instead.\n", "You can pass a keyword argument to the method `.sort()`\n", @@ -2479,7 +2557,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 62, "id": "408fa179-c9e9-4119-8c68-1b373436e7f6", "metadata": {}, "outputs": [ @@ -2489,7 +2567,7 @@ "[61, 72, 79, 98]" ] }, - "execution_count": 59, + "execution_count": 62, "metadata": {}, "output_type": "execute_result" } @@ -2501,7 +2579,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 63, "id": "7dacec28-0f47-43b7-b105-80b486fe8d05", "metadata": {}, "outputs": [ @@ -2511,13 +2589,13 @@ "[98, 79, 72, 61]" ] }, - "execution_count": 60, + "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 11\n", "# help(scores.sort)\n", "scores.sort(reverse=True)\n", "scores" @@ -2549,7 +2627,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 64, "id": "562fa828-7888-4aad-93e2-c919ff86d022", "metadata": {}, "outputs": [], @@ -2561,7 +2639,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 65, "id": "d1731890-e68b-44ee-ace3-3ba0ab82cad7", "metadata": {}, "outputs": [], @@ -2620,7 +2698,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 66, "id": "56002dd2", "metadata": {}, "outputs": [ @@ -2683,7 +2761,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 67, "id": "dfcd2e70", "metadata": {}, "outputs": [ @@ -2693,7 +2771,7 @@ "77.5" ] }, - "execution_count": 64, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } @@ -2718,7 +2796,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 68, "id": "6961867c", "metadata": {}, "outputs": [ @@ -2821,7 +2899,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 69, "id": "0366153c-0d85-464b-b258-f23bbaa2f379", "metadata": {}, "outputs": [ @@ -2831,7 +2909,7 @@ "[1, 4, 9, 16, 25]" ] }, - "execution_count": 66, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } @@ -2844,423 +2922,29 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 70, "id": "3bef5387-1393-4cb5-9d80-3c684a9651f0", - "metadata": {}, - "outputs": [], - "source": [ - "# # ALT. using the `range` function\n", - "# numbers = range(1,6)\n", - "# squares = [n**2 for n in numbers]\n", - "# squares" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "18dcdcc5-701d-4ce5-8acc-87a40d1a7e9d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5c315aa6-2183-406b-9450-aa14a88597ac", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f2643d3c-c79e-47b5-94ac-b778294c77ee", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fdd0a674-7150-4c0f-8bb9-9e8de0df2270", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "455f2656-a69c-43ef-a35d-36b774e807e4", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "da98ba3b-f72c-4515-a300-58303f2be1c3", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cb2ade48-e5ac-42d0-a41b-bd6fc4a4e780", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6142c785-8908-467d-9764-c35d0367e604", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "c4659d87-1def-459a-bf1a-1c099bd03168", - "metadata": {}, - "source": [ - "\n", - "# Functions\n", - "\n", - "Functions!\n", - "Finally we get to the good stuff!\n", - "Functions are an important building block in programming,\n", - "since they allow you to encapsulate any multi-step program or procedure as reusable piece of functionality. You can think of functions as reusable chunks of code that can be defined once, and used multiple times by \"calling\" them with different arguments.\n", - "\n", - "\n", - "Let's do a quick review the concepts of a function in mathematics,\n", - "since the Python syntax for functions is inspired by math functions.\n", - "The convention in math to call function $f$,\n", - "denote the inputs of the function $x$, and its outputs $y$:\n", - "\n", - "$$ y = f(x) = \\text{some expression involving } x $$\n", - "\n", - "Note we defined the function $f$ by simply writing some expression we need to compute, that depends on the input $x$. For example $f(x) = 2x+3$." - ] - }, - { - "cell_type": "markdown", - "id": "765c8d71-7ab2-4cf3-a949-e364dfb38be7", - "metadata": {}, - "source": [ - "## Python functions\n", - "\n", - "Functions in Python are similar to functions in math: \n", - "a Python function takes certain inputs and produces certain outputs.\n", - "We define the function called `f` using the following syntax:\n", - "\n", - "```Python\n", - "def f(x):\n", - " \n", - " return y\n", - "```\n", - "\n", - "There are a lot of things going on in this code example,\n", - "so let's go over the code line-by-line and explain all the new elements of syntax:\n", - "\n", - "- The Python keyword `def` is used to declare a function definition.\n", - "- Next we see the name of the function `f`\n", - "- Next we must specify the inputs of the function (a.k.a. arguments) inside parentheses.\n", - " In this example, the function `f`,\n", - " it takes only a single argument `x`.\n", - "- The colon `:` indicates the beginning of a code block,\n", - " which contains the the function body.\n", - " The function body consists of one or more lines of code,\n", - " indented by four spaces,\n", - " just like the other types of code blocks we have seen.\n", - "- The final line of the function `f`,\n", - " we use the `return` statement to indicate the output of the function.\n", - " The return statement is usually the last line in the function body.\n", - " \n" - ] - }, - { - "cell_type": "markdown", - "id": "68efd802-86b5-44b7-928b-cf6d883e2141", - "metadata": {}, - "source": [ - "#### Example 1\n", - "\n", - "A first example of a simple math-like function. The function is called `f`,\n", - "takes numbers as inputs, and produces numbers as outputs:" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "id": "6d4e3530", - "metadata": {}, - "outputs": [], - "source": [ - "def f(x):\n", - " return 2*x + 3" - ] - }, - { - "cell_type": "markdown", - "id": "9be06b8b-0ab3-4f52-9927-95d1f939ca39", - "metadata": {}, - "source": [ - "To call the function `f`,\n", - "we use the function name,\n", - "the pass in the argument(s) of the function in parentheses." - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "id": "680f7ed9", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "23" - ] - }, - "execution_count": 69, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f(10)" - ] - }, - { - "cell_type": "markdown", - "id": "e5a345e3", - "metadata": {}, - "source": [ - "### Example 2" - ] - }, - { - "cell_type": "code", - "execution_count": 70, - "id": "42d6774a", - "metadata": {}, - "outputs": [], - "source": [ - "# TODO\n" - ] - }, - { - "cell_type": "markdown", - "id": "cbf57020-6506-4132-9212-740f1575a18e", - "metadata": {}, - "source": [ - "## Statistics functions\n", - "\n", - "Your turn to play with lists now!\n", - "Complete the code required to implement the functions `mean` and `std` below." - ] - }, - { - "cell_type": "markdown", - "id": "7be9064c-2ad6-4743-bcf2-929ba5e9f11d", - "metadata": {}, - "source": [ - "### Question 1: Mean\n", - "\n", - "The formula for the mean of a list of numbers $[x_1, x_2, \\ldots, x_n]$ is:\n", - "$$\n", - " \\text{mean} = \\overline{x}\n", - " = \\frac{1}{n}\\sum_{i=1}^n x_i\n", - " = \\tfrac{1}{n} \\left[ x_1 + x_2 + \\cdots + x_n \\right].\n", - "$$\n", - "\n", - "\n", - "Write the function `mean(numbers)`: a function that computes the mean of a list of numbers" - ] - }, - { - "cell_type": "code", - "execution_count": 71, - "id": "a2752b71", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "100.5" - ] - }, - "execution_count": 71, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def mean(numbers):\n", - " \"\"\"\n", - " Computes the mean of the `numbers` list using a for loop.\n", - " \"\"\"\n", - " total = 0\n", - " for number in numbers:\n", - " total = total + number\n", - " return total / len(numbers) \n", - "\n", - "\n", - "mean([100,101])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "898cdb91", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "b7ba0463-a0dd-4b80-b0a9-eae8d9627e92", - "metadata": {}, - "source": [ - "### Question 2: Sample standard deviation\n", - "\n", - "The formula for the sample standard seviation of a list of numbers is:\n", - "$$\n", - " \\text{std}(\\textbf{x}) = s\n", - " = \\sqrt{ \\tfrac{1}{n-1}\\sum_{i=1}^n (x_i-\\overline{x})^2 }\n", - " = \\sqrt{ \\tfrac{1}{n-1}\\left[ (x_1-\\overline{x})^2 + (x_2-\\overline{x})^2 + \\cdots + (x_n-\\overline{x})^2\\right]}.\n", - "$$\n", - "\n", - "Note the division is by $(n-1)$ and not $n$. Strange, no? You'll have to wait until stats to see why this is the case.\n", - "\n", - "Write `compute_std(numbers)`: computes the sample standard deviation" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "id": "d9a96ad2", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "29.011491975882016" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import math\n", - "\n", - "def std(numbers):\n", - " \"\"\"\n", - " Computes the sample standard deviation (square root of the sample variance)\n", - " using a for loop.\n", - " \"\"\"\n", - " avg = mean(numbers) \n", - " total = 0\n", - " for number in numbers:\n", - " total = total + (number-avg)**2\n", - " var = total/(len(numbers)-1) \n", - " return math.sqrt(var)\n", - "\n", - "numbers = list(range(0,100))\n", - "std(numbers)" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "id": "f3dedc7e", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "29.011491975882016" - ] - }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# compare to known good function...\n", - "import statistics\n", - "statistics.stdev(numbers)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "45e1a6c2", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "b86d8ef3", - "metadata": {}, - "source": [ - "#### Exercise 2\n", - "\n", - "Write a Python function called `temp_C_to_F` that converts C to F" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "id": "cbdb8946-991b-4851-b9fd-043ebf245d92", - "metadata": {}, - "outputs": [], - "source": [ - "def temp_C_to_F(temp_C):\n", - " \"\"\"\n", - " Convert the temprate `temp_C` to Farenheit.\n", - " \"\"\"\n", - " pass # replace `pass` with your code" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "id": "78964c7e", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "100.03999999999999" - ] - }, - "execution_count": 75, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "#@titlesolution\n", - "def temp_C_to_F(temp_C):\n", - " \"\"\"\n", - " Convert the temprate `temp_C` to Farenheit.\n", - " \"\"\"\n", - " temp_F = (temp_C * 9/5) + 32\n", - " return temp_F\n", - "\n", - "temp_C_to_F(37.8)" + "metadata": {}, + "outputs": [], + "source": [ + "# # ALT. using the `range` function\n", + "# numbers = range(1,6)\n", + "# squares = [n**2 for n in numbers]\n", + "# squares" ] }, { "cell_type": "code", "execution_count": null, - "id": "6e88420a-7e02-4d34-b5e8-6f00bd4aaef4", + "id": "18dcdcc5-701d-4ce5-8acc-87a40d1a7e9d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c315aa6-2183-406b-9450-aa14a88597ac", "metadata": {}, "outputs": [], "source": [] @@ -3268,7 +2952,7 @@ { "cell_type": "code", "execution_count": null, - "id": "36c71805-e0ae-410b-9fa0-51dc75f8c066", + "id": "6142c785-8908-467d-9764-c35d0367e604", "metadata": {}, "outputs": [], "source": [] @@ -3305,7 +2989,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 71, "id": "cacec474-dbdf-4dd2-9ae9-c2f73266ffc6", "metadata": {}, "outputs": [ @@ -3315,7 +2999,7 @@ "'Hello julie'" ] }, - "execution_count": 76, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -3328,7 +3012,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 72, "id": "4ac8448a-7481-4ecf-96fe-48777dc3aa38", "metadata": {}, "outputs": [ @@ -3338,7 +3022,7 @@ "'Hi Julie Tremblay!'" ] }, - "execution_count": 77, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -3377,7 +3061,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 73, "id": "b4a766d5-8f1b-4973-a6c1-8fdfc28140cc", "metadata": {}, "outputs": [ @@ -3387,7 +3071,7 @@ "'abcdefghijklmnopqrstuvwxyz'" ] }, - "execution_count": 78, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -3399,7 +3083,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 74, "id": "22b75d6b-5459-4548-81d5-50bd9f1b7dbe", "metadata": {}, "outputs": [ @@ -3409,7 +3093,7 @@ "26" ] }, - "execution_count": 79, + "execution_count": 74, "metadata": {}, "output_type": "execute_result" } @@ -3429,7 +3113,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 75, "id": "6fbe60a9-7cb4-445b-8e60-51f24aaf30d3", "metadata": {}, "outputs": [ @@ -3439,7 +3123,7 @@ "'a'" ] }, - "execution_count": 80, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -3458,7 +3142,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 76, "id": "b85fb88e-75a9-4e15-8d38-d0d035f8de4c", "metadata": {}, "outputs": [ @@ -3468,7 +3152,7 @@ "'b'" ] }, - "execution_count": 81, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -3487,7 +3171,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 77, "id": "e91868f7-65b6-4914-85fb-37f593b469b9", "metadata": {}, "outputs": [ @@ -3497,7 +3181,7 @@ "'z'" ] }, - "execution_count": 82, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -3517,7 +3201,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 78, "id": "14a8ffb8-f13d-4a86-b157-e2a4ef608b2b", "metadata": {}, "outputs": [ @@ -3527,7 +3211,7 @@ "'z'" ] }, - "execution_count": 83, + "execution_count": 78, "metadata": {}, "output_type": "execute_result" } @@ -3548,7 +3232,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 79, "id": "1f73b115-bbca-43a0-a9bc-ce7913d28624", "metadata": {}, "outputs": [ @@ -3558,7 +3242,7 @@ "'abcd'" ] }, - "execution_count": 84, + "execution_count": 79, "metadata": {}, "output_type": "execute_result" } @@ -3614,7 +3298,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 80, "id": "83c8e652-cf92-4870-b6f5-5f90a351adfd", "metadata": {}, "outputs": [ @@ -3624,7 +3308,7 @@ "str" ] }, - "execution_count": 85, + "execution_count": 80, "metadata": {}, "output_type": "execute_result" } @@ -3635,7 +3319,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 81, "id": "cf095c04-70bd-483c-a995-c7cfb3c157b5", "metadata": {}, "outputs": [ @@ -3645,7 +3329,7 @@ "42.5" ] }, - "execution_count": 86, + "execution_count": 81, "metadata": {}, "output_type": "execute_result" } @@ -3657,7 +3341,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 82, "id": "d5233dd6-dd80-4fe4-9334-b3cbdeb3e3fa", "metadata": {}, "outputs": [ @@ -3667,7 +3351,7 @@ "float" ] }, - "execution_count": 87, + "execution_count": 82, "metadata": {}, "output_type": "execute_result" } @@ -3689,7 +3373,7 @@ "id": "34c1ceb9-35a9-47d2-81b9-706c677b3314", "metadata": {}, "source": [ - "#### Exercise 6: compute the sum of two strings\n", + "#### Exercise 13: compute the sum of two strings\n", "\n", "Suppose we're given two numbers $m$ and $n$ and we want to compute their sum $m+n$.\n", "The two numbers are given to use given expressed as strings." @@ -3697,7 +3381,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 83, "id": "66eab1bc-a797-4d00-8aa2-d615eb492bec", "metadata": {}, "outputs": [ @@ -3727,7 +3411,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 84, "id": "b32fbd79-ef98-4af5-bb32-d441baea5407", "metadata": {}, "outputs": [ @@ -3737,7 +3421,7 @@ "'2.13.4'" ] }, - "execution_count": 89, + "execution_count": 84, "metadata": {}, "output_type": "execute_result" } @@ -3768,7 +3452,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 85, "id": "2e222e82-84b1-4640-8522-3693f3965a7a", "metadata": {}, "outputs": [ @@ -3786,13 +3470,13 @@ "5.5" ] }, - "execution_count": 90, + "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 13\n", "mfloat = float(mstr)\n", "nfloat = float(nstr)\n", "print(\"The variable mfloat has value\", mfloat, \"and type\", type(mfloat))\n", @@ -3808,13 +3492,13 @@ "id": "5ce68f29-4825-4430-8798-d21f1f57cad9", "metadata": {}, "source": [ - "**Exercise** write the Python code that converts a list of string variables `prices_str`\n", + "**Exercise 14**. Write the Python code that converts a list of string variables `prices_str`\n", "to floating point numbers and add them together." ] }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 86, "id": "20ceb250-9718-4446-8c64-fe3b9defe3d8", "metadata": {}, "outputs": [], @@ -3826,7 +3510,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 87, "id": "8c9174d8-dff1-4b0e-b2a2-f983ecadeb34", "metadata": {}, "outputs": [ @@ -3836,13 +3520,13 @@ "65.6" ] }, - "execution_count": 92, + "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 14\n", "prices_str = [\"22.2\", \"10.1\", \"33.3\"]\n", "prices_float = [float(price) for price in prices_str]\n", "sum(prices_float)" @@ -3871,7 +3555,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 88, "id": "ddae232f-1bee-4264-b2bd-cc845ab06c1a", "metadata": {}, "outputs": [ @@ -3881,7 +3565,7 @@ "True" ] }, - "execution_count": 93, + "execution_count": 88, "metadata": {}, "output_type": "execute_result" } @@ -3909,7 +3593,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 89, "id": "ee093a89-c0e8-4697-b851-730266148e36", "metadata": {}, "outputs": [ @@ -3919,7 +3603,7 @@ "True" ] }, - "execution_count": 94, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } @@ -3950,7 +3634,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 90, "id": "aff62239-c311-4639-bcbb-3a67d617318a", "metadata": {}, "outputs": [ @@ -3972,7 +3656,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 91, "id": "bb67abe5-01e6-413b-9f24-863e7270cca3", "metadata": {}, "outputs": [ @@ -4002,7 +3686,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 92, "id": "b0cb5ac6-acdc-4af4-b953-c2c5f072d53f", "metadata": {}, "outputs": [ @@ -4053,7 +3737,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 93, "id": "a3fd0d1a-b256-495a-b747-b895a42e91fc", "metadata": {}, "outputs": [ @@ -4063,7 +3747,7 @@ "(True, False, False, False)" ] }, - "execution_count": 98, + "execution_count": 93, "metadata": {}, "output_type": "execute_result" } @@ -4074,7 +3758,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 94, "id": "0204a343-689e-475e-82f2-bcccfea7f47d", "metadata": {}, "outputs": [ @@ -4084,7 +3768,7 @@ "(True, True, True, False)" ] }, - "execution_count": 99, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -4095,7 +3779,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 95, "id": "b517fd0f", "metadata": {}, "outputs": [ @@ -4105,7 +3789,7 @@ "True" ] }, - "execution_count": 100, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -4117,7 +3801,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 96, "id": "0df03371-779f-4aa9-afdc-6c135f4fe26c", "metadata": {}, "outputs": [ @@ -4127,7 +3811,7 @@ "False" ] }, - "execution_count": 101, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -4149,7 +3833,7 @@ "id": "83ff1fa9-c72d-4590-af3b-d54000ddb337", "metadata": {}, "source": [ - "**Exercise 1**\n", + "**Exercise 15**.\n", "The phase of water (at sea-level pressure = 1 atm = 101.3 kPa = 14.7 psi) ,\n", "depends on its temperature `temp`.\n", "The three possible phases of water are `\"gas\"` (water vapour), `\"liquid\"` (water), and `\"solid\"` (ice).\n", @@ -4171,7 +3855,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 97, "id": "e9ff9df7-38b0-4812-8cc6-ef053f725797", "metadata": {}, "outputs": [], @@ -4194,7 +3878,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 98, "id": "72f83a8b-264d-4413-9a74-cc707408a977", "metadata": {}, "outputs": [ @@ -4207,7 +3891,7 @@ } ], "source": [ - "#@titlesolution\n", + "#@titlesolution Exercise 15\n", "temp = 90\n", "\n", "if temp >= 100:\n", @@ -4223,20 +3907,16 @@ "id": "801585ff-490f-4883-b3f4-4015159bbacc", "metadata": {}, "source": [ - "**Exercise 2**\n", + "**Exercise 16**.\n", "Teacher Joelle has computed the final scores of the students as a percentage (a `score` out of 100). The final grade was computed as a weighted combination of the student's average grade on the assignments, one midterm exam, and a final exam (more on this later).\n", "\n", - "The school where she teachers, requires her to convert each student's `score` to a letter grade, according to the following [grading scale](https://www.mcgill.ca/study/2022-2023/university_regulations_and_resources/undergraduate/gi_grading_and_grade_point_averages):\n", + "The school where she teachers, requires her to convert each student's `score` to a letter grade, according to the following grading scale:\n", + "\n", "```\n", "Grade Numerical score interval\n", - "A 85% – 100%\n", - "A- 80% – 84.999…%\n", - "B+ 75% – 79.999…%\n", - "B 70% – 74.999…%\n", - "B- 65% – 69.999…%\n", - "C+ 60% – 64.999…%\n", - "C 55% – 59.999…%\n", - "D 50% – 54.999…%\n", + "A 90% – 100%\n", + "B 70% – 89.999…%\n", + "C 50% – 69.999…%\n", "F 0% – 49.999…%\n", "```\n", "Write the if-elif-elif-.....-else statement that takes the score variable (an int between 0 and 100), and prints the appropriate letter grade for that score.\n" @@ -4244,7 +3924,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 99, "id": "dd91cc5f-8079-4ee9-b36f-7d2177d8a1c6", "metadata": {}, "outputs": [], @@ -4256,17 +3936,19 @@ "# print(....)\n", "# elif ...:\n", "# print(....)\n", + "# elif ...:\n", + "# print(....)\n", "# .....\n", "\n", "# uncomment the code if-elif-.. statement above and replace:\n", "# ... with the appropriate conditions,\n", "# .... with the appropriate letter grades (strings), and\n", - "# ..... with additional elif blocks to cover all the cases." + "# ..... with additional elif or else blocks to cover all the cases." ] }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 100, "id": "5f32a484-b4c2-45e9-8b5c-348913e3eee8", "metadata": {}, "outputs": [ @@ -4274,30 +3956,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "B\n" + "A\n" ] } ], "source": [ - "#@titlesolution\n", - "score = 73\n", + "#@titlesolution Exercise 16\n", + "score = 90\n", "\n", - "if score >= 85:\n", + "if score >= 90:\n", " print(\"A\")\n", - "elif score >= 80:\n", - " print(\"A-\")\n", - "elif score >= 75:\n", - " print(\"B+\")\n", "elif score >= 70:\n", " print(\"B\")\n", - "elif score >= 65:\n", - " print(\"B-\")\n", - "elif score >= 60:\n", - " print(\"C+\")\n", - "elif score >= 55:\n", - " print(\"C\")\n", "elif score >= 50:\n", - " print(\"D\")\n", + " print(\"C\")\n", "else:\n", " print(\"F\")" ] @@ -4329,7 +4001,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 101, "id": "9c2ab462-9fd8-4054-95c3-d9c9777b8ca5", "metadata": {}, "outputs": [ @@ -4339,7 +4011,7 @@ "\"It's hot!\"" ] }, - "execution_count": 106, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -4385,7 +4057,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 102, "id": "2cbe5469-d441-4527-b8cc-df5d550f7f87", "metadata": {}, "outputs": [], @@ -4395,7 +4067,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 103, "id": "d67f493f-3c41-468e-b282-2c693c2ce362", "metadata": {}, "outputs": [ @@ -4405,7 +4077,7 @@ "{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}" ] }, - "execution_count": 108, + "execution_count": 103, "metadata": {}, "output_type": "execute_result" } @@ -4416,7 +4088,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 104, "id": "7137d147-a739-4882-8795-e39770b436a9", "metadata": {}, "outputs": [ @@ -4426,7 +4098,7 @@ "dict_keys(['key1', 'key2', 'key3'])" ] }, - "execution_count": 109, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -4437,7 +4109,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 105, "id": "cf558c1f-bb8a-43c5-9159-61c2de03dc8f", "metadata": {}, "outputs": [ @@ -4447,7 +4119,7 @@ "dict_values(['value1', 'value2', 'value3'])" ] }, - "execution_count": 110, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } @@ -4469,7 +4141,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 106, "id": "292445ea-72b6-4adc-a06d-08d4e5d574d0", "metadata": {}, "outputs": [ @@ -4479,7 +4151,7 @@ "'value1'" ] }, - "execution_count": 111, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } @@ -4498,7 +4170,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 107, "id": "cea52a07-c70b-497a-8bd6-a13231032aef", "metadata": {}, "outputs": [ @@ -4508,7 +4180,7 @@ "{'key1': 'value1', 'key2': 'newval2', 'key3': 'value3'}" ] }, - "execution_count": 112, + "execution_count": 107, "metadata": {}, "output_type": "execute_result" } @@ -4539,7 +4211,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 108, "id": "e0bd5689-068b-4240-a194-0cfa4d170240", "metadata": {}, "outputs": [ @@ -4549,7 +4221,7 @@ "{'first_name': 'Justin', 'last_name': 'Trudeau', 'score': 31}" ] }, - "execution_count": 113, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } @@ -4581,7 +4253,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 109, "id": "e8e2a019-417a-4004-b139-ba806ab47838", "metadata": {}, "outputs": [ @@ -4591,7 +4263,7 @@ "set()" ] }, - "execution_count": 114, + "execution_count": 109, "metadata": {}, "output_type": "execute_result" } @@ -4603,7 +4275,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 110, "id": "ed497bbd-43fb-42d3-ada5-9fa129748072", "metadata": {}, "outputs": [ @@ -4613,7 +4285,7 @@ "{3, 5}" ] }, - "execution_count": 115, + "execution_count": 110, "metadata": {}, "output_type": "execute_result" } @@ -4626,7 +4298,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 111, "id": "e0aeddb7-b2c5-40b6-b0c3-ee6751f603ee", "metadata": {}, "outputs": [ @@ -4636,7 +4308,7 @@ "True" ] }, - "execution_count": 116, + "execution_count": 111, "metadata": {}, "output_type": "execute_result" } @@ -4647,7 +4319,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 112, "id": "a0ba1c57-fcd2-4eb1-b4b3-82b685f54b60", "metadata": {}, "outputs": [ @@ -4687,7 +4359,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 113, "id": "9733ed7d-21c8-4745-8b31-352c2d3f3b67", "metadata": {}, "outputs": [ @@ -4697,7 +4369,7 @@ "(2, 3)" ] }, - "execution_count": 118, + "execution_count": 113, "metadata": {}, "output_type": "execute_result" } @@ -4708,7 +4380,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 114, "id": "0bc549ca-1c20-4777-9c62-41dc200c8b27", "metadata": {}, "outputs": [ @@ -4718,7 +4390,7 @@ "(2, 3)" ] }, - "execution_count": 119, + "execution_count": 114, "metadata": {}, "output_type": "execute_result" } @@ -4737,7 +4409,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 115, "id": "cdf38d2e-e16f-422c-93e7-d5abbd6f0fe2", "metadata": {}, "outputs": [], @@ -4755,7 +4427,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 116, "id": "73fa9f45-7b18-4601-86d4-c2f501708fe3", "metadata": {}, "outputs": [], @@ -4768,7 +4440,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 117, "id": "a89051e1-1222-46b7-ad7d-28c9631486cf", "metadata": {}, "outputs": [], @@ -4819,7 +4491,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 118, "id": "f6826b95-a9b5-45cf-bab9-a220f1e2d98c", "metadata": {}, "outputs": [ @@ -4829,7 +4501,7 @@ "str" ] }, - "execution_count": 123, + "execution_count": 118, "metadata": {}, "output_type": "execute_result" } @@ -4842,7 +4514,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 119, "id": "6d9fa3e6-a77d-41e3-af0c-3ed9cca092d9", "metadata": {}, "outputs": [], @@ -4853,7 +4525,7 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 120, "id": "0d773b9a-6df1-45f5-9086-58c0f4ee2151", "metadata": {}, "outputs": [], @@ -4863,7 +4535,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 121, "id": "2a9133a7-0f0e-4681-8d66-794551f616be", "metadata": {}, "outputs": [ @@ -4873,7 +4545,7 @@ "True" ] }, - "execution_count": 126, + "execution_count": 121, "metadata": {}, "output_type": "execute_result" } @@ -4906,7 +4578,7 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 122, "id": "2f2411b9-4ad2-4234-8593-7492e112649b", "metadata": {}, "outputs": [ @@ -4916,7 +4588,7 @@ "_io.TextIOWrapper" ] }, - "execution_count": 127, + "execution_count": 122, "metadata": {}, "output_type": "execute_result" } @@ -4930,7 +4602,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 123, "id": "125f24ea-882e-4875-b475-f696e6d313b1", "metadata": {}, "outputs": [], @@ -4941,7 +4613,7 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 124, "id": "6ce0043e-6fc5-4ef0-8213-190ed5cc4229", "metadata": {}, "outputs": [ @@ -4951,7 +4623,7 @@ "('message.txt', 'w', 'UTF-8')" ] }, - "execution_count": 129, + "execution_count": 124, "metadata": {}, "output_type": "execute_result" } @@ -4963,7 +4635,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": 125, "id": "d3400e18-ccf7-446b-8270-57851594ac34", "metadata": {}, "outputs": [], @@ -5025,7 +4697,7 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": 126, "id": "4bd62411-3349-4a29-ad7d-c3f70919d667", "metadata": {}, "outputs": [], @@ -5073,17 +4745,17 @@ }, { "cell_type": "code", - "execution_count": 132, + "execution_count": 127, "id": "46b56640-cd44-4ad5-9549-855d8bab8729", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "<__main__.Interval at 0x10a8e71c0>" + "<__main__.Interval at 0x1083cfdf0>" ] }, - "execution_count": 132, + "execution_count": 127, "metadata": {}, "output_type": "execute_result" } @@ -5095,7 +4767,7 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": 128, "id": "8f0630f8-7334-48af-9b24-b64ecb949724", "metadata": {}, "outputs": [ @@ -5105,7 +4777,7 @@ "__main__.Interval" ] }, - "execution_count": 133, + "execution_count": 128, "metadata": {}, "output_type": "execute_result" } @@ -5116,7 +4788,7 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": 129, "id": "3133b9b2-9805-4f99-9a31-67b55315e0ce", "metadata": {}, "outputs": [ @@ -5126,7 +4798,7 @@ "'[2,4]'" ] }, - "execution_count": 134, + "execution_count": 129, "metadata": {}, "output_type": "execute_result" } @@ -5137,7 +4809,7 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": 130, "id": "fa340a71-734b-410c-ad87-0cfa38aebbf8", "metadata": {}, "outputs": [ @@ -5147,7 +4819,7 @@ "True" ] }, - "execution_count": 135, + "execution_count": 130, "metadata": {}, "output_type": "execute_result" } @@ -5158,7 +4830,7 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": 131, "id": "c5f70080-1728-4101-aaf7-ce31b0d889ef", "metadata": {}, "outputs": [ @@ -5168,7 +4840,7 @@ "False" ] }, - "execution_count": 136, + "execution_count": 131, "metadata": {}, "output_type": "execute_result" } @@ -5179,7 +4851,7 @@ }, { "cell_type": "code", - "execution_count": 137, + "execution_count": 132, "id": "d192fa9c-79a8-4ec1-abb3-bc4993328d33", "metadata": {}, "outputs": [ @@ -5189,7 +4861,7 @@ "2" ] }, - "execution_count": 137, + "execution_count": 132, "metadata": {}, "output_type": "execute_result" } @@ -5251,7 +4923,7 @@ }, { "cell_type": "code", - "execution_count": 138, + "execution_count": 133, "id": "fac4bab4-c52e-49c5-9a80-ae5484649da4", "metadata": {}, "outputs": [ @@ -5261,7 +4933,7 @@ "3" ] }, - "execution_count": 138, + "execution_count": 133, "metadata": {}, "output_type": "execute_result" } @@ -5291,7 +4963,7 @@ }, { "cell_type": "code", - "execution_count": 139, + "execution_count": 134, "id": "4c382060-350b-425a-a28c-e39704949454", "metadata": {}, "outputs": [ @@ -5301,7 +4973,7 @@ "3" ] }, - "execution_count": 139, + "execution_count": 134, "metadata": {}, "output_type": "execute_result" } @@ -5471,7 +5143,7 @@ }, { "cell_type": "code", - "execution_count": 140, + "execution_count": 135, "id": "e8101dfa", "metadata": {}, "outputs": [ @@ -5481,7 +5153,7 @@ "[(0, 61), (1, 79), (2, 98), (3, 72)]" ] }, - "execution_count": 140, + "execution_count": 135, "metadata": {}, "output_type": "execute_result" } @@ -5492,7 +5164,7 @@ }, { "cell_type": "code", - "execution_count": 141, + "execution_count": 136, "id": "f49a6d1c", "metadata": {}, "outputs": [ @@ -5538,7 +5210,7 @@ }, { "cell_type": "code", - "execution_count": 142, + "execution_count": 137, "id": "571cbbae-6870-4040-8054-1c2013b10fa2", "metadata": {}, "outputs": [ @@ -5548,7 +5220,7 @@ "[(1, 'a'), (2, 'b'), (3, 'c')]" ] }, - "execution_count": 142, + "execution_count": 137, "metadata": {}, "output_type": "execute_result" } @@ -5560,7 +5232,7 @@ }, { "cell_type": "code", - "execution_count": 143, + "execution_count": 138, "id": "0ef79303", "metadata": {}, "outputs": [ @@ -5570,7 +5242,7 @@ "[(1, 4), (2, 5), (3, 6)]" ] }, - "execution_count": 143, + "execution_count": 138, "metadata": {}, "output_type": "execute_result" } @@ -5585,7 +5257,7 @@ }, { "cell_type": "code", - "execution_count": 144, + "execution_count": 139, "id": "7181f631-ef97-4a95-82e1-717d9c1df407", "metadata": {}, "outputs": [ @@ -5655,7 +5327,7 @@ }, { "cell_type": "code", - "execution_count": 145, + "execution_count": 140, "id": "1c5c9094-d5ad-41d3-84eb-454fecdaa5b1", "metadata": {}, "outputs": [ @@ -5665,7 +5337,7 @@ "range(0, 4)" ] }, - "execution_count": 145, + "execution_count": 140, "metadata": {}, "output_type": "execute_result" } @@ -5676,7 +5348,7 @@ }, { "cell_type": "code", - "execution_count": 146, + "execution_count": 141, "id": "0cf5ab82-b76b-435b-b225-60ae3ca7aa4d", "metadata": {}, "outputs": [ @@ -5686,7 +5358,7 @@ "[0, 1, 2, 3]" ] }, - "execution_count": 146, + "execution_count": 141, "metadata": {}, "output_type": "execute_result" } @@ -5714,7 +5386,7 @@ }, { "cell_type": "code", - "execution_count": 147, + "execution_count": 142, "id": "a9f6ff40-85bd-4073-be0c-46add2a6983b", "metadata": {}, "outputs": [], @@ -5724,7 +5396,7 @@ }, { "cell_type": "code", - "execution_count": 148, + "execution_count": 143, "id": "1083f64a-db92-44ba-a2db-74c255411c57", "metadata": {}, "outputs": [ @@ -5734,7 +5406,7 @@ "['first_name', 'last_name', 'score']" ] }, - "execution_count": 148, + "execution_count": 143, "metadata": {}, "output_type": "execute_result" } @@ -5745,7 +5417,7 @@ }, { "cell_type": "code", - "execution_count": 149, + "execution_count": 144, "id": "160ffab8-9cfe-4ced-b972-c56bbbbb41aa", "metadata": {}, "outputs": [ @@ -5755,7 +5427,7 @@ "['first_name', 'last_name', 'score']" ] }, - "execution_count": 149, + "execution_count": 144, "metadata": {}, "output_type": "execute_result" } @@ -5767,7 +5439,7 @@ }, { "cell_type": "code", - "execution_count": 150, + "execution_count": 145, "id": "131daff9-cde5-431c-8422-5b71f491f266", "metadata": {}, "outputs": [ @@ -5777,7 +5449,7 @@ "['Julie', 'Tremblay', 98]" ] }, - "execution_count": 150, + "execution_count": 145, "metadata": {}, "output_type": "execute_result" } @@ -5788,7 +5460,7 @@ }, { "cell_type": "code", - "execution_count": 151, + "execution_count": 146, "id": "abd1b2b1-d69f-4cb3-842d-c7a962eed73a", "metadata": {}, "outputs": [ @@ -5798,7 +5470,7 @@ "[('first_name', 'Julie'), ('last_name', 'Tremblay'), ('score', 98)]" ] }, - "execution_count": 151, + "execution_count": 146, "metadata": {}, "output_type": "execute_result" } @@ -5855,7 +5527,7 @@ }, { "cell_type": "code", - "execution_count": 152, + "execution_count": 147, "id": "b9f71b2b-e8a2-4fa9-a3b8-0a5046f54fe6", "metadata": {}, "outputs": [ @@ -5865,7 +5537,7 @@ "dict_keys(['first_name', 'last_name', 'score'])" ] }, - "execution_count": 152, + "execution_count": 147, "metadata": {}, "output_type": "execute_result" } @@ -5876,7 +5548,7 @@ }, { "cell_type": "code", - "execution_count": 153, + "execution_count": 148, "id": "74d98037-a2de-4beb-8a89-dd70edceca10", "metadata": {}, "outputs": [ @@ -5886,7 +5558,7 @@ "dict_keys" ] }, - "execution_count": 153, + "execution_count": 148, "metadata": {}, "output_type": "execute_result" } @@ -5909,7 +5581,7 @@ }, { "cell_type": "code", - "execution_count": 154, + "execution_count": 149, "id": "526f6562-8441-459e-ba21-1e0e51827f4d", "metadata": {}, "outputs": [ @@ -5919,7 +5591,7 @@ "['first_name', 'last_name', 'score']" ] }, - "execution_count": 154, + "execution_count": 149, "metadata": {}, "output_type": "execute_result" } @@ -5930,7 +5602,7 @@ }, { "cell_type": "code", - "execution_count": 155, + "execution_count": 150, "id": "d30f5ca0-c86f-4d7c-8c9d-132817f282e4", "metadata": {}, "outputs": [ @@ -5940,7 +5612,7 @@ "list" ] }, - "execution_count": 155, + "execution_count": 150, "metadata": {}, "output_type": "execute_result" }