From 4e7a5d3feef88edaf0562bc1bfafb44398ad56d8 Mon Sep 17 00:00:00 2001 From: Magnus Tveit Date: Tue, 3 Feb 2026 17:33:23 -0700 Subject: [PATCH 1/2] AllMods --- Intro2Python/Module1-DataTypes.ipynb | 234 +++++++++++------- Intro2Python/Module2-Conditionals.ipynb | 110 ++++++-- Intro2Python/Module3-DataStructures.ipynb | 80 ++++-- Intro2Python/Module4-SlicingAndIndexing.ipynb | 38 ++- Intro2Python/Module5-LoopsComprehension.ipynb | 59 ++++- 5 files changed, 385 insertions(+), 136 deletions(-) diff --git a/Intro2Python/Module1-DataTypes.ipynb b/Intro2Python/Module1-DataTypes.ipynb index fc104f6..7748ecc 100644 --- a/Intro2Python/Module1-DataTypes.ipynb +++ b/Intro2Python/Module1-DataTypes.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -50,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -59,7 +59,7 @@ "239" ] }, - "execution_count": 4, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -79,7 +79,7 @@ "2" ] }, - "execution_count": 5, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -97,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -128,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -137,6 +137,7 @@ "text": [ "Hello world!\n", "This is my text\n", + "My first message is: Hello world! and my second message is: This is my text\n", "My first message is: Hello world! and my second message is: This is my text\n" ] } @@ -148,7 +149,8 @@ "\n", "print(e)\n", "print(my_text)\n", - "print(f\"My first message is: {e} and my second message is: {my_text}\") #this is an f-string" + "print(f\"My first message is: {e} and my second message is: {my_text}\") #this is an f-string\n", + "print(\"My first message is:\", e, \"and my second message is:\",my_text)" ] }, { @@ -160,7 +162,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -177,7 +179,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[6], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#try to change the strings\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(e[\u001b[38;5;241m0\u001b[39m]) \u001b[38;5;66;03m#Call the first character of the e string from above\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[43me\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mh\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;66;03m# This will raise an error because strings are immutable\u001b[39;00m\n", + "Cell \u001b[0;32mIn[12], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#try to change the strings\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(e[\u001b[38;5;241m0\u001b[39m]) \u001b[38;5;66;03m#Call the first character of the e string from above\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[43me\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mh\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;66;03m# This will raise an error because strings are immutable\u001b[39;00m\n", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } @@ -197,7 +199,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -235,27 +237,27 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "180" + "10" ] }, - "execution_count": 9, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "2 * 9*10" + "2 * 5" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -265,7 +267,7 @@ "The data type of variable 'a' is: \n", " the value is: 8\n", "The data type of variable 'b' is: \n", - " the value is: 16.0\n" + " the value is: 24.0\n" ] } ], @@ -275,14 +277,14 @@ "#print the data type of variable 'a'\n", "print(\"The data type of variable 'a' is:\", type(a), \"\\n the value is:\", a) #the \\n adds a new line\n", "\n", - "b = 2.0 * a\n", + "b = 2.0 * a + a\n", "#print the data type of variable 'b'\n", "print(f\"The data type of variable 'b' is: {type(b)} \\n the value is: {b}\") #using an f-string" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -290,18 +292,18 @@ "output_type": "stream", "text": [ "The data type of variable 'c' is: \n", - " the value is: 0.4\n" + " the value is: 4.8\n" ] } ], "source": [ - "c = 2 / 5\n", + "c = b / 5\n", "print(f\"The data type of variable 'c' is: {type(c)} \\n the value is: {c}\") #using an f-string, we divide two integers and get a float" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -327,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -369,7 +371,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -378,7 +380,7 @@ "8" ] }, - "execution_count": 15, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -404,7 +406,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -434,7 +436,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -460,7 +462,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -469,13 +471,13 @@ "'Cool Hydrology'" ] }, - "execution_count": 20, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'Cool ' + 'Hydrology' # concatenation of strings, the ' ' adds a space between the two strings" + "'Cool' + ' ' + 'Hydrology' # concatenation of strings, the ' ' adds a space between the two strings" ] }, { @@ -487,22 +489,22 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'aaaaaaaaaa'" + "'magmagmag'" ] }, - "execution_count": 22, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'a' * 10 # string repetition, this will repeat the string 'a' three times resulting in 'aaa'" + "'mag' * 3 # string repetition, this will repeat the string 'a' three times resulting in 'aaa'" ] }, { @@ -514,7 +516,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -524,7 +526,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[23], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\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;43m3\u001b[39;49m \u001b[38;5;66;03m# Division results in error, you cannot divide a string by a number\u001b[39;00m\n", + "Cell \u001b[0;32mIn[32], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\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;43m3\u001b[39;49m \u001b[38;5;66;03m# Division results in error, you cannot divide a string by a number\u001b[39;00m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'int'" ] } @@ -535,7 +537,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 33, "metadata": {}, "outputs": [ { @@ -545,7 +547,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[24], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\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;124;43m'\u001b[39;49m\u001b[38;5;124;43mb\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m \u001b[38;5;66;03m# Subtraction results in error\u001b[39;00m\n", + "Cell \u001b[0;32mIn[33], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\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;124;43m'\u001b[39;49m\u001b[38;5;124;43mb\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m \u001b[38;5;66;03m# Subtraction results in error\u001b[39;00m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'str'" ] } @@ -570,7 +572,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -592,20 +594,20 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Division: a / b = 2 / 27 = 0.07407\n" + "Division: a / b = 2 / 27 = 0.0740740741\n" ] } ], "source": [ "# f-strings with formatting for number of decimal places\n", - "print(f'Division: a / b = {a} / {b} = {a/b:.5f}') # The part ':.xf' specfifies 'x' decimals to be printed, in this case 3 decimals" + "print(f'Division: a / b = {a} / {b} = {a/b:.10f}') # The part ':.xf' specfifies 'x' decimals to be printed, in this case 3 decimals" ] }, { @@ -617,7 +619,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -642,27 +644,27 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "3" + "6" ] }, - "execution_count": 26, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "len('aaa') # returns the length of the string, in this case 3" + "len('magnus') # returns the length of the string, in this case 3" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -671,7 +673,7 @@ "7" ] }, - "execution_count": 29, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } @@ -690,7 +692,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 50, "metadata": {}, "outputs": [ { @@ -699,7 +701,7 @@ "29" ] }, - "execution_count": 30, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -722,7 +724,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 51, "metadata": {}, "outputs": [ { @@ -755,7 +757,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 52, "metadata": {}, "outputs": [ { @@ -792,7 +794,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -801,18 +803,18 @@ "False" ] }, - "execution_count": 33, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "name.endswith('g')" + "name.endswith('x')" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -821,7 +823,7 @@ "True" ] }, - "execution_count": 34, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -832,7 +834,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 57, "metadata": {}, "outputs": [ { @@ -841,7 +843,7 @@ "True" ] }, - "execution_count": 35, + "execution_count": 57, "metadata": {}, "output_type": "execute_result" } @@ -871,7 +873,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 59, "metadata": {}, "outputs": [ { @@ -887,7 +889,7 @@ "3" ] }, - "execution_count": 36, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -899,7 +901,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 60, "metadata": {}, "outputs": [ { @@ -908,7 +910,7 @@ "1" ] }, - "execution_count": 37, + "execution_count": 60, "metadata": {}, "output_type": "execute_result" } @@ -926,7 +928,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 61, "metadata": {}, "outputs": [ { @@ -935,7 +937,7 @@ "3" ] }, - "execution_count": 38, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } @@ -946,7 +948,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 62, "metadata": {}, "outputs": [ { @@ -955,7 +957,7 @@ "0" ] }, - "execution_count": 39, + "execution_count": 62, "metadata": {}, "output_type": "execute_result" } @@ -983,10 +985,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "62" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"Utah's SNOTEL sites are recording below average snow this year\"\n", + "len(s)" + ] }, { "cell_type": "markdown", @@ -998,10 +1014,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.endswith('year')" + ] }, { "cell_type": "markdown", @@ -1019,10 +1048,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are 62 SNOTEL sites in Utah.\n" + ] + } + ], + "source": [ + "print(f'There are {len(s)} SNOTEL sites in Utah.')" + ] }, { "cell_type": "markdown", @@ -1034,10 +1073,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.count(\"e\")" + ] }, { "cell_type": "markdown", @@ -1055,10 +1107,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "SNOTEL in situ monitoring sites support regional water management activities\n", + "USGE in situ monitoring sites support regional water management activities\n" + ] + } + ], + "source": [ + "string1 = 'SNOTEL in situ monitoring sites support regional water management activities'\n", + "print(string1)\n", + "string2 = string1.replace('SNOTEL', 'USGE')\n", + "print(string2)" + ] }, { "cell_type": "markdown", @@ -1076,7 +1142,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/Intro2Python/Module2-Conditionals.ipynb b/Intro2Python/Module2-Conditionals.ipynb index a598273..a070b51 100644 --- a/Intro2Python/Module2-Conditionals.ipynb +++ b/Intro2Python/Module2-Conditionals.ipynb @@ -143,10 +143,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "String has more than 100 characters\n" + ] + } + ], + "source": [ + "string = 'There are approximately 114 SNOTEL sites in Utah, as a part of the Natural Resouces Conservation Service (NRCS) and ~150 USGS NWIS streamflow monitoring gages.'\n", + "if len(string) > 100:\n", + " print(\"String has more than 100 characters\")\n", + "else:\n", + " print(\"String has less than or exactly 100 characters\")" + ] }, { "cell_type": "markdown", @@ -158,10 +172,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "24" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "string.count(' ')" + ] }, { "cell_type": "markdown", @@ -178,23 +205,43 @@ "Convert this pseudo code to a Python program:\n", "\n", "```python\n", - "if there are more occurrences of letter1 than letter2 in string:\n", - " print(\"There are more {insert_letter1} than {insert_letter2} \")\n", + "if len(letter1) > len(letter2): #there are more occurrences of letter1 than letter2 in string:\n", + " print(\"{letter1} is larger than {letter2}\")\n", " \n", - "elif there are more occurrences of letter2 than letter1 in string:\n", - " print(\"There are more {insert_letter2} than {insert_letter1} \")\n", + "elif len(letter1) < len(letter2): #there are more occurrences of letter2 than letter1 in string:\n", + " print(\"{letter2} is larger than {letter1}\")\n", "\n", "else:\n", - " print(\"There are exactly the same number {insert_letter1} and {insert_letter2}\")\n", + " print(\"{letter1} and {letter2} are equal in number\")\n", "```" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The variable known as letter2: {'SNOTEL sites'} has a length of 12 which is larger than letter1: {'USGS sites'} which has a length of 10\n" + ] + } + ], + "source": [ + "def check_relation(letter1, letter2):\n", + " if len(letter1) > len(letter2): #there are more occurrences of letter1 than letter2 in string:\n", + " print(\"The variable known as letter1:\",{letter1},\" has a length of\",len(letter1),\"which is larger than letter2:\",{letter2},\" which has a length of\",len(letter2))\n", + " \n", + " elif len(letter1) < len(letter2): #there are more occurrences of letter2 than letter1 in string:\n", + " print(\"The variable known as letter2:\",{letter2},\" has a length of\",len(letter2),\"which is larger than letter1:\",{letter1},\" which has a length of\",len(letter1))\n", + "\n", + " else:\n", + " print(\"The variables known as letter1:\",{letter1},\" and letter2:\",{letter2},\" are equal in length with a length of\",len(letter1)) \n", + "\n", + "check_relation('USGS sites', 'SNOTEL sites')" + ] }, { "cell_type": "markdown", @@ -207,10 +254,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The variable known as letter1: {'Very Long Message'} has a length of 17 which is larger than letter2: {'Short Message'} which has a length of 13\n" + ] + } + ], + "source": [ + "check_relation('Very Long Message', 'Short Message')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The variables known as letter1: {'Four'} and letter2: {'Five'} are equal in length with a length of 4\n" + ] + } + ], + "source": [ + "check_relation('Four', 'Five')" + ] }, { "cell_type": "markdown", @@ -228,7 +302,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/Intro2Python/Module3-DataStructures.ipynb b/Intro2Python/Module3-DataStructures.ipynb index 5d8b103..b06fdff 100644 --- a/Intro2Python/Module3-DataStructures.ipynb +++ b/Intro2Python/Module3-DataStructures.ipynb @@ -314,7 +314,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -340,7 +340,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -358,7 +358,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -368,7 +368,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[11], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m s1 \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mset\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m32\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m86\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m6\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Differnt than the dict() function, we cannot use set() to make a set from integer values directly\u001b[39;00m\n\u001b[1;32m 2\u001b[0m s1\n", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m s1 \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mset\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m32\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m86\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m6\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Differnt than the dict() function, we cannot use set() to make a set from integer values directly\u001b[39;00m\n\u001b[1;32m 2\u001b[0m s1\n", "\u001b[0;31mTypeError\u001b[0m: set expected at most 1 argument, got 6" ] } @@ -380,20 +380,20 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "List with duplicates: [1, 2, 3, 4, 5, 2, 2, 3, 1]. It has 9 elements.\n", + "List with duplicates: [1, 2, 5, 4, 3, 2, 2, 3, 1]. It has 9 elements.\n", "Set s3 created from list_with_duplicates: {1, 2, 3, 4, 5}. It has 5 elements.\n" ] } ], "source": [ - "list_with_duplicates = [1, 2, 3, 4, 5, 2, 2, 3, 1] #create a list with duplicates\n", + "list_with_duplicates = [1, 2, 5, 4, 3, 2, 2, 3, 1] #create a list with duplicates\n", "print(f\"List with duplicates: {list_with_duplicates}. It has {len(list_with_duplicates)} elements.\")\n", "\n", "# Create a set of the list (which removed duplicates)\n", @@ -810,10 +810,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Hi!', 'Hello', 'Hi', 'Hey', 'hey']\n" + ] + } + ], + "source": [ + "L2 = ['Hi', 'Hello', 'Hi!', 'Hey', 'Hi', 'hey', 'Hey']\n", + "L2_unique = list(set(L2))\n", + "print(L2_unique)" + ] }, { "cell_type": "markdown", @@ -830,10 +842,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 122\n", + "3 535\n", + "t T\n", + "rum cola\n" + ] + } + ], + "source": [ + "d = {2: 122, 3: 535, 't': 'T', 'rum': 'cola'}\n", + "\n", + "for key in d:\n", + " print(key, d[key])\n", + " " + ] }, { "cell_type": "markdown", @@ -850,10 +879,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['HE210B', 'HE210A', 'HE200A']\n" + ] + } + ], + "source": [ + "#Find the intersection of the two lists (elements that occur in both lists)\n", + "\n", + "s1 = ['HE170B', 'HE210B', 'HE190A', 'HE200A', 'HE210A', 'HE210A']\n", + "\n", + "s2 = ['HE200A', 'HE210A', 'HE240A', 'HE200A', 'HE210B', 'HE340A']\n", + "\n", + "intersection = list(set(s1) & set(s2))\n", + "print(intersection)" + ] }, { "cell_type": "markdown", @@ -871,7 +917,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/Intro2Python/Module4-SlicingAndIndexing.ipynb b/Intro2Python/Module4-SlicingAndIndexing.ipynb index d90a100..7d16fd8 100644 --- a/Intro2Python/Module4-SlicingAndIndexing.ipynb +++ b/Intro2Python/Module4-SlicingAndIndexing.ipynb @@ -314,10 +314,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First element: 10\n", + "Last element: 60\n" + ] + } + ], + "source": [ + "L1 = [10, 20, 30, 40, 50, 60]\n", + "print(\"First element:\", L1[0])\n", + "print(\"Last element:\", L1[-1])" + ] }, { "cell_type": "markdown", @@ -333,10 +346,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[20, 30, 40]\n" + ] + } + ], + "source": [ + "extracted_list = L1[1:4]\n", + "print(extracted_list)" + ] }, { "cell_type": "markdown", @@ -354,7 +378,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/Intro2Python/Module5-LoopsComprehension.ipynb b/Intro2Python/Module5-LoopsComprehension.ipynb index b5a4d26..a4dcefa 100644 --- a/Intro2Python/Module5-LoopsComprehension.ipynb +++ b/Intro2Python/Module5-LoopsComprehension.ipynb @@ -514,10 +514,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 squared is 529\n", + "73 squared is 5329\n", + "12 squared is 144\n", + "84 squared is 7056\n" + ] + } + ], + "source": [ + "n = [23, 73, 12, 84]\n", + "for i in n:\n", + " print(f'{i} squared is {i**2}')" + ] }, { "cell_type": "markdown", @@ -533,10 +548,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[78.53975, 113.09724, 201.06176, 314.159, 490.87343749999997, 804.24704]\n" + ] + } + ], + "source": [ + "diameters = [10, 12, 16, 20, 25, 32]\n", + "areas = [3.14159 * (d/2)**2 for d in diameters]\n", + "print(areas)" + ] }, { "cell_type": "markdown", @@ -551,10 +578,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The words with five characters are: ['Alpha', 'Bravo', 'Delta']\n" + ] + } + ], + "source": [ + "phonetic_alphabet = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot']\n", + "five = [word for word in phonetic_alphabet if len(word) == 5]\n", + "print(\"The words with five characters are:\", five)" + ] }, { "cell_type": "markdown", @@ -572,7 +611,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, From 1f6ac64a679fd94c858204ce506bedbbfd076f03 Mon Sep 17 00:00:00 2001 From: Magnus Tveit Date: Thu, 5 Feb 2026 16:56:45 -0700 Subject: [PATCH 2/2] HoldNP --- NumPy/1. Data Types.ipynb | 2 +- NumPy/2. Create an Array.ipynb | 65 ++++++++++++++----------------- NumPy/3. Inspect an Array.ipynb | 6 +-- NumPy/4. Sampling Methods.ipynb | 2 +- NumPy/5. Math Functions.ipynb | 18 +++++---- NumPy/6. Sort an Array.ipynb | 10 +++-- NumPy/7. Slicing & Indexing.ipynb | 8 ++-- 7 files changed, 54 insertions(+), 57 deletions(-) diff --git a/NumPy/1. Data Types.ipynb b/NumPy/1. Data Types.ipynb index f6feb84..060cedf 100644 --- a/NumPy/1. Data Types.ipynb +++ b/NumPy/1. Data Types.ipynb @@ -576,7 +576,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/NumPy/2. Create an Array.ipynb b/NumPy/2. Create an Array.ipynb index 8cd63b0..6c1a811 100644 --- a/NumPy/2. Create an Array.ipynb +++ b/NumPy/2. Create an Array.ipynb @@ -97,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -128,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -162,7 +162,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -192,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -222,7 +222,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -241,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -268,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -300,7 +300,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -320,7 +320,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -355,22 +355,22 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[9.68970833e-317 0.00000000e+000 1.14051258e-310 1.14051258e-310\n", - " 1.14051261e-310]\n", - " [1.14051258e-310 1.14051258e-310 1.14051258e-310 1.14051258e-310\n", - " 1.14051258e-310]\n", - " [1.14051258e-310 1.14051258e-310 1.14051258e-310 1.14051258e-310\n", - " 1.14051258e-310]\n", - " [1.14051258e-310 1.14051258e-310 1.14051258e-310 1.14051258e-310\n", - " 1.14051258e-310]\n", - " [1.14051258e-310 1.14051258e-310 1.14051258e-310 1.14051258e-310\n", + "[[1.01345339e-316 0.00000000e+000 1.13409464e-310 1.13409464e-310\n", + " 1.13409464e-310]\n", + " [1.13409464e-310 1.13409464e-310 1.13409464e-310 1.13409464e-310\n", + " 1.13409464e-310]\n", + " [1.13409464e-310 1.13409464e-310 1.13409464e-310 1.13409464e-310\n", + " 1.13409464e-310]\n", + " [1.13409464e-310 1.13409464e-310 1.13409464e-310 1.13409464e-310\n", + " 1.13409464e-310]\n", + " [1.13409464e-310 1.13409464e-310 1.13409464e-310 1.13409464e-310\n", " 1.66006057e-321]]\n" ] } @@ -382,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 52, "metadata": {}, "outputs": [ { @@ -413,7 +413,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, "outputs": [ { @@ -434,7 +434,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 54, "metadata": {}, "outputs": [ { @@ -473,7 +473,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 55, "metadata": {}, "outputs": [ { @@ -518,7 +518,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -538,7 +538,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, "outputs": [ { @@ -557,7 +557,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -565,9 +565,7 @@ "output_type": "stream", "text": [ "[[0 1 2 0 1 2]\n", - " [3 4 5 3 4 5]\n", - " [0 1 2 0 1 2]\n", - " [3 4 5 3 4 5]]\n" + " [0 1 2 0 1 2]]\n" ] } ], @@ -585,18 +583,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[0.69646919 0.28613933 0.22685145 0.55131477 0.71946897]\n", - " [0.42310646 0.9807642 0.68482974 0.4809319 0.39211752]\n", - " [0.34317802 0.72904971 0.43857224 0.0596779 0.39804426]\n", - " [0.73799541 0.18249173 0.17545176 0.53155137 0.53182759]\n", - " [0.63440096 0.84943179 0.72445532 0.61102351 0.72244338]]\n", "[[0.69646919 0.28613933 0.22685145 0.55131477 0.71946897]\n", " [0.42310646 0.9807642 0.68482974 0.4809319 0.39211752]\n", " [0.34317802 0.72904971 0.43857224 0.0596779 0.39804426]\n", @@ -744,7 +737,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/NumPy/3. Inspect an Array.ipynb b/NumPy/3. Inspect an Array.ipynb index 413d0a9..9329537 100644 --- a/NumPy/3. Inspect an Array.ipynb +++ b/NumPy/3. Inspect an Array.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -42,7 +42,7 @@ "aligned: True\n", "contiguous: True\n", "fortran: False\n", - "data pointer: 0x197d0a0\n", + "data pointer: 0x2a82d60\n", "byteorder: little\n", "byteswap: False\n", "type: int64\n", @@ -240,7 +240,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/NumPy/4. Sampling Methods.ipynb b/NumPy/4. Sampling Methods.ipynb index 0d19af7..8fbed23 100644 --- a/NumPy/4. Sampling Methods.ipynb +++ b/NumPy/4. Sampling Methods.ipynb @@ -427,7 +427,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/NumPy/5. Math Functions.ipynb b/NumPy/5. Math Functions.ipynb index 55b1106..da111a1 100644 --- a/NumPy/5. Math Functions.ipynb +++ b/NumPy/5. Math Functions.ipynb @@ -762,16 +762,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "fractional: [0.85947941 0.77912359 0.79646157 0.47671458 0.52884718 0.26675636\n", - " 0.07553753 0.84070297 0.8926896 0.17344596]\n", - "integral: [8. 0. 9. 2. 7. 5. 9. 8. 0. 5.]\n" + "ename": "NameError", + "evalue": "name 'np' 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[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m rs \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241m.\u001b[39mrandom\u001b[38;5;241m.\u001b[39mRandomState(\u001b[38;5;241m321\u001b[39m) \u001b[38;5;66;03m# create a random state object\u001b[39;00m\n\u001b[1;32m 2\u001b[0m arr1 \u001b[38;5;241m=\u001b[39m rs\u001b[38;5;241m.\u001b[39mrand(\u001b[38;5;241m10\u001b[39m) \u001b[38;5;241m*\u001b[39m \u001b[38;5;241m10\u001b[39m \u001b[38;5;66;03m# create an array of 10 random numbers scaled by 10\u001b[39;00m\n\u001b[1;32m 3\u001b[0m fr, intg \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mmodf(arr1) \u001b[38;5;66;03m# separate the fractional and integral parts\u001b[39;00m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" ] } ], @@ -928,7 +930,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/NumPy/6. Sort an Array.ipynb b/NumPy/6. Sort an Array.ipynb index 3455e6f..10c8412 100644 --- a/NumPy/6. Sort an Array.ipynb +++ b/NumPy/6. Sort an Array.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -58,7 +58,9 @@ ], "source": [ "# sort along the column and return a copy\n", - "print(np.sort(arr, axis=0)) \n", + "\n", + "arr2 = (np.sort(arr, axis=0)) \n", + "print(arr2) # sorted array\n", "print(\" \")\n", "print(arr) # original array remains unsorted" ] @@ -228,7 +230,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/NumPy/7. Slicing & Indexing.ipynb b/NumPy/7. Slicing & Indexing.ipynb index 87c4019..e3e1329 100644 --- a/NumPy/7. Slicing & Indexing.ipynb +++ b/NumPy/7. Slicing & Indexing.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -73,7 +73,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[5], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m arr \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray(\u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m90\u001b[39m)) \u001b[38;5;66;03m# Create a numpy array with values from 0 to 99\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(arr)\n\u001b[0;32m----> 4\u001b[0m arr1 \u001b[38;5;241m=\u001b[39m \u001b[43marr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Reshape the array to 10x10\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(arr1)\n", + "Cell \u001b[0;32mIn[4], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m arr \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray(\u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m90\u001b[39m)) \u001b[38;5;66;03m# Create a numpy array with values from 0 to 99\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(arr)\n\u001b[0;32m----> 4\u001b[0m arr1 \u001b[38;5;241m=\u001b[39m \u001b[43marr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Reshape the array to 10x10\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(arr1)\n", "\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 90 into shape (10,10)" ] } @@ -619,7 +619,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" },