From ddd8e5d06101ea2323c44b97b1b99f61545ecae5 Mon Sep 17 00:00:00 2001 From: Anna Sosnovske Date: Tue, 3 Feb 2026 12:19:47 -0700 Subject: [PATCH] Completed exercises --- Intro2Python/Module1-DataTypes.ipynb | 96 +++++++++++++++---- Intro2Python/Module2-Conditionals.ipynb | 85 +++++++++++++--- Intro2Python/Module3-DataStructures.ipynb | 70 ++++++++++++-- Intro2Python/Module4-SlicingAndIndexing.ipynb | 41 ++++++-- Intro2Python/Module5-LoopsComprehension.ipynb | 65 +++++++++++-- 5 files changed, 301 insertions(+), 56 deletions(-) diff --git a/Intro2Python/Module1-DataTypes.ipynb b/Intro2Python/Module1-DataTypes.ipynb index fe81974..f73a4d6 100644 --- a/Intro2Python/Module1-DataTypes.ipynb +++ b/Intro2Python/Module1-DataTypes.ipynb @@ -983,10 +983,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "62" + ] + }, + "execution_count": 1, + "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 +1012,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.endswith('year')" + ] }, { "cell_type": "markdown", @@ -1019,10 +1046,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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 +1071,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.count('e')" + ] }, { "cell_type": "markdown", @@ -1055,10 +1105,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "SNOTEL in situ monitoring sites support regional water management activities\n", + "USGS in situ monitoring sites support regional water management activities\n" + ] + } + ], + "source": [ + "string1 = 'SNOTEL in situ monitoring sites support regional water management activities'\n", + "string2= string1.replace('SNOTEL','USGS')\n", + "print(string1)\n", + "print(string2)" + ] }, { "cell_type": "markdown", @@ -1076,7 +1140,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..a23b01a 100644 --- a/Intro2Python/Module2-Conditionals.ipynb +++ b/Intro2Python/Module2-Conditionals.ipynb @@ -143,10 +143,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "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,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "24\n" + ] + } + ], + "source": [ + "print(string.count(' '))" + ] }, { "cell_type": "markdown", @@ -191,10 +215,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are more SNOTEL sites than USGS sites \n" + ] + } + ], + "source": [ + "letter1 = 'USGS sites'\n", + "letter2 = 'SNOTEL sites'\n", + "if string.count(letter1) > string.count(letter2):\n", + " print(f'There are more {letter1} than {letter2}')\n", + "elif string.count(letter2) > string.count(letter1):\n", + " print(f'There are more {letter2} than {letter1} ')\n", + "else: \n", + " print(f'There are exactly the same number {letter1} and {letter2}')\n", + "\n" + ] }, { "cell_type": "markdown", @@ -207,10 +249,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are more s than f\n" + ] + } + ], + "source": [ + "letter1 = 's'\n", + "letter2 = 'f'\n", + "if string.count(letter1) > string.count(letter2):\n", + " print(f'There are more {letter1} than {letter2}')\n", + "elif string.count(letter2) > string.count(letter1):\n", + " print(f'There are more {letter2} than {letter1} ')\n", + "else: \n", + " print(f'There are exactly the same number {letter1} and {letter2}')" + ] }, { "cell_type": "markdown", @@ -228,7 +287,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..fbdebd0 100644 --- a/Intro2Python/Module3-DataStructures.ipynb +++ b/Intro2Python/Module3-DataStructures.ipynb @@ -810,10 +810,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Hello', 'Hi', 'Hi!', 'hey', 'Hey'}\n" + ] + } + ], + "source": [ + "L2 = ['Hi', 'Hello', 'Hi!', 'Hey', 'Hi', 'hey', 'Hey']\n", + "S2 = set(L2) \n", + "L2= S2 \n", + "print(L2)" + ] }, { "cell_type": "markdown", @@ -830,10 +843,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "122\n", + "535\n", + "T\n", + "cola\n" + ] + } + ], + "source": [ + "d = {2: 122, 3: 535, 't': 'T', 'rum': 'cola'}\n", + "print(d[2])\n", + "print(d[3])\n", + "print(d['t'])\n", + "print(d['rum'])" + ] }, { "cell_type": "markdown", @@ -850,10 +880,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "{'HE170B', 'HE190A', 'HE200A', 'HE210A', 'HE210B', 'HE240A', 'HE340A'}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1 = ['HE170B', 'HE210B', 'HE190A', 'HE200A', 'HE210A', 'HE210A']\n", + "\n", + "s2 = ['HE200A', 'HE210A', 'HE240A', 'HE200A', 'HE210B', 'HE340A']\n", + "\n", + "s1= set(s1)\n", + "s2= set(s2)\n", + "\n", + "s1.union(s2)" + ] }, { "cell_type": "markdown", @@ -871,7 +921,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..4aaa7be 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": [ + "10\n", + "60\n" + ] + } + ], + "source": [ + "L1 = [10, 20, 30, 40, 50, 60]\n", + "print (L1[0])\n", + "print (L1[-1])" + ] }, { "cell_type": "markdown", @@ -333,10 +346,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[20, 30, 40]\n" + ] + } + ], + "source": [ + "L1 = [10, 20, 30, 40, 50, 60]\n", + "start = 1\n", + "stop = 4\n", + "\n", + "print( L1[start:stop])" + ] }, { "cell_type": "markdown", @@ -354,7 +381,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..28ac73b 100644 --- a/Intro2Python/Module5-LoopsComprehension.ipynb +++ b/Intro2Python/Module5-LoopsComprehension.ipynb @@ -514,10 +514,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "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 num in n:\n", + " print(f'{num} squared is {num**2}')" + ] }, { "cell_type": "markdown", @@ -533,10 +548,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[157.0, 226.08, 401.92, 628.0, 981.25, 1607.68]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diameters = [10, 12, 16, 20, 25, 32]\n", + "areas = [1.57*elem **2 for elem in diameters]\n", + "areas" + ] }, { "cell_type": "markdown", @@ -551,10 +581,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Alpha\n", + "Bravo\n", + "Delta\n" + ] + } + ], + "source": [ + "phonetic_alphabet = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot']\n", + "for code in phonetic_alphabet:\n", + " if len(code) == 5:\n", + " print(code)" + ] }, { "cell_type": "markdown", @@ -572,7 +617,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" },