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
164 changes: 133 additions & 31 deletions CodeSprints/Python101.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
},
"outputs": [],
"source": [
"name = \"Gandolf\" #type your name before the pound sign, make sure you put it in quotes!\n",
"age = \"135\" #type your age before the pound sign, make sure you put it in quotes! More on this later.\n",
"address = \"The Shire\" #type your address before the pound sign, make sure you put it in quotes!\n",
"story = \"My name is \" + name + \". I am \" + age + \" years old. \\nI am a data scientist! \\nIt's kind of like being a wizard. \\nTo see if I can tell you something about your environmental past or future, visit me at \" + address + \".\"\n",
"name = \"Pierre\" #type your name before the pound sign, make sure you put it in quotes!\n",
"age = \"1\" #type your age before the pound sign, make sure you put it in quotes! More on this later.\n",
"address = \"Fance\" #type your address before the pound sign, make sure you put it in quotes!\n",
"story = \"My name is \" + name + \". I am \" + age + \" year old. \\nI am a not data scientist! \\nIt's kind of like being a wizard. \\nTo see if I can tell you something about your environmental past or future, visit me at \" + address + \".\"\n",
"print(story)\n"
]
},
Expand Down Expand Up @@ -155,7 +155,7 @@
"source": [
"#But we cannot input a numeric value into a string:\n",
"#Copy and paste this code into the code cell under task 2. Edit it to complete task 2.\n",
"story = \"My name is \" + name + \". I am \" + age + \" years old. Find me at \" + address + \".\"\n",
"story = \"My name is \" + name + \". I am \" + str(age) + \" years old. Find me at \" + address + \".\"\n",
"print(story)\n"
]
},
Expand All @@ -182,7 +182,9 @@
},
"outputs": [],
"source": [
"#Type story here\n"
"#Type story here\n",
"story = \"My name is \" + name + \". I am \" + str(age) + \" years old. Find me at \" + address + \".\"\n",
"print(story)"
]
},
{
Expand Down Expand Up @@ -273,12 +275,12 @@
"outputs": [],
"source": [
"#first, convert \"age\" back to a numeric (float) variable\n",
"age =\n",
"age = float(age)\n",
"\n",
"#Complete your equations in python syntax below. Remember to use \"age\" as a variable.\n",
"a =\n",
"b =\n",
"c =\n",
"a = 5 + age\n",
"b = age*12\n",
"c = age < 100\n",
"print(a,b,c)"
]
},
Expand Down Expand Up @@ -365,7 +367,12 @@
},
"outputs": [],
"source": [
"#write tip15 function here\n"
"#write tip15 function here\n",
"def tip15(price, tip_perc):\n",
" res = (1 + tip_perc/100)*price\n",
" return res\n",
"\n",
"print(tip15(42.75, 15))"
]
},
{
Expand All @@ -387,7 +394,12 @@
},
"outputs": [],
"source": [
"#write tip function here\n"
"#write tip function here\n",
"def tip(price, tip_perc):\n",
" res = (1 + tip_perc/100)*price\n",
" return res\n",
"\n",
"print(tip15(42.75, 25))"
]
},
{
Expand Down Expand Up @@ -511,7 +523,9 @@
},
"outputs": [],
"source": [
"# type answer here\n"
"# type answer here\n",
"print(\"Name: \" + names_and_ages[0][2])\n",
"print(\"Age: \" + str(names_and_ages[1][2]))"
]
},
{
Expand All @@ -532,7 +546,10 @@
},
"outputs": [],
"source": [
"# type answer here\n"
"# type answer here\n",
"names_and_ages[0].append(\"Grandpa\")\n",
"names_and_ages[1].append(67)\n",
"print(names_and_ages)"
]
},
{
Expand All @@ -553,7 +570,9 @@
},
"outputs": [],
"source": [
"# type answer here\n"
"# type answer here\n",
"names_and_ages[1][3] += 1\n",
"print(names_and_ages)"
]
},
{
Expand Down Expand Up @@ -659,7 +678,10 @@
},
"outputs": [],
"source": [
"# type your for loop here\n"
"# type your for loop here\n",
"for i in range(len(names_and_ages[1])):\n",
" names_and_ages[1][i] += 1\n",
"print(names_and_ages)"
]
},
{
Expand Down Expand Up @@ -718,7 +740,11 @@
},
"outputs": [],
"source": [
"# Write for loop here\n"
"# Write for loop here\n",
"for i in range(len(names_and_ages[1])):\n",
" if names_and_ages[0][i] != \"Maggie\": \n",
" names_and_ages[1][i] += 1\n",
"print(names_and_ages)"
]
},
{
Expand Down Expand Up @@ -883,7 +909,9 @@
},
"outputs": [],
"source": [
"#Complete task eleven in the space below\n"
"#Complete task eleven in the space below\n",
"names_and_ages['age_months']=names_and_ages.ages*12\n",
"print(names_and_ages)"
]
},
{
Expand All @@ -906,8 +934,18 @@
"source": [
"# Create new column, fill it with empty values (np.NaN)\n",
"names_and_ages['maturity']=np.NaN\n",
"\n",
"# Write for loop here\n"
"print(names_and_ages['ages'][1])\n",
"\t\n",
"pd.options.mode.chained_assignment = None\n",
"# Write for loop here\n",
"for i in range(len(names_and_ages)):\n",
" if names_and_ages['ages'][i]>=18:\n",
" names_and_ages.at[i, 'maturity'] = \"adult\" \n",
" elif names_and_ages['ages'][i]>=10:\n",
" names_and_ages['maturity'][i]=\"adolescent\"\n",
" else:\n",
" names_and_ages['maturity'].iloc[i]=\"child\"\n",
"print(names_and_ages)"
]
},
{
Expand Down Expand Up @@ -1131,16 +1169,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 95,
"metadata": {
"id": "0wlzj7Cb2YBr"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Flow_cms\n",
"Date Time (AST) \n",
"2021-05-24 0.006558\n",
"2021-05-25 0.032139\n",
"2021-05-26 0.043637\n",
"2021-05-27 0.018943\n",
"2021-05-28 0.011835\n",
"... ...\n",
"2021-09-27 NaN\n",
"2021-09-28 NaN\n",
"2021-09-29 NaN\n",
"2021-09-30 NaN\n",
"2021-10-01 NaN\n",
"\n",
"[131 rows x 1 columns]\n",
"0.000109842165625 0.6283447772812499 0.04407668264820562\n"
]
}
],
"source": [
"#use pandas.DataFrame functions to assign appropriate values to the variables here.\n",
"min_dis =\n",
"max_dis =\n",
"mean_dis =\n",
"# Getting rid off the non numeric values\n",
"mystream = pd.DataFrame()\n",
"mystream[\"Date Time (AST)\"] = pd.to_datetime(stream[\"Date Time (AST)\"])\n",
"mystream[\"Flow_cms\"] = stream[\"Flow_cms\"]\n",
"\n",
"# Resampling at daily (using mean but should we sum()?)\n",
"mystream_daily = mystream.resample(\"1d\", on=\"Date Time (AST)\").mean()\n",
"print(mystream_daily)\n",
"\n",
"min_dis = mystream_daily[\"Flow_cms\"].min()\n",
"max_dis = mystream_daily[\"Flow_cms\"].max()\n",
"mean_dis = mystream_daily[\"Flow_cms\"].mean()\n",
"print(min_dis, max_dis, mean_dis)"
]
},
Expand All @@ -1155,13 +1225,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 103,
"metadata": {
"id": "T37eb3cW2YBs"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Flow_cms\n",
"Date Time (AST) \n",
"2021-12-31 4.530374e+07\n"
]
}
],
"source": [
"#enter calculations here\n"
"#enter calculations here\n",
"# Not putting into a column. Would need to check the units.\n",
"Discharge_total = mystream.resample(\"1y\", on=\"Date Time (AST)\").sum()*24*3600\n",
"print(Discharge_total)"
]
},
{
Expand All @@ -1178,14 +1261,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 100,
"metadata": {
"id": "K8HNETRl2YBs"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.6283447772812499\n",
" Flow_cms\n",
"Date Time (AST) \n",
"2021-06-04 0.628345\n"
]
}
],
"source": [
"#Type your answer here, see if you can get it in one line of code!\n",
"\n"
"print(max_dis)\n",
"print(mystream_daily.loc[mystream_daily.index == mystream_daily[\"Flow_cms\"].idxmax()])"
]
},
{
Expand All @@ -1202,6 +1297,13 @@
"5) git commit -m \"Completed code sprint\"\n",
"6) git push"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading