From 1c26e8b906bf595f575f8c3521a1e8b23be55064 Mon Sep 17 00:00:00 2001 From: kaylielau Date: Wed, 20 Aug 2025 01:32:22 +0100 Subject: [PATCH 1/5] Add live code 08/19/2025 --- 04_this_cohort/live_code/08_20_2025.ipynb | 1569 +++++++++++++++++++++ 1 file changed, 1569 insertions(+) create mode 100644 04_this_cohort/live_code/08_20_2025.ipynb diff --git a/04_this_cohort/live_code/08_20_2025.ipynb b/04_this_cohort/live_code/08_20_2025.ipynb new file mode 100644 index 000000000..0936ab34c --- /dev/null +++ b/04_this_cohort/live_code/08_20_2025.ipynb @@ -0,0 +1,1569 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "3a708ea8", + "metadata": {}, + "source": [ + "# Python\n", + "## August 19th 2025" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "ef084562", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "print('Hello World')" + ] + }, + { + "cell_type": "markdown", + "id": "07bc5265", + "metadata": {}, + "source": [ + "**Keyboard Shortcuts:**\n", + " - Utilize keyboard shortcuts for faster execution:\n", + " - `Shift + Enter`: Run the current cell and move to the next cell.\n", + " - `Ctrl + Enter`: Run the current cell and stay on the same cell.\n", + " - `Alt + Enter`: Run the current cell and insert a new cell below." + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "668637e4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "print('Hello World')" + ] + }, + { + "cell_type": "markdown", + "id": "f3171117", + "metadata": {}, + "source": [ + "- int: whole numbers\n", + "- float: decimal numbers\n", + "- string: text\n", + "- bool: true/false" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "afe3c287", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 + 6" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "4f88bc62", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.5" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 / 2" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "13495679", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.3333333333333335" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "16e44314", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# integer division -> divide the first operand by the second and return the quotient rounded down to the nearest int\n", + "10 // 3" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "4c6f4a58", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# modulo -> return the remainder of the division of the first operand by the second\n", + "10 % 3" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "5e68b158", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1000" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 ** 3 " + ] + }, + { + "cell_type": "markdown", + "id": "2082c9cb", + "metadata": {}, + "source": [ + "| Operator | Description |\n", + "| --- | --- |\n", + "| `>` | Greater than |\n", + "| `>=` | Greater than or equal to |\n", + "| `<` | Less than |\n", + "| `<=` | Less than or equal to |\n", + "| `==` | Equal to |\n", + "| `!=` | Not equal to |" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "e3a300f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "be88cf56", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "False" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "f00fae10", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "50 > 25" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "36fcfc66", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "-15 >= -14.99" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "4d4baf68", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "20 == 20" + ] + }, + { + "cell_type": "markdown", + "id": "77effa12", + "metadata": {}, + "source": [ + "\n", + "| Order | Operator | Description |\n", + "|---|---|---|\n", + "| 1 | `**` | Exponentiation |\n", + "| 2 | `-`| Negation |\n", + "| 3 | `*`, `/`, `//`, `%` | Multiplication, division, integer division, and modulo |\n", + "| 4 | `+`, `-` | Addition and subtraction |\n", + "| 5 | `<`, `<=`, `>`, `>=`, `==`, `!=` | Less than, less than or equal to, greater than, greater than or equal to, equal, not equal |" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "afe5d9ed", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(10 * 2) / 3 > 15" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "1c7c767d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 * 2" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "id": "0bb7aae0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6.666666666666667" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "20 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "3eb8eb8a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "6.666666666666667 > 15" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "992957e4", + "metadata": {}, + "outputs": [], + "source": [ + "# variable names\n", + "# can have letters, digits, and underscores\n", + "# cannot start with a digit\n", + "# case sensitive\n", + "\n", + "degrees_celsius = 25" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "cb75ef25", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77.0" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_fahrenheit = (9 / 5) * degrees_celsius + 32\n", + "degrees_fahrenheit" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "1712855b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77.0" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(9 / 5) * 25 + 32" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "id": "5e4c6a19", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_celsius = 0\n", + "degrees_celsius" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "ad95dc26", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77.0" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_fahrenheit" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "a672812c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "32.0" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_fahrenheit = (9 / 5) * degrees_celsius + 32\n", + "degrees_fahrenheit" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "1e58c58f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_celsius" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "20df63a5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_celsius = degrees_celsius + 10\n", + "degrees_celsius" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "aeab4d73", + "metadata": {}, + "outputs": [], + "source": [ + "a = 1" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "a537cef5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "d676653d", + "metadata": {}, + "outputs": [], + "source": [ + "b = a" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "b68e00aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "id": "d250e226", + "metadata": {}, + "outputs": [], + "source": [ + "a = 2" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "id": "a3ce84c8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "27ad2700", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "13ca91ce", + "metadata": {}, + "outputs": [], + "source": [ + "degrees_celsius = 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "832bb8d7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 123, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# degrees_celsius = degrees_celsius + 10\n", + "\n", + "# augmented assignment\n", + "degrees_celsius += 10\n", + "degrees_celsius\n", + "\n", + "# degrees_celsius **= 10" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "18d23121", + "metadata": {}, + "outputs": [], + "source": [ + "julia = 'cheese sandwich'\n", + "kaylie = julia" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "id": "9bfcf134", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 125, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "id": "8912a684", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 126, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "id": "c4ef4583", + "metadata": {}, + "outputs": [], + "source": [ + "julia = 'ham sandwich'" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "9cc7280d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ham sandwich'" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "0ad8fb10", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 129, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "0bde7350", + "metadata": {}, + "outputs": [], + "source": [ + "kaylie = julia" + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "id": "db32f129", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ham sandwich'" + ] + }, + "execution_count": 131, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 132, + "id": "ebfca44f", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "cannot assign to literal here. Maybe you meant '==' instead of '='? (3541004665.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[132], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 12 = x\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m cannot assign to literal here. Maybe you meant '==' instead of '='?\n" + ] + } + ], + "source": [ + "12 = x" + ] + }, + { + "cell_type": "code", + "execution_count": 133, + "id": "b8283460", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (934255472.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[133], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 25 -\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "25 -" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "id": "3d3b241c", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'my_variable' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[134], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_variable\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n", + "\u001b[0;31mNameError\u001b[0m: name 'my_variable' is not defined" + ] + } + ], + "source": [ + "my_variable + 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "02d5313a", + "metadata": {}, + "outputs": [], + "source": [ + "# this is a comment!\n", + "\n", + "dog = 'woof'\n", + "# dog = 'bark'\n", + "cat = 'meow'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3384b06", + "metadata": {}, + "outputs": [], + "source": [ + "# readability\n", + "\n", + "# white space: space to either side of an operator helps with readability\n", + "# can split code across multiple lines by wrapping it in parentheses\n", + "\n", + "# comments: help explain sections of code\n", + "# can toggle lines of code on/off\n", + "\n", + "# clear and consistent variable names: descriptive variable names\n", + "# consistent naming practice" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "id": "352cd1da", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(5 + 5 +\n", + " 5 + 5)" + ] + }, + { + "cell_type": "markdown", + "id": "960de3e0", + "metadata": {}, + "source": [ + "Version 1\n", + "```python\n", + "studentone_grade=90\n", + "StudentGrade2=50\n", + "stu3gr=74\n", + "avggrade=(studentone_grade+StudentGrade2+stut3gr)/3\n", + "```\n", + "\n", + "Version 2 \n", + "```python\n", + "student1_grade = 90\n", + "student2_grade = 50\n", + "student3_grade = 74\n", + "average_grade = ((student1_grade\n", + " + student2_grade\n", + " + student3_grade)\n", + " / 3)\n", + "```\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e2ac4eae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "42\n" + ] + } + ], + "source": [ + "# function: block of code that performs a task\n", + "# can take zero or more inputs and return an output\n", + "\n", + "print(42)" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "id": "f7c97d25", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 141, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(42.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "id": "549350d7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 142, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(True)" + ] + }, + { + "cell_type": "code", + "execution_count": 143, + "id": "8054647d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "43" + ] + }, + "execution_count": 143, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abs(-43)" + ] + }, + { + "cell_type": "code", + "execution_count": 144, + "id": "ba5e2c18", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 144, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(2/3)" + ] + }, + { + "cell_type": "code", + "execution_count": 146, + "id": "4de0a0f6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.6666666666666666" + ] + }, + "execution_count": 146, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": 147, + "id": "a9ee0753", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.0" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "float(4)" + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "id": "5cb855cf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(4.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "id": "38734581", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 149, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(round(2 * 1.5))" + ] + }, + { + "cell_type": "code", + "execution_count": 150, + "id": "b57533e7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.0" + ] + }, + "execution_count": 150, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 * 1.5" + ] + }, + { + "cell_type": "code", + "execution_count": 151, + "id": "367b194d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 151, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(3.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "id": "b0f10f1d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 152, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 153, + "id": "a2911504", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function round in module builtins:\n", + "\n", + "round(number, ndigits=None)\n", + " Round a number to a given precision in decimal digits.\n", + " \n", + " The return value is an integer if ndigits is omitted or None. Otherwise\n", + " the return value has the same type as the number. ndigits may be negative.\n", + "\n" + ] + } + ], + "source": [ + "help(round)" + ] + }, + { + "cell_type": "code", + "execution_count": 161, + "id": "cee2fa40", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.66667" + ] + }, + "execution_count": 161, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(2/3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "34a135e0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 154, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(2/3)" + ] + }, + { + "cell_type": "code", + "execution_count": 156, + "id": "2f0e2761", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.6666666666666666" + ] + }, + "execution_count": 156, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 / 3 " + ] + }, + { + "cell_type": "code", + "execution_count": 157, + "id": "07e074d6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 157, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(10/3)" + ] + }, + { + "cell_type": "code", + "execution_count": 159, + "id": "dc267146", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.3333333333333335" + ] + }, + "execution_count": 159, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7950db8b", + "metadata": {}, + "outputs": [], + "source": [ + "# function names:\n", + "# can use letters, digits, and underscores\n", + "# cannot start with a digit\n", + "# case sensitive\n", + "# typically functions start with lowercase letters\n", + "\n", + "\n", + "# parameters: variables used only within a function\n", + "# pass an argument to a function, argument value is assigned to a corresponding parameter\n", + "\n", + "# None: special data type representing no data\n", + "# if we don't have a return statement, Python will fill in an implicit one and return None" + ] + }, + { + "cell_type": "code", + "execution_count": 169, + "id": "488a0cd2", + "metadata": {}, + "outputs": [], + "source": [ + "def c_to_f(degrees_c):\n", + " degrees_f = (9 / 5) * degrees_c + 32\n", + " return degrees_f" + ] + }, + { + "cell_type": "code", + "execution_count": 163, + "id": "3c64200c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "212.0" + ] + }, + "execution_count": 163, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c_to_f(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 164, + "id": "6a569714", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12.2" + ] + }, + "execution_count": 164, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c_to_f(-11)" + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "id": "38c9d40f", + "metadata": {}, + "outputs": [], + "source": [ + "boiling_f = c_to_f(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 171, + "id": "e2ef8f99", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "212.0" + ] + }, + "execution_count": 171, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "boiling_f" + ] + }, + { + "cell_type": "code", + "execution_count": 172, + "id": "b1a72385", + "metadata": {}, + "outputs": [], + "source": [ + "def c_to_f(degrees_c):\n", + " degrees_f = (9 / 5) * degrees_c + 32\n", + " print(degrees_f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ac52ac1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "212.0\n" + ] + } + ], + "source": [ + "boiling_f = c_to_f(100) # output is None" + ] + }, + { + "cell_type": "code", + "execution_count": 177, + "id": "c021b747", + "metadata": {}, + "outputs": [], + "source": [ + "boiling_f" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f8eb2d2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "dsi_participant", + "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.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 39ec4947ddb1738e01bb3703e97ac5e7909db1c7 Mon Sep 17 00:00:00 2001 From: kaylielau Date: Thu, 21 Aug 2025 01:34:48 +0100 Subject: [PATCH 2/5] Upload live coding notebook 08/20/2025 --- 04_this_cohort/live_code/08_19_2025.ipynb | 1569 ++++++++++++++ 04_this_cohort/live_code/08_20_2025.ipynb | 2278 +++++++++++++++------ 2 files changed, 3248 insertions(+), 599 deletions(-) create mode 100644 04_this_cohort/live_code/08_19_2025.ipynb diff --git a/04_this_cohort/live_code/08_19_2025.ipynb b/04_this_cohort/live_code/08_19_2025.ipynb new file mode 100644 index 000000000..0936ab34c --- /dev/null +++ b/04_this_cohort/live_code/08_19_2025.ipynb @@ -0,0 +1,1569 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "3a708ea8", + "metadata": {}, + "source": [ + "# Python\n", + "## August 19th 2025" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "ef084562", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "print('Hello World')" + ] + }, + { + "cell_type": "markdown", + "id": "07bc5265", + "metadata": {}, + "source": [ + "**Keyboard Shortcuts:**\n", + " - Utilize keyboard shortcuts for faster execution:\n", + " - `Shift + Enter`: Run the current cell and move to the next cell.\n", + " - `Ctrl + Enter`: Run the current cell and stay on the same cell.\n", + " - `Alt + Enter`: Run the current cell and insert a new cell below." + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "668637e4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "print('Hello World')" + ] + }, + { + "cell_type": "markdown", + "id": "f3171117", + "metadata": {}, + "source": [ + "- int: whole numbers\n", + "- float: decimal numbers\n", + "- string: text\n", + "- bool: true/false" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "afe3c287", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 + 6" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "4f88bc62", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.5" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 / 2" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "13495679", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.3333333333333335" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "16e44314", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# integer division -> divide the first operand by the second and return the quotient rounded down to the nearest int\n", + "10 // 3" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "4c6f4a58", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# modulo -> return the remainder of the division of the first operand by the second\n", + "10 % 3" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "5e68b158", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1000" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 ** 3 " + ] + }, + { + "cell_type": "markdown", + "id": "2082c9cb", + "metadata": {}, + "source": [ + "| Operator | Description |\n", + "| --- | --- |\n", + "| `>` | Greater than |\n", + "| `>=` | Greater than or equal to |\n", + "| `<` | Less than |\n", + "| `<=` | Less than or equal to |\n", + "| `==` | Equal to |\n", + "| `!=` | Not equal to |" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "e3a300f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "be88cf56", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "False" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "f00fae10", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "50 > 25" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "36fcfc66", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "-15 >= -14.99" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "4d4baf68", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "20 == 20" + ] + }, + { + "cell_type": "markdown", + "id": "77effa12", + "metadata": {}, + "source": [ + "\n", + "| Order | Operator | Description |\n", + "|---|---|---|\n", + "| 1 | `**` | Exponentiation |\n", + "| 2 | `-`| Negation |\n", + "| 3 | `*`, `/`, `//`, `%` | Multiplication, division, integer division, and modulo |\n", + "| 4 | `+`, `-` | Addition and subtraction |\n", + "| 5 | `<`, `<=`, `>`, `>=`, `==`, `!=` | Less than, less than or equal to, greater than, greater than or equal to, equal, not equal |" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "afe5d9ed", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(10 * 2) / 3 > 15" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "1c7c767d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 * 2" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "id": "0bb7aae0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6.666666666666667" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "20 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "3eb8eb8a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "6.666666666666667 > 15" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "992957e4", + "metadata": {}, + "outputs": [], + "source": [ + "# variable names\n", + "# can have letters, digits, and underscores\n", + "# cannot start with a digit\n", + "# case sensitive\n", + "\n", + "degrees_celsius = 25" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "cb75ef25", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77.0" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_fahrenheit = (9 / 5) * degrees_celsius + 32\n", + "degrees_fahrenheit" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "1712855b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77.0" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(9 / 5) * 25 + 32" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "id": "5e4c6a19", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_celsius = 0\n", + "degrees_celsius" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "ad95dc26", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77.0" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_fahrenheit" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "a672812c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "32.0" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_fahrenheit = (9 / 5) * degrees_celsius + 32\n", + "degrees_fahrenheit" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "1e58c58f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_celsius" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "20df63a5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "degrees_celsius = degrees_celsius + 10\n", + "degrees_celsius" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "aeab4d73", + "metadata": {}, + "outputs": [], + "source": [ + "a = 1" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "a537cef5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "d676653d", + "metadata": {}, + "outputs": [], + "source": [ + "b = a" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "b68e00aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "id": "d250e226", + "metadata": {}, + "outputs": [], + "source": [ + "a = 2" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "id": "a3ce84c8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "27ad2700", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "13ca91ce", + "metadata": {}, + "outputs": [], + "source": [ + "degrees_celsius = 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "832bb8d7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 123, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# degrees_celsius = degrees_celsius + 10\n", + "\n", + "# augmented assignment\n", + "degrees_celsius += 10\n", + "degrees_celsius\n", + "\n", + "# degrees_celsius **= 10" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "18d23121", + "metadata": {}, + "outputs": [], + "source": [ + "julia = 'cheese sandwich'\n", + "kaylie = julia" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "id": "9bfcf134", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 125, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "id": "8912a684", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 126, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "id": "c4ef4583", + "metadata": {}, + "outputs": [], + "source": [ + "julia = 'ham sandwich'" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "9cc7280d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ham sandwich'" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "0ad8fb10", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 129, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "0bde7350", + "metadata": {}, + "outputs": [], + "source": [ + "kaylie = julia" + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "id": "db32f129", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ham sandwich'" + ] + }, + "execution_count": 131, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 132, + "id": "ebfca44f", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "cannot assign to literal here. Maybe you meant '==' instead of '='? (3541004665.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[132], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 12 = x\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m cannot assign to literal here. Maybe you meant '==' instead of '='?\n" + ] + } + ], + "source": [ + "12 = x" + ] + }, + { + "cell_type": "code", + "execution_count": 133, + "id": "b8283460", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (934255472.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[133], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 25 -\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "25 -" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "id": "3d3b241c", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'my_variable' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[134], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_variable\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n", + "\u001b[0;31mNameError\u001b[0m: name 'my_variable' is not defined" + ] + } + ], + "source": [ + "my_variable + 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "02d5313a", + "metadata": {}, + "outputs": [], + "source": [ + "# this is a comment!\n", + "\n", + "dog = 'woof'\n", + "# dog = 'bark'\n", + "cat = 'meow'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3384b06", + "metadata": {}, + "outputs": [], + "source": [ + "# readability\n", + "\n", + "# white space: space to either side of an operator helps with readability\n", + "# can split code across multiple lines by wrapping it in parentheses\n", + "\n", + "# comments: help explain sections of code\n", + "# can toggle lines of code on/off\n", + "\n", + "# clear and consistent variable names: descriptive variable names\n", + "# consistent naming practice" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "id": "352cd1da", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(5 + 5 +\n", + " 5 + 5)" + ] + }, + { + "cell_type": "markdown", + "id": "960de3e0", + "metadata": {}, + "source": [ + "Version 1\n", + "```python\n", + "studentone_grade=90\n", + "StudentGrade2=50\n", + "stu3gr=74\n", + "avggrade=(studentone_grade+StudentGrade2+stut3gr)/3\n", + "```\n", + "\n", + "Version 2 \n", + "```python\n", + "student1_grade = 90\n", + "student2_grade = 50\n", + "student3_grade = 74\n", + "average_grade = ((student1_grade\n", + " + student2_grade\n", + " + student3_grade)\n", + " / 3)\n", + "```\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e2ac4eae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "42\n" + ] + } + ], + "source": [ + "# function: block of code that performs a task\n", + "# can take zero or more inputs and return an output\n", + "\n", + "print(42)" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "id": "f7c97d25", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 141, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(42.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "id": "549350d7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 142, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(True)" + ] + }, + { + "cell_type": "code", + "execution_count": 143, + "id": "8054647d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "43" + ] + }, + "execution_count": 143, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abs(-43)" + ] + }, + { + "cell_type": "code", + "execution_count": 144, + "id": "ba5e2c18", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 144, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(2/3)" + ] + }, + { + "cell_type": "code", + "execution_count": 146, + "id": "4de0a0f6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.6666666666666666" + ] + }, + "execution_count": 146, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": 147, + "id": "a9ee0753", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.0" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "float(4)" + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "id": "5cb855cf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(4.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "id": "38734581", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 149, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(round(2 * 1.5))" + ] + }, + { + "cell_type": "code", + "execution_count": 150, + "id": "b57533e7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.0" + ] + }, + "execution_count": 150, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 * 1.5" + ] + }, + { + "cell_type": "code", + "execution_count": 151, + "id": "367b194d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 151, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(3.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "id": "b0f10f1d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 152, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 153, + "id": "a2911504", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function round in module builtins:\n", + "\n", + "round(number, ndigits=None)\n", + " Round a number to a given precision in decimal digits.\n", + " \n", + " The return value is an integer if ndigits is omitted or None. Otherwise\n", + " the return value has the same type as the number. ndigits may be negative.\n", + "\n" + ] + } + ], + "source": [ + "help(round)" + ] + }, + { + "cell_type": "code", + "execution_count": 161, + "id": "cee2fa40", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.66667" + ] + }, + "execution_count": 161, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(2/3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "34a135e0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 154, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(2/3)" + ] + }, + { + "cell_type": "code", + "execution_count": 156, + "id": "2f0e2761", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.6666666666666666" + ] + }, + "execution_count": 156, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 / 3 " + ] + }, + { + "cell_type": "code", + "execution_count": 157, + "id": "07e074d6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 157, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(10/3)" + ] + }, + { + "cell_type": "code", + "execution_count": 159, + "id": "dc267146", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.3333333333333335" + ] + }, + "execution_count": 159, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 / 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7950db8b", + "metadata": {}, + "outputs": [], + "source": [ + "# function names:\n", + "# can use letters, digits, and underscores\n", + "# cannot start with a digit\n", + "# case sensitive\n", + "# typically functions start with lowercase letters\n", + "\n", + "\n", + "# parameters: variables used only within a function\n", + "# pass an argument to a function, argument value is assigned to a corresponding parameter\n", + "\n", + "# None: special data type representing no data\n", + "# if we don't have a return statement, Python will fill in an implicit one and return None" + ] + }, + { + "cell_type": "code", + "execution_count": 169, + "id": "488a0cd2", + "metadata": {}, + "outputs": [], + "source": [ + "def c_to_f(degrees_c):\n", + " degrees_f = (9 / 5) * degrees_c + 32\n", + " return degrees_f" + ] + }, + { + "cell_type": "code", + "execution_count": 163, + "id": "3c64200c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "212.0" + ] + }, + "execution_count": 163, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c_to_f(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 164, + "id": "6a569714", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12.2" + ] + }, + "execution_count": 164, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c_to_f(-11)" + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "id": "38c9d40f", + "metadata": {}, + "outputs": [], + "source": [ + "boiling_f = c_to_f(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 171, + "id": "e2ef8f99", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "212.0" + ] + }, + "execution_count": 171, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "boiling_f" + ] + }, + { + "cell_type": "code", + "execution_count": 172, + "id": "b1a72385", + "metadata": {}, + "outputs": [], + "source": [ + "def c_to_f(degrees_c):\n", + " degrees_f = (9 / 5) * degrees_c + 32\n", + " print(degrees_f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ac52ac1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "212.0\n" + ] + } + ], + "source": [ + "boiling_f = c_to_f(100) # output is None" + ] + }, + { + "cell_type": "code", + "execution_count": 177, + "id": "c021b747", + "metadata": {}, + "outputs": [], + "source": [ + "boiling_f" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f8eb2d2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "dsi_participant", + "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.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/04_this_cohort/live_code/08_20_2025.ipynb b/04_this_cohort/live_code/08_20_2025.ipynb index 0936ab34c..a14965013 100644 --- a/04_this_cohort/live_code/08_20_2025.ipynb +++ b/04_this_cohort/live_code/08_20_2025.ipynb @@ -1,220 +1,1359 @@ { "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "9d0ff086", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "42\n" + ] + } + ], + "source": [ + "# August 20, 2025\n", + "\n", + "print(42)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "030b58ec", + "metadata": {}, + "outputs": [], + "source": [ + "def c_to_f(degrees_c):\n", + " degrees_f = (9 / 5) * degrees_c + 32\n", + " return degrees_f" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "85a87410", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "212.0" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c_to_f(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9a976f8d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "39.2" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c_to_f(4.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "901e070c", + "metadata": {}, + "outputs": [], + "source": [ + "def divide(dividend, divisor):\n", + " result = dividend / divisor\n", + " return int(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "3e58d32f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divide(0, 2) # 0 / 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c9183bf0", + "metadata": {}, + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "division by zero", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdivide\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[0;32mIn[5], line 2\u001b[0m, in \u001b[0;36mdivide\u001b[0;34m(dividend, divisor)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdivide\u001b[39m(dividend, divisor):\n\u001b[0;32m----> 2\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mdividend\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mdivisor\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], + "source": [ + "divide(2, 0) # 2 / 0" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "68afd840", + "metadata": {}, + "outputs": [], + "source": [ + "def calc_sales_tax(price, tax_rate=0.13):\n", + " tax_amount = price * tax_rate\n", + " return tax_amount" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "ab003d7a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.65" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calc_sales_tax(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "45b97958", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.65" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 * 0.13" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e1e266d6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.4" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calc_sales_tax(5, 0.08)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8ca927fd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.4" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 * 0.08" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "be6e8d78", + "metadata": {}, + "outputs": [], + "source": [ + "def calc_total_bill(price, tax_rate=0.13, tip_rate=0.15):\n", + " tax_amount = price * tax_rate\n", + " tip_amount = price * tip_rate\n", + " total_bill = price + tax_amount + tip_amount\n", + " return total_bill" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "127e67c3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "128.0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calc_total_bill(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "c2cd3752", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "128.0" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "100 + 100 * 0.13 + 100 * 0.15" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "42fa3953", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "135.0" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calc_total_bill(100, 0.20)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "7ba15f90", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "135.0" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "100 + 100 * 0.20 + 100 * 0.15" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "82ec6fef", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "133.0" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calc_total_bill(100, tip_rate=0.20)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "cf065dd9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "133.0" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "100 + 100 * 0.13 + 100 * 0.20" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "6836f7db", + "metadata": {}, + "outputs": [], + "source": [ + "def calc_total_bill_2(price, tax_rate, tip_rate):\n", + " tax_amount = price * tax_rate\n", + " tip_amount = price * tip_rate\n", + " total_bill = price + tax_amount + tip_amount\n", + " return total_bill" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "90ed654b", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "calc_total_bill_2() missing 2 required positional arguments: 'tax_rate' and 'tip_rate'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[28], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mcalc_total_bill_2\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: calc_total_bill_2() missing 2 required positional arguments: 'tax_rate' and 'tip_rate'" + ] + } + ], + "source": [ + "calc_total_bill_2(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "8e823957", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function round in module builtins:\n", + "\n", + "round(number, ndigits=None)\n", + " Round a number to a given precision in decimal digits.\n", + " \n", + " The return value is an integer if ndigits is omitted or None. Otherwise\n", + " the return value has the same type as the number. ndigits may be negative.\n", + "\n" + ] + } + ], + "source": [ + "help(round)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "b79cb63b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on function c_to_f in module __main__:\n", + "\n", + "c_to_f(degrees_c)\n", + "\n" + ] + } + ], + "source": [ + "help(c_to_f)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "843b9931", + "metadata": {}, + "outputs": [], + "source": [ + "def c_to_f(degrees_c):\n", + " \"\"\"Convert degrees from Celsius to Fahrenheit\"\"\"\n", + " degrees_f = (9 / 5) * degrees_c + 32\n", + " return degrees_f" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "6245fa70", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on function c_to_f in module __main__:\n", + "\n", + "c_to_f(degrees_c)\n", + " Convert degrees from Celsius to Fahrenheit\n", + "\n" + ] + } + ], + "source": [ + "help(c_to_f)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "9e781f6c", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'degrees_c' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[33], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdegrees_c\u001b[49m\n", + "\u001b[0;31mNameError\u001b[0m: name 'degrees_c' is not defined" + ] + } + ], + "source": [ + "degrees_c" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "69237183", + "metadata": {}, + "outputs": [], + "source": [ + "kaylie = 'loves python'" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "a39b5a2e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'loves python'" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "6dfd2533", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'This is a string'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'This is a string'" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "448e0c00", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'This is also a string'" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"This is also a string\"" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "542265a7", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unterminated string literal (detected at line 1) (3110063236.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[38], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 'This does not work\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" + ] + } + ], + "source": [ + "'This does not work\"" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "e71ab187", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unterminated string literal (detected at line 1) (3205553238.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[39], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 'Let's see if this works'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" + ] + } + ], + "source": [ + "'Let's see if this works'" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "6a0295f8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Let's see if this works\"" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Let's see if this works\"" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "f089e2d3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nI am a multiline string\\n\\nThis is handy for long texts\\n'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''\n", + "I am a multiline string\n", + "\n", + "This is handy for long texts\n", + "'''" + ] + }, { "cell_type": "markdown", - "id": "3a708ea8", + "id": "19971831", + "metadata": {}, + "source": [ + "### Escape sequences\n", + "\n", + "| Escape sequence | Description |\n", + "|-----------------|--------------------|\n", + "| \\\\' | Single quote |\n", + "| \\\\\" | Double quote |\n", + "| \\\\\\\\ | Backslash |\n", + "| \\\\t | Tab |\n", + "| \\\\n | Newline |\n", + "| \\\\r | Carriage return |" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "b11e4eea", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a backslash: \\\n" + ] + } + ], + "source": [ + "print('This is a backslash: \\\\')" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "96ebbaad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a double-quote string with a quote inside: \"example\"\n" + ] + } + ], + "source": [ + "print('This is a double-quote string with a quote inside: \"example\"')" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "72c9916b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a double-quote string with a quote inside: \"example\"\n" + ] + } + ], + "source": [ + "print(\"This is a double-quote string with a quote inside: \\\"example\\\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "b55bf263", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a single-quoted string with a quote inside: won't\n" + ] + } + ], + "source": [ + "print(\"This is a single-quoted string with a quote inside: won't\")" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "10b4f1d1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a single-quoted string with a quote inside: won't\n" + ] + } + ], + "source": [ + "print('This is a single-quoted string with a quote inside: won\\'t')" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "79cb31aa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a return carriagee of \n" + ] + } + ], + "source": [ + "print('This is an example of \\ra return carriage')" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "551ba45d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "aaXXX\n" + ] + } + ], + "source": [ + "print('XXXXX\\raa')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7da855ec", + "metadata": {}, + "outputs": [], + "source": [ + "'aaXXX'" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "ef156d02", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "67345\n" + ] + } + ], + "source": [ + "print('12345\\r67')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8e5ca326", + "metadata": {}, + "outputs": [], + "source": [ + "'67345'" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "3c0a2d47", "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Python\n", - "## August 19th 2025" + "3 + 3" ] }, { "cell_type": "code", - "execution_count": 81, - "id": "ef084562", + "execution_count": 56, + "id": "419d1406", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n" + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3 * 3" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "7798ad1a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world'" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'hello' + ' ' + 'world'" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "cd96116c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hahaha'" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'ha' * 3" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "21834f6c", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "can only concatenate str (not \"int\") to str", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[60], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mThe year is \u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2025\u001b[39;49m\n", + "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ - "print('Hello World')" - ] - }, - { - "cell_type": "markdown", - "id": "07bc5265", - "metadata": {}, - "source": [ - "**Keyboard Shortcuts:**\n", - " - Utilize keyboard shortcuts for faster execution:\n", - " - `Shift + Enter`: Run the current cell and move to the next cell.\n", - " - `Ctrl + Enter`: Run the current cell and stay on the same cell.\n", - " - `Alt + Enter`: Run the current cell and insert a new cell below." + "'The year is ' + 2025" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "37b09e1d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'apple' == 'Apple'" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "ad9f7ff1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'apple' != 'Apple'" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "109e656d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'20' == 20" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "3e2acc62", + "metadata": {}, + "outputs": [], + "source": [ + "first_name = 'Ada'\n", + "last_name = 'Lovelace'" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "80f7a91b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A'" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_name[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "2bcfd2d1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'L'" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "f19b53ce", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Ad'" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_name[0:2]" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "9c77cfad", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Lov'" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[0:3]" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "3247e655", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'lace'" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[4:8]" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "1f05d5aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'lace'" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[4:]" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "2caf3079", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Love'" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[:4]" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "ffd2da38", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A'" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_name[0]" ] }, { "cell_type": "code", "execution_count": 82, - "id": "668637e4", + "id": "c7d02351", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n" - ] + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print('Hello World')" - ] - }, - { - "cell_type": "markdown", - "id": "f3171117", - "metadata": {}, - "source": [ - "- int: whole numbers\n", - "- float: decimal numbers\n", - "- string: text\n", - "- bool: true/false" + "first_name[2]" ] }, { "cell_type": "code", - "execution_count": 83, - "id": "afe3c287", + "execution_count": 84, + "id": "9c5920bd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "11" + "'Aa'" ] }, - "execution_count": 83, + "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "5 + 6" + "first_name[::2]" ] }, { "cell_type": "code", - "execution_count": 84, - "id": "4f88bc62", + "execution_count": 86, + "id": "feba273d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "2.5" + "'oea'" ] }, - "execution_count": 84, + "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "5 / 2" + "last_name[1:7:2]" ] }, { "cell_type": "code", - "execution_count": 85, - "id": "13495679", + "execution_count": null, + "id": "1fc73262", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "3.3333333333333335" + "'Le'" ] }, - "execution_count": 85, + "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "10 / 3" + "last_name[0:6:3] # start at index 1 to index 5, moving 3 at a time" ] }, { "cell_type": "code", - "execution_count": 86, - "id": "16e44314", + "execution_count": 89, + "id": "3233e61a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "3" + "'555'" ] }, - "execution_count": 86, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# integer division -> divide the first operand by the second and return the quotient rounded down to the nearest int\n", - "10 // 3" + "phone_number = '+1 555-123-4567'\n", + "\n", + "phone_number[3:6]" ] }, { "cell_type": "code", - "execution_count": 87, - "id": "4c6f4a58", + "execution_count": 92, + "id": "0935ea8f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "'1 555-123'" ] }, - "execution_count": 87, + "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# modulo -> return the remainder of the division of the first operand by the second\n", - "10 % 3" + "phone_number[1:10]" ] }, { "cell_type": "code", - "execution_count": 88, - "id": "5e68b158", + "execution_count": 91, + "id": "8c0fe64f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1000" + "'15513'" ] }, - "execution_count": 88, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "10 ** 3 " + "phone_number[1:10:2]" ] }, { - "cell_type": "markdown", - "id": "2082c9cb", + "cell_type": "code", + "execution_count": 94, + "id": "622dc5d2", "metadata": {}, + "outputs": [], "source": [ - "| Operator | Description |\n", - "| --- | --- |\n", - "| `>` | Greater than |\n", - "| `>=` | Greater than or equal to |\n", - "| `<` | Less than |\n", - "| `<=` | Less than or equal to |\n", - "| `==` | Equal to |\n", - "| `!=` | Not equal to |" + "job_qualifications = 'The successful applicant will be proficient in R, Python, SQL, statistics'" ] }, { "cell_type": "code", - "execution_count": 89, - "id": "e3a300f0", + "execution_count": 95, + "id": "1379e68e", "metadata": {}, "outputs": [ { @@ -223,19 +1362,19 @@ "True" ] }, - "execution_count": 89, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "True" + "'R' in job_qualifications" ] }, { "cell_type": "code", - "execution_count": 90, - "id": "be88cf56", + "execution_count": 96, + "id": "e9de6e9a", "metadata": {}, "outputs": [ { @@ -244,477 +1383,534 @@ "False" ] }, - "execution_count": 90, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "False" + "' r ' in job_qualifications" ] }, { "cell_type": "code", - "execution_count": 91, - "id": "f00fae10", + "execution_count": 97, + "id": "460d26de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "False" ] }, - "execution_count": 91, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "50 > 25" + "'JavaScript' in job_qualifications" ] }, { "cell_type": "code", - "execution_count": 92, - "id": "36fcfc66", + "execution_count": 98, + "id": "1b509030", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 fish 2 fish\n" + ] + } + ], + "source": [ + "print(1, 'fish', 2, 'fish')" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "c4b3489d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "False" + "43" ] }, - "execution_count": 92, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "-15 >= -14.99" + "len('Sometimes we have a lonnnnggg piece of text')" ] }, { "cell_type": "code", - "execution_count": 93, - "id": "4d4baf68", + "execution_count": 100, + "id": "ce7765a2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "['a', 'a', 'a', 'b', 'n', 'n']" ] }, - "execution_count": 93, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "20 == 20" - ] - }, - { - "cell_type": "markdown", - "id": "77effa12", - "metadata": {}, - "source": [ - "\n", - "| Order | Operator | Description |\n", - "|---|---|---|\n", - "| 1 | `**` | Exponentiation |\n", - "| 2 | `-`| Negation |\n", - "| 3 | `*`, `/`, `//`, `%` | Multiplication, division, integer division, and modulo |\n", - "| 4 | `+`, `-` | Addition and subtraction |\n", - "| 5 | `<`, `<=`, `>`, `>=`, `==`, `!=` | Less than, less than or equal to, greater than, greater than or equal to, equal, not equal |" + "sorted('banana')" ] }, { "cell_type": "code", - "execution_count": 94, - "id": "afe5d9ed", + "execution_count": 101, + "id": "0db3344f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "False" + "['a', 'e', 'i', 'k', 'l', 'y']" ] }, - "execution_count": 94, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "(10 * 2) / 3 > 15" + "sorted('kaylie')" ] }, { "cell_type": "code", - "execution_count": 95, - "id": "1c7c767d", + "execution_count": 102, + "id": "701ee89f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "20" + "'I AM NOT YELLING'" ] }, - "execution_count": 95, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "10 * 2" + "'I am not yelling'.upper()" ] }, { "cell_type": "code", - "execution_count": 96, - "id": "0bb7aae0", + "execution_count": 103, + "id": "ee0786f1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "6.666666666666667" + "0" ] }, - "execution_count": 96, + "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "20 / 3" + "'This string is unusual'.count('e')" ] }, { "cell_type": "code", - "execution_count": 97, - "id": "3eb8eb8a", + "execution_count": 104, + "id": "12f8dcec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "False" + "True" ] }, - "execution_count": 97, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "6.666666666666667 > 15" + "'file_name.csv'.endswith('.csv')" ] }, { "cell_type": "code", - "execution_count": 98, - "id": "992957e4", + "execution_count": 105, + "id": "23b419dc", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'long_file_name_with_spaces.csv'" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# variable names\n", - "# can have letters, digits, and underscores\n", - "# cannot start with a digit\n", - "# case sensitive\n", - "\n", - "degrees_celsius = 25" + "'long file name with spaces.csv'.replace(' ', '_')" ] }, { "cell_type": "code", - "execution_count": 99, - "id": "cb75ef25", + "execution_count": 106, + "id": "c7c836bb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "77.0" + "'Ada'" ] }, - "execution_count": 99, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "degrees_fahrenheit = (9 / 5) * degrees_celsius + 32\n", - "degrees_fahrenheit" + "first_name" ] }, { "cell_type": "code", - "execution_count": 100, - "id": "1712855b", + "execution_count": 107, + "id": "319764ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "77.0" + "'Lovelace'" ] }, - "execution_count": 100, + "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "(9 / 5) * 25 + 32" + "last_name" ] }, { "cell_type": "code", - "execution_count": 101, - "id": "5e4c6a19", + "execution_count": 108, + "id": "63f3ae18", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0" + "\"Ada Lovelace's initials are A. L.\"" ] }, - "execution_count": 101, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "degrees_celsius = 0\n", - "degrees_celsius" + "'Ada Lovelace\\'s initials are {}. {}.'.format(first_name[0], last_name[0])" ] }, { "cell_type": "code", - "execution_count": 102, - "id": "ad95dc26", + "execution_count": 112, + "id": "ed6ae208", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "77.0" + "'My favourite veggie is lettuce'" ] }, - "execution_count": 102, + "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "degrees_fahrenheit" + "'My favourite veggie is {}'.format('lettuce')" ] }, { "cell_type": "code", - "execution_count": 103, - "id": "a672812c", + "execution_count": 113, + "id": "718d52c9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "32.0" + "\"Ada Lovelace's initials are A. L.\"" ] }, - "execution_count": 103, + "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "degrees_fahrenheit = (9 / 5) * degrees_celsius + 32\n", - "degrees_fahrenheit" + "f'Ada Lovelace\\'s initials are {first_name[0]}. {last_name[0]}.'" ] }, { "cell_type": "code", - "execution_count": 104, - "id": "1e58c58f", + "execution_count": 115, + "id": "09c67c0a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0" + "'My favourite veggie is lettuce'" ] }, - "execution_count": 104, + "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "degrees_celsius" + "f\"My favourite veggie is {'lettuce'}\"" ] }, { "cell_type": "code", - "execution_count": 107, - "id": "20df63a5", + "execution_count": null, + "id": "186d20b7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "30" + "'My favourite veggie is tomato'" ] }, - "execution_count": 107, + "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "degrees_celsius = degrees_celsius + 10\n", - "degrees_celsius" + "veggie = 'tomato'\n", + "f'My favourite veggie is {veggie}'" ] }, { "cell_type": "code", - "execution_count": 108, - "id": "aeab4d73", + "execution_count": 118, + "id": "28655f0f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on method_descriptor:\n", + "\n", + "lower(self, /)\n", + " Return a copy of the string converted to lowercase.\n", + "\n" + ] + } + ], + "source": [ + "help(str.lower)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ee48d03", "metadata": {}, "outputs": [], "source": [ - "a = 1" + "# int()\n", + "# if float -> truncate the decimal part\n", + "# if string representing a valid integer -> convert the string to an integer\n", + "# if string representing a float -> return error" ] }, { "cell_type": "code", - "execution_count": 109, - "id": "a537cef5", + "execution_count": 119, + "id": "f08f6199", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "12" ] }, - "execution_count": 109, + "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a" - ] - }, - { - "cell_type": "code", - "execution_count": 110, - "id": "d676653d", - "metadata": {}, - "outputs": [], - "source": [ - "b = a" + "int(12.25)" ] }, { "cell_type": "code", - "execution_count": 111, - "id": "b68e00aa", + "execution_count": 120, + "id": "4d8aad43", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "2" ] }, - "execution_count": 111, + "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "b" + "int('2')" ] }, { "cell_type": "code", - "execution_count": 112, - "id": "d250e226", + "execution_count": 121, + "id": "575f3e74", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: '2.2222'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[121], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m2.2222\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '2.2222'" + ] + } + ], + "source": [ + "int('2.2222')" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "id": "4422aefd", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'me'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[122], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mme\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'me'" + ] + } + ], + "source": [ + "int('me')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73c980dd", "metadata": {}, "outputs": [], "source": [ - "a = 2" + "# float()\n", + "# if int -> convert to a floating-point number\n", + "# if string representing a valid float -> convert the string to a float" ] }, { "cell_type": "code", - "execution_count": 113, - "id": "a3ce84c8", + "execution_count": 124, + "id": "863d7cea", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "2" + "892.0" ] }, - "execution_count": 113, + "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a" + "float(892)" ] }, { "cell_type": "code", - "execution_count": 114, - "id": "27ad2700", + "execution_count": 127, + "id": "7e9c4204", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "122.222" ] }, - "execution_count": 114, + "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "b" - ] - }, - { - "cell_type": "code", - "execution_count": 120, - "id": "13ca91ce", - "metadata": {}, - "outputs": [], - "source": [ - "degrees_celsius = 0" + "float('122.222')" ] }, { "cell_type": "code", - "execution_count": null, - "id": "832bb8d7", + "execution_count": 123, + "id": "98895e6d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "30" + "892.0" ] }, "execution_count": 123, @@ -723,322 +1919,310 @@ } ], "source": [ - "# degrees_celsius = degrees_celsius + 10\n", - "\n", - "# augmented assignment\n", - "degrees_celsius += 10\n", - "degrees_celsius\n", - "\n", - "# degrees_celsius **= 10" + "float('892')" ] }, { "cell_type": "code", - "execution_count": 124, - "id": "18d23121", + "execution_count": 125, + "id": "34614738", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ValueError", + "evalue": "could not convert string to float: 'me'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[125], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mme\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'me'" + ] + } + ], "source": [ - "julia = 'cheese sandwich'\n", - "kaylie = julia" + "float('me')" ] }, { "cell_type": "code", - "execution_count": 125, - "id": "9bfcf134", + "execution_count": null, + "id": "b1f46683", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'37.5'" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str(37.5) #float to string" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a21c50f0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'cheese sandwich'" + "'29'" ] }, - "execution_count": 125, + "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "julia" + "str(29) #int to string" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "764f5fd3", + "metadata": {}, + "outputs": [], + "source": [ + "# input() function prompt the user and return their response as a string" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "7050a4ce", + "metadata": {}, + "outputs": [], + "source": [ + "age = input(\"How old are you? \")" ] }, { "cell_type": "code", - "execution_count": 126, - "id": "8912a684", + "execution_count": 131, + "id": "f4258dec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'cheese sandwich'" + "'10000000'" ] }, - "execution_count": 126, + "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "kaylie" + "age" ] }, { "cell_type": "code", - "execution_count": 127, - "id": "c4ef4583", + "execution_count": 132, + "id": "2ae59a8c", "metadata": {}, "outputs": [], "source": [ - "julia = 'ham sandwich'" + "fruit = input(\"What is your favourite fruit? \")" ] }, { "cell_type": "code", - "execution_count": 128, - "id": "9cc7280d", + "execution_count": 133, + "id": "95f035f2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'ham sandwich'" + "'kiwi'" ] }, - "execution_count": 128, + "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "julia" + "fruit" ] }, { "cell_type": "code", - "execution_count": 129, - "id": "0ad8fb10", + "execution_count": 134, + "id": "29e00786", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'cheese sandwich'" + "10000001" ] }, - "execution_count": 129, + "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "kaylie" + "age_next_year = int(age) + 1\n", + "age_next_year" ] }, { "cell_type": "code", - "execution_count": 130, - "id": "0bde7350", + "execution_count": 135, + "id": "7a339668", "metadata": {}, "outputs": [], "source": [ - "kaylie = julia" + "age = input(\"How old are you? \")" ] }, { "cell_type": "code", - "execution_count": 131, - "id": "db32f129", + "execution_count": 136, + "id": "9f6fa5c9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'ham sandwich'" + "'cat'" ] }, - "execution_count": 131, + "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "kaylie" + "age" ] }, { "cell_type": "code", - "execution_count": 132, - "id": "ebfca44f", + "execution_count": 137, + "id": "5edc2341", "metadata": {}, "outputs": [ { - "ename": "SyntaxError", - "evalue": "cannot assign to literal here. Maybe you meant '==' instead of '='? (3541004665.py, line 1)", + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'cat'", "output_type": "error", "traceback": [ - "\u001b[0;36m Cell \u001b[0;32mIn[132], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 12 = x\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m cannot assign to literal here. Maybe you meant '==' instead of '='?\n" + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[137], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m age_next_year \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mage\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m 2\u001b[0m age_next_year\n", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'cat'" ] } ], "source": [ - "12 = x" + "age_next_year = int(age) + 1\n", + "age_next_year" ] }, { - "cell_type": "code", - "execution_count": 133, - "id": "b8283460", + "cell_type": "markdown", + "id": "4af3f994", "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (934255472.py, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m Cell \u001b[0;32mIn[133], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 25 -\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" - ] - } - ], "source": [ - "25 -" + "## `not`\n", + "\n", + "|X|`not` X|\n", + "|-|-|\n", + "|True|False|\n", + "|False|True|" ] }, { "cell_type": "code", - "execution_count": 134, - "id": "3d3b241c", + "execution_count": 138, + "id": "8959f807", "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'my_variable' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[134], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_variable\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n", - "\u001b[0;31mNameError\u001b[0m: name 'my_variable' is not defined" - ] + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "my_variable + 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "02d5313a", - "metadata": {}, - "outputs": [], - "source": [ - "# this is a comment!\n", - "\n", - "dog = 'woof'\n", - "# dog = 'bark'\n", - "cat = 'meow'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e3384b06", - "metadata": {}, - "outputs": [], - "source": [ - "# readability\n", - "\n", - "# white space: space to either side of an operator helps with readability\n", - "# can split code across multiple lines by wrapping it in parentheses\n", + "# not:\n", + "# negate the truth value of the statement\n", "\n", - "# comments: help explain sections of code\n", - "# can toggle lines of code on/off\n", - "\n", - "# clear and consistent variable names: descriptive variable names\n", - "# consistent naming practice" + "not True" ] }, { "cell_type": "code", - "execution_count": 138, - "id": "352cd1da", + "execution_count": 139, + "id": "1b181857", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "20" + "True" ] }, - "execution_count": 138, + "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "(5 + 5 +\n", - " 5 + 5)" - ] - }, - { - "cell_type": "markdown", - "id": "960de3e0", - "metadata": {}, - "source": [ - "Version 1\n", - "```python\n", - "studentone_grade=90\n", - "StudentGrade2=50\n", - "stu3gr=74\n", - "avggrade=(studentone_grade+StudentGrade2+stut3gr)/3\n", - "```\n", - "\n", - "Version 2 \n", - "```python\n", - "student1_grade = 90\n", - "student2_grade = 50\n", - "student3_grade = 74\n", - "average_grade = ((student1_grade\n", - " + student2_grade\n", - " + student3_grade)\n", - " / 3)\n", - "```\n" + "not False" ] }, { "cell_type": "code", "execution_count": null, - "id": "e2ac4eae", + "id": "3a0e55e0", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "42\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 140, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "# function: block of code that performs a task\n", - "# can take zero or more inputs and return an output\n", - "\n", - "print(42)" + "3 == 3" ] }, { "cell_type": "code", "execution_count": 141, - "id": "f7c97d25", + "id": "6bc81c5a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "float" + "False" ] }, "execution_count": 141, @@ -1047,19 +2231,36 @@ } ], "source": [ - "type(42.0)" + "not (3 == 3)" + ] + }, + { + "cell_type": "markdown", + "id": "d89739d0", + "metadata": {}, + "source": [ + "## `and`\n", + "\n", + "Evaluates to `True` if both statements are true.\n", + "\n", + "|X|Y|X `and` Y|\n", + "|-|-|-|\n", + "|True|True|True|\n", + "|False|True|False|\n", + "|True|False|False|\n", + "|False|False|False|" ] }, { "cell_type": "code", "execution_count": 142, - "id": "549350d7", + "id": "75bc5247", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "bool" + "True" ] }, "execution_count": 142, @@ -1068,19 +2269,19 @@ } ], "source": [ - "type(True)" + "7 == 7 " ] }, { "cell_type": "code", "execution_count": 143, - "id": "8054647d", + "id": "5eeb52ea", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "43" + "True" ] }, "execution_count": 143, @@ -1089,19 +2290,19 @@ } ], "source": [ - "abs(-43)" + "32 > 9" ] }, { "cell_type": "code", "execution_count": 144, - "id": "ba5e2c18", + "id": "525f159b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "True" ] }, "execution_count": 144, @@ -1110,82 +2311,71 @@ } ], "source": [ - "round(2/3)" + "7 == 7 and 32 > 9 # True and True" ] }, { "cell_type": "code", - "execution_count": 146, - "id": "4de0a0f6", + "execution_count": null, + "id": "4e4a025c", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.6666666666666666" - ] - }, - "execution_count": 146, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "2 / 3" + "# and: check if the statements on both sides are true" ] }, { "cell_type": "code", - "execution_count": 147, - "id": "a9ee0753", + "execution_count": 146, + "id": "f62a8981", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "4.0" + "False" ] }, - "execution_count": 147, + "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "float(4)" + "'Python' == 'python'" ] }, { "cell_type": "code", - "execution_count": 148, - "id": "5cb855cf", + "execution_count": null, + "id": "0544ee19", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "4" + "False" ] }, - "execution_count": 148, + "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "int(4.0)" + "'Python' == 'python' and True # False and True" ] }, { "cell_type": "code", "execution_count": 149, - "id": "38734581", + "id": "540f788c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "int" + "True" ] }, "execution_count": 149, @@ -1194,19 +2384,21 @@ } ], "source": [ - "type(round(2 * 1.5))" + "is_summer = True\n", + "is_sunny = True\n", + "is_summer and is_sunny # True and True" ] }, { "cell_type": "code", "execution_count": 150, - "id": "b57533e7", + "id": "0dc91788", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "3.0" + "True" ] }, "execution_count": 150, @@ -1215,19 +2407,19 @@ } ], "source": [ - "2 * 1.5" + "45 != 90 " ] }, { "cell_type": "code", "execution_count": 151, - "id": "367b194d", + "id": "0c3b91ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "3" + "True" ] }, "execution_count": 151, @@ -1236,19 +2428,19 @@ } ], "source": [ - "round(3.0)" + "20 > 5" ] }, { "cell_type": "code", "execution_count": 152, - "id": "b0f10f1d", + "id": "26aa4a0e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "int" + "True" ] }, "execution_count": 152, @@ -1257,292 +2449,180 @@ } ], "source": [ - "type(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 153, - "id": "a2911504", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function round in module builtins:\n", - "\n", - "round(number, ndigits=None)\n", - " Round a number to a given precision in decimal digits.\n", - " \n", - " The return value is an integer if ndigits is omitted or None. Otherwise\n", - " the return value has the same type as the number. ndigits may be negative.\n", - "\n" - ] - } - ], - "source": [ - "help(round)" + "45 != 90 and 20 > 5 # True and True" ] }, { - "cell_type": "code", - "execution_count": 161, - "id": "cee2fa40", + "cell_type": "markdown", + "id": "69d408d2", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.66667" - ] - }, - "execution_count": 161, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "round(2/3, 5)" + "# To avoid confusion and to make sure you have consistent behaviour, you should always use boolean values (True and False) explicitly when working with logical operators." ] }, { "cell_type": "code", "execution_count": null, - "id": "34a135e0", + "id": "3fbf0d88", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "True" ] }, - "execution_count": 154, + "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "round(2/3)" + "# and operator returns the first falsy value encountered or the last truthy value if all \n", + "# operands are true\n", + "\n", + "is_winter = \"True\" # non-empty string so it is considered a True (bool)\n", + "is_cloudy = True # True (bool))\n", + "\n", + "is_winter and is_cloudy" ] }, { "cell_type": "code", - "execution_count": 156, - "id": "2f0e2761", + "execution_count": 154, + "id": "ee22fafa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0.6666666666666666" + "'True'" ] }, - "execution_count": 156, + "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "2 / 3 " + "is_cloudy and is_winter" ] }, { "cell_type": "code", - "execution_count": 157, - "id": "07e074d6", + "execution_count": null, + "id": "0140c606", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "3" + "'cat'" ] }, - "execution_count": 157, + "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "round(10/3)" + "my_pet = 'dog' # non-empty string so it is considered True (bool)\n", + "xindi_pet = 'cat' # non-empty string so it is considered True (bool)\n", + "\n", + "# 'dog' and 'cat'\n", + "my_pet and xindi_pet" ] }, { "cell_type": "code", - "execution_count": 159, - "id": "dc267146", + "execution_count": 156, + "id": "da71aa0e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "3.3333333333333335" + "'dog'" ] }, - "execution_count": 159, + "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "10 / 3" + "xindi_pet and my_pet" ] }, { "cell_type": "code", "execution_count": null, - "id": "7950db8b", - "metadata": {}, - "outputs": [], - "source": [ - "# function names:\n", - "# can use letters, digits, and underscores\n", - "# cannot start with a digit\n", - "# case sensitive\n", - "# typically functions start with lowercase letters\n", - "\n", - "\n", - "# parameters: variables used only within a function\n", - "# pass an argument to a function, argument value is assigned to a corresponding parameter\n", - "\n", - "# None: special data type representing no data\n", - "# if we don't have a return statement, Python will fill in an implicit one and return None" - ] - }, - { - "cell_type": "code", - "execution_count": 169, - "id": "488a0cd2", - "metadata": {}, - "outputs": [], - "source": [ - "def c_to_f(degrees_c):\n", - " degrees_f = (9 / 5) * degrees_c + 32\n", - " return degrees_f" - ] - }, - { - "cell_type": "code", - "execution_count": 163, - "id": "3c64200c", + "id": "c109dd78", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "212.0" + "''" ] }, - "execution_count": 163, + "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "c_to_f(100)" + "day_of_week = 'Wednesday' # non-empty string so it is considered True (bool)\n", + "month = '' # empty string so it is considered False (bool)\n", + "\n", + "# '' and 'Wednesday'\n", + "month and day_of_week" ] }, { "cell_type": "code", - "execution_count": 164, - "id": "6a569714", + "execution_count": 159, + "id": "aaac53a1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "12.2" + "False" ] }, - "execution_count": 164, + "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "c_to_f(-11)" - ] - }, - { - "cell_type": "code", - "execution_count": 170, - "id": "38c9d40f", - "metadata": {}, - "outputs": [], - "source": [ - "boiling_f = c_to_f(100)" + "False and True" ] }, { "cell_type": "code", - "execution_count": 171, - "id": "e2ef8f99", + "execution_count": 158, + "id": "164ecc30", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "212.0" + "0" ] }, - "execution_count": 171, + "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "boiling_f" - ] - }, - { - "cell_type": "code", - "execution_count": 172, - "id": "b1a72385", - "metadata": {}, - "outputs": [], - "source": [ - "def c_to_f(degrees_c):\n", - " degrees_f = (9 / 5) * degrees_c + 32\n", - " print(degrees_f)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1ac52ac1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "212.0\n" - ] - } - ], - "source": [ - "boiling_f = c_to_f(100) # output is None" - ] - }, - { - "cell_type": "code", - "execution_count": 177, - "id": "c021b747", - "metadata": {}, - "outputs": [], - "source": [ - "boiling_f" + "season = 'summer' # non-empty string so it is considered True (bool)\n", + "time_of_year = 0 # 0 so it is considered False (bool)\n", + "\n", + "season and 0" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1f8eb2d2", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { From 2b82b95f3350752760d8aeb7d4357041badeee60 Mon Sep 17 00:00:00 2001 From: kaylielau Date: Fri, 22 Aug 2025 01:31:50 +0100 Subject: [PATCH 3/5] Upload live code 08/21/2025 --- 04_this_cohort/live_code/08_21_2025.ipynb | 1535 +++++++++++++++++++++ 1 file changed, 1535 insertions(+) create mode 100644 04_this_cohort/live_code/08_21_2025.ipynb diff --git a/04_this_cohort/live_code/08_21_2025.ipynb b/04_this_cohort/live_code/08_21_2025.ipynb new file mode 100644 index 000000000..5da530ead --- /dev/null +++ b/04_this_cohort/live_code/08_21_2025.ipynb @@ -0,0 +1,1535 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 9, + "id": "36860799", + "metadata": {}, + "outputs": [], + "source": [ + "# August 21, 2025" + ] + }, + { + "cell_type": "markdown", + "id": "7fb0f1d5", + "metadata": {}, + "source": [ + "## `not`\n", + "\n", + "|X|`not` X|\n", + "|-|-|\n", + "|True|False|\n", + "|False|True|" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "833e8c5c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not (3 == 3)" + ] + }, + { + "cell_type": "markdown", + "id": "e4cc2032", + "metadata": {}, + "source": [ + "## `and`\n", + "\n", + "Evaluates to `True` if both statements are true.\n", + "\n", + "|X|Y|X `and` Y|\n", + "|-|-|-|\n", + "|True|True|True|\n", + "|False|True|False|\n", + "|True|False|False|\n", + "|False|False|False|" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "61a17c2e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(4 > 1) and (2 == 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "85151bcc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(4 == 1) and (2 == 2)" + ] + }, + { + "cell_type": "markdown", + "id": "49ac662f", + "metadata": {}, + "source": [ + "## To avoid confusion and ensure consistent behaviour, it's recommended to use boolean values (`True` or `False`) explicitly when working with logical operations to ensure predictable outcomes based on boolean logic." + ] + }, + { + "cell_type": "markdown", + "id": "05657084", + "metadata": {}, + "source": [ + "## `or`\n", + "\n", + "Evaluates to `True` if just one of the statements is true.\n", + "\n", + "|X|Y|X `or` Y|\n", + "|-|-|-|\n", + "|True|True|True|\n", + "|False|True|True|\n", + "|True|False|True|\n", + "|False|False|False|" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f7b847be", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Python' == 'python' or True" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "d3f1bad5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Python' == 'python'" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "a9efd3c7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not (7 % 2 == 1) or False" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e1df7477", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "7 % 2" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b9620c57", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 == 1" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "dba54401", + "metadata": {}, + "outputs": [], + "source": [ + "# and operator: return the first falsy value encountered or the last truthy value if all operands \n", + "# are `True` based on the conditions provided\n", + "\n", + "# or operator: return the first truthy value encountered or the last falsy value if all operands\n", + "# are `False` based on the conditions provided" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "486c4a6b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'dog'" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# \"dog\" is a non-empty string, True\n", + "# \"cat\" is a non-empty string, True\n", + "\n", + "\"dog\" or \"cat\" " + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "a55a4a8a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 0 is False\n", + "\n", + "0 or 0" + ] + }, + { + "cell_type": "markdown", + "id": "33e284de", + "metadata": {}, + "source": [ + "## Operator precedence\n", + "\n", + "Boolean operators are evaluated after arithmetic and comparison operators.\n", + "\n", + "| Order | Operator | Description |\n", + "|---|---|---|\n", + "| 1 | `**` | Exponentiation |\n", + "| 2 | `-`| Negation |\n", + "| 3 | `*`, `/`, `//`, `%` | Multiplication, division, integer division, and modulo |\n", + "| 4 | `+`, `-` | Addition and subtraction |\n", + "| 5 | `<`, `<=`, `>`, `>=`, `==`, `!=` | Less than, less than or equal to, greater than, greater than or equal to, equal, not equal |\n", + "| 6 | `not` | Not |\n", + "| 7 | `and` | And |\n", + "| 8 | `or` | Or|" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "b2337bd5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True and True or False" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "ec888abe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True or False" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "f3423fc1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not ((5 != 6) or (4 < 100))" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "bccb03f4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True or True" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "05431594", + "metadata": {}, + "outputs": [], + "source": [ + "year = 1990\n", + "\n", + "if year >= 2000:\n", + " print('We are in the 21st century.')" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "e20b919d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We are in the 21st century.\n" + ] + } + ], + "source": [ + "year = 2025\n", + "\n", + "if year >= 2000:\n", + " print(\"We are in the 21st century.\")\n", + "else:\n", + " print(\"We are not in the 21st century.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "05c050a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We are in the 21st century.\n" + ] + } + ], + "source": [ + "year = 2025\n", + "\n", + "if year >= 2000:\n", + " print('We are in the 21st century.')\n", + "elif year >= 1900:\n", + " print('We are in the 20th century.')\n", + "elif year >= 1800:\n", + " print('We are in the 19th century.')\n", + "else:\n", + " print('We have gone way back in time!')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "b9ba8d69", + "metadata": {}, + "outputs": [], + "source": [ + "def eye_exam_covered(age, qualifying_condition, time_since_last_exam):\n", + " # age is int\n", + "\n", + " # qualifying_condition is a bool: True is you have an eligible\n", + " # medical condition, False is you do not\n", + "\n", + " # time_since_last_exam is int representing the months since your last exam\n", + "\n", + " if age <= 19:\n", + " if time_since_last_exam >= 12:\n", + " return \"You are eligible for 1 major eye exam and any minor assessments\"\n", + " else:\n", + " return \"You are eligible for minor assessments, but not a major eye exam.\"\n", + " \n", + " elif 20 <= age <= 64: \n", + " if qualifying_condition:\n", + " if time_since_last_exam >= 12:\n", + " return \"You are eligible for 1 major eye exam and 2 additional follow up minor assessments\"\n", + " else:\n", + " return \"It hasn't been 12 months since your last major eye exam.\"\n", + " else:\n", + " return \"You do not have an eligible medical condition affecting your eyes.\"\n", + " \n", + " elif age >= 65:\n", + " if time_since_last_exam >= 18:\n", + " return \"You are eligible for 1 major eye exam and 2 additional follow-up minor assessments\"\n", + " else:\n", + " return \"It hasn't been 18 months since your last major eye exam\"\n", + " \n", + " else:\n", + " return \"Invalid age input.\"" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "3160470b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'You are eligible for 1 major eye exam and 2 additional follow-up minor assessments'" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "eye_exam_covered(67, True, 20)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "592bf5ec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'You are eligible for 1 major eye exam and 2 additional follow up minor assessments'" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "eye_exam_covered(27, True, 15)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "cb353640", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'You are eligible for minor assessments, but not a major eye exam.'" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "eye_exam_covered(19, False, 11)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "0bb21681", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'e', 'i', 'o', 'u']" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vowels = ['a', 'e', 'i', 'o', 'u']\n", + "vowels" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "b93df2aa", + "metadata": {}, + "outputs": [], + "source": [ + "empty_list = [] # conventional way\n", + "\n", + "empty_list2 = list()" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "1706ac14", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(empty_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "bfdc77fa", + "metadata": {}, + "outputs": [], + "source": [ + "scores = [90, 80, 82, 91, 80]\n", + "grades = ['K', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n", + "summary_functions = [len, sum, max, min]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a43a7e9", + "metadata": {}, + "outputs": [], + "source": [ + "mystery_solvers = [['Sherlock', 'Watson'], \n", + " ['Scooby', 'Shaggy', 'Fred'], \n", + " 'Nancy']" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "40be8280", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['K', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "73c342cb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'K'" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "442cbc38", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 7, 8]" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades[6:9]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "478374d5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[9, 10, 11, 12]" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades[-4:]" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "df0dbf67", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[45], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mgrades\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m13\u001b[39;49m\u001b[43m]\u001b[49m\n", + "\u001b[0;31mIndexError\u001b[0m: list index out of range" + ] + } + ], + "source": [ + "grades[13]" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "99217e5d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'dog' in 'examples of pets are dogs, cats, and birds'" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "3b4260a1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'e', 'i', 'o', 'u']" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vowels" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "926b9e8d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'y' in vowels" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "58e82724", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "37" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "perfect_squares = [1, 4, 9, 16, 25, 37, 49]\n", + "\n", + "perfect_squares[5]" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "6209aed4", + "metadata": {}, + "outputs": [], + "source": [ + "perfect_squares[5] = 36" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "6e39b032", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49]" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "perfect_squares" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "d8547186", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'cheese', 'bread']" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia_sandwich = ['bread', 'cheese', 'bread']\n", + "julia_sandwich" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "a1fee9b3", + "metadata": {}, + "outputs": [], + "source": [ + "kaylie_sandwich = julia_sandwich" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "407adedc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'cheese', 'bread']" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie_sandwich" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "42c233d9", + "metadata": {}, + "outputs": [], + "source": [ + "julia_sandwich[1] = 'tomato'" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "d50a687b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'tomato', 'bread']" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia_sandwich" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "7d0d687e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'tomato', 'bread']" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie_sandwich" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20262ed3", + "metadata": {}, + "outputs": [], + "source": [ + "# ^^^ lists are mutable data types" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "357aa997", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia_sandwich2 = 'cheese sandwich'\n", + "julia_sandwich2" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "6aa5cbc2", + "metadata": {}, + "outputs": [], + "source": [ + "kaylie_sandwich2 = julia_sandwich2" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "0ba67a45", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie_sandwich2" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "46f41187", + "metadata": {}, + "outputs": [], + "source": [ + "julia_sandwich2 = 'tomato sandwich'" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "0fe68d48", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'tomato sandwich'" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia_sandwich2" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "c81f7afc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cheese sandwich'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kaylie_sandwich2" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "7453d1c8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 1\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "0afaf657", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = a\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "251ca6b5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 2\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "ae3e5897", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "70ee88ab", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2]" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [1, 2]\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "8ce0aeec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2]" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = a\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "f89a9902", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 2]" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[0] = 6\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "84f772b6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 2]" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "28c33f2e", + "metadata": {}, + "outputs": [], + "source": [ + "combo = ['burger', 'fries', 'drink']\n", + "kid_meal = list(combo)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "858104ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['burger', 'fries', 'drink']" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "combo" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "b118a24f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['burger', 'fries', 'drink']" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kid_meal" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "9031f93d", + "metadata": {}, + "outputs": [], + "source": [ + "combo[0] = 'chicken sandwich'" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "44cdb53a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['chicken sandwich', 'fries', 'drink']" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "combo" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "6e1738ab", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['burger', 'fries', 'drink']" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kid_meal" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "2dd30b0c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49]" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "perfect_squares" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "aa4f3822", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(perfect_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "7fbc0e0e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49]" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "perfect_squares" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "68220fb6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "49" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(perfect_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "1d19d501", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49]" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "perfect_squares" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "da7925ed", + "metadata": {}, + "outputs": [], + "source": [ + "perfect_squares.append(64)" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "04c7a8a9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49, 64]" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "perfect_squares" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "299c554f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "dsi_participant", + "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.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From cd73e159c44fc21d31912f0e1d23a7681bf61442 Mon Sep 17 00:00:00 2001 From: Paul Radziievskyi Date: Sat, 23 Aug 2025 22:17:18 -0400 Subject: [PATCH 4/5] Completed assignment-1 file --- 02_activities/assignments/assignment_1.ipynb | 94 ++++++++++++++++---- 1 file changed, 77 insertions(+), 17 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 156d0408e..4f8bbbd5c 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -26,10 +26,10 @@ " * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.\n", "\n", "Checklist:\n", - "- [ ] Created a branch with the correct naming convention.\n", - "- [ ] Ensured that the repository is public.\n", - "- [ ] Reviewed the PR description guidelines and adhered to them.\n", - "- [ ] Verify that the link is accessible in a private browser window.\n", + "- [*] Created a branch with the correct naming convention.\n", + "- [*] Ensured that the repository is public.\n", + "- [*] Reviewed the PR description guidelines and adhered to them.\n", + "- [*] Verify that the link is accessible in a private browser window.\n", "\n", "If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#dc-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges." ] @@ -56,13 +56,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "These words are anagrams (not case-sensitive)\n" + ] + } + ], "source": [ "# For testing purposes, we will write our code in the function\n", "def anagram_checker(word_a, word_b):\n", - " # Your code here\n", + " if sorted(word_a.lower()) == sorted(word_b.lower()):\n", + " print(\"These words are anagrams (not case-sensitive)\")\n", + " else:\n", + " print(\"These words are NOT anagrams (not case-sensitive)\")\n", "\n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")" @@ -70,18 +81,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "These words are NOT anagrams (not case-sensitive)\n" + ] + } + ], "source": [ "anagram_checker(\"Silent\", \"Night\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "These words are anagrams (not case-sensitive)\n" + ] + } + ], "source": [ "anagram_checker(\"night\", \"Thing\")" ] @@ -99,20 +126,53 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def anagram_checker(word_a, word_b, is_case_sensitive):\n", - " # Modify your existing code here\n", + " if is_case_sensitive:\n", + " if sorted(word_a) == sorted(word_b):\n", + " return True\n", + " else:\n", + " return False\n", + " else:\n", + " if sorted(word_a.lower()) == sorted(word_b.lower()):\n", + " return True\n", + " else:\n", + " return False\n", "\n", "# Run your code to check using the words below:\n", + "# [PR] this is where I realized why we started with non-case-sensitive anagrams, despite it being more complicated\n", + "# [PR] tried to add descriptive strings in addition to the boolean T/F, but after struggling a bit, decided to just follow the instructions\n", "anagram_checker(\"Silent\", \"listen\", False) # True" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "anagram_checker(\"Silent\", \"Listen\", True) # False" ] @@ -130,7 +190,7 @@ ], "metadata": { "kernelspec": { - "display_name": "new-learner", + "display_name": "dsi_participant", "language": "python", "name": "python3" }, @@ -144,7 +204,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.9.19" } }, "nbformat": 4, From 1ac4e60b8c02ffc57c7a2985765c2c1c96612bfd Mon Sep 17 00:00:00 2001 From: Paul Radziievskyi Date: Tue, 26 Aug 2025 04:50:49 -0400 Subject: [PATCH 5/5] updated the assignment based on feedback --- 02_activities/assignments/assignment_1.ipynb | 70 ++++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 4f8bbbd5c..e5621ca5e 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -56,40 +56,43 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 1, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "These words are anagrams (not case-sensitive)\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# For testing purposes, we will write our code in the function\n", "def anagram_checker(word_a, word_b):\n", - " if sorted(word_a.lower()) == sorted(word_b.lower()):\n", - " print(\"These words are anagrams (not case-sensitive)\")\n", - " else:\n", - " print(\"These words are NOT anagrams (not case-sensitive)\")\n", - "\n", + " return sorted(word_a.lower()) == sorted(word_b.lower())\n", + " \n", "# Run your code to check using the words below:\n", "anagram_checker(\"Silent\", \"listen\")" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 2, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "These words are NOT anagrams (not case-sensitive)\n" - ] + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -98,15 +101,18 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 3, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "These words are anagrams (not case-sensitive)\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -124,7 +130,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -133,7 +139,7 @@ "True" ] }, - "execution_count": 28, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -141,15 +147,9 @@ "source": [ "def anagram_checker(word_a, word_b, is_case_sensitive):\n", " if is_case_sensitive:\n", - " if sorted(word_a) == sorted(word_b):\n", - " return True\n", - " else:\n", - " return False\n", + " return sorted(word_a) == sorted(word_b)\n", " else:\n", - " if sorted(word_a.lower()) == sorted(word_b.lower()):\n", - " return True\n", - " else:\n", - " return False\n", + " return sorted(word_a.lower()) == sorted(word_b.lower())\n", "\n", "# Run your code to check using the words below:\n", "# [PR] this is where I realized why we started with non-case-sensitive anagrams, despite it being more complicated\n", @@ -159,7 +159,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -168,7 +168,7 @@ "False" ] }, - "execution_count": 29, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" }