Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 150 additions & 84 deletions Intro2Python/Module1-DataTypes.ipynb

Large diffs are not rendered by default.

110 changes: 92 additions & 18 deletions Intro2Python/Module2-Conditionals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -228,7 +302,7 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "p310env",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand Down
80 changes: 63 additions & 17 deletions Intro2Python/Module3-DataStructures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -340,7 +340,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -358,7 +358,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -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"
]
}
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -871,7 +917,7 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "p310env",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand Down
38 changes: 31 additions & 7 deletions Intro2Python/Module4-SlicingAndIndexing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -354,7 +378,7 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "p310env",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand Down
Loading