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
66 changes: 57 additions & 9 deletions Intro2Python/Module5-LoopsComprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,26 @@
},
{
"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 x in n:\n",
" print(f'{x} squared is {x**2}')\n",
" "
]
},
{
"cell_type": "markdown",
Expand All @@ -535,8 +551,27 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"[78.5, 113.04, 200.96, 314.0, 490.625, 803.84]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"diameters = [10, 12, 16, 20, 25, 32]\n",
"\n",
"Areas = [3.14*(d/2)**2 for d in diameters]\n",
"Areas\n",
"\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -551,10 +586,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Alpha', 'Bravo', 'Delta']\n"
]
}
],
"source": [
"phonetic_alphabet = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot']\n",
"\n",
"five_letter_words = [word for word in phonetic_alphabet if len(word) == 5]\n",
"print(five_letter_words)"
]
},
{
"cell_type": "markdown",
Expand All @@ -572,7 +620,7 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "p310env",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand Down
49 changes: 24 additions & 25 deletions Intro2Python/Module6-Functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -109,7 +109,7 @@
],
"source": [
"def first_char(word): #initiate a function first_char with parameter word\n",
" word[0] # <--- No return statement, function returns None\n",
" word[0] # <--- No return statement, function returns None\n",
" \n",
" \n",
"# Variable a will be equal to None\n",
Expand All @@ -128,7 +128,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -168,7 +168,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -184,21 +184,21 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Flow rate Q: 43.86 cfs\n"
"Flow rate Q: 56.20 cfs\n"
]
}
],
"source": [
"width = 10 #channel width in ft\n",
"depth = 1 #channel depth in ft\n",
"slope = 0.01 #channel slope in ft/ft\n",
"depth = 2 #channel depth in ft\n",
"slope = 0.002 #channel slope in ft/ft\n",
"\n",
"Q = mannings_equation_rect(width, depth, slope) #call mannings_equation_rect function with width, depth, slope, and default n value\n",
"print(f'Flow rate Q: {Q:.2f} cfs') #print"
Expand All @@ -215,7 +215,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand All @@ -241,7 +241,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -273,7 +273,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -352,41 +352,40 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def mannings_equation_rect(width, depth, slope): #initiate function mannings_equation with parameters width, depth, slope\n",
"def mannings_equation_rect(width, depth, slope, n): #initiate function mannings_equation with parameters width, depth, slope\n",
" ''' Calculate flow rate Q using Manning's equation for a rectangular channel '''\n",
" A = width * depth #cross-sectional flow area in sqft\n",
" P = width + 2 * depth #wetted perimeter in ft\n",
" R = A / P #hydraulic radius in ft\n",
" Q = (1.486/n) * A * R**(2/3) * slope**0.5 #Manning's equation using global n\n",
" return Q #return flow rate Q, cfs"
" return Q, A, P, R #return flow rate Q, A, P, R\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 27,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "mannings_equation_rect() takes 3 positional arguments but 4 were given",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[12], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m width, depth, slope \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m0.01\u001b[39m \u001b[38;5;66;03m#channel width in ft, channel depth in ft, channel slope in ft/ft\u001b[39;00m\n\u001b[1;32m 3\u001b[0m n \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0.02\u001b[39m \u001b[38;5;66;03m#global variable n\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m Q \u001b[38;5;241m=\u001b[39m \u001b[43mmannings_equation_rect\u001b[49m\u001b[43m(\u001b[49m\u001b[43mwidth\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdepth\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mslope\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m#initiate function mannings_equation with parameters width, depth, slope, n\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mFlow rate Q: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mQ\u001b[38;5;132;01m:\u001b[39;00m\u001b[38;5;124m.2f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m cfs\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mArea: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mA\u001b[38;5;132;01m:\u001b[39;00m\u001b[38;5;124m.2f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m sqft\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;66;03m#this shoould give an error since A is a local variable inside the function, why do we get a value?\u001b[39;00m\n",
"\u001b[0;31mTypeError\u001b[0m: mannings_equation_rect() takes 3 positional arguments but 4 were given"
"name": "stdout",
"output_type": "stream",
"text": [
"Flow rate Q: 65.80 cfs\n",
"Area: 10.00 sqft\n",
"R: 0.83 ft\n",
"Perimeter: 12.00 ft\n"
]
}
],
"source": [
"#lets go back to our mannings equation function to see local vs global variables\n",
"width, depth, slope = 10, 1, 0.01 #channel width in ft, channel depth in ft, channel slope in ft/ft\n",
"n = 0.02 #global variable n\n",
"Q = mannings_equation_rect(width, depth, slope, n) #initiate function mannings_equation with parameters width, depth, slope, n\n",
"Q, A, P, R = mannings_equation_rect(width, depth, slope, n) #initiate function mannings_equation with parameters width, depth, slope, n\n",
"\n",
"print(f'Flow rate Q: {Q:.2f} cfs')\n",
"print(f'Area: {A:.2f} sqft') #this shoould give an error since A is a local variable inside the function, why do we get a value?\n",
Expand Down Expand Up @@ -501,7 +500,7 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "p310env",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand Down
Loading