Skip to content

Commit b5e860d

Browse files
committed
Changes
1 parent cadbba4 commit b5e860d

6 files changed

Lines changed: 182 additions & 94 deletions

File tree

‎Week_0_python/d_tutorial_list.ipynb‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@
204204
"# for the c programmer, there is no for (i = 0, i < n, i++) equivalent\n",
205205
"my_list_result = []\n",
206206
"for i in range(0, len(my_list_todos)):\n",
207-
" if my_list_todos[i] is \"add\":\n",
207+
" if my_list_todos[i] == \"add\":\n",
208208
" my_list_result.append(my_list_lhs_numbers[i] + my_list_rhs_numbers[i])\n",
209-
" elif my_list_todos[i] is \"multiply\":\n",
209+
" elif my_list_todos[i] == \"multiply\":\n",
210210
" my_list_result.append(my_list_lhs_numbers[i] * my_list_rhs_numbers[i])\n",
211-
" elif my_list_todos[i] is \"skip\":\n",
211+
" elif my_list_todos[i] == \"skip\":\n",
212212
" pass \n",
213213
" else:\n",
214214
" # This is the RIGHT way to do this, so you catch if someone mis-typed something\n",
@@ -244,11 +244,11 @@
244244
"\n",
245245
"# Notice i, n\n",
246246
"for i, n in enumerate(my_list_todos):\n",
247-
" if n is \"add\":\n",
247+
" if n == \"add\":\n",
248248
" my_list_result.append(my_list_lhs_numbers[i] + my_list_rhs_numbers[i])\n",
249-
" elif n is \"multiply\":\n",
249+
" elif n == \"multiply\":\n",
250250
" my_list_result.append(my_list_lhs_numbers[i] * my_list_rhs_numbers[i])\n",
251-
" elif n is \"skip\":\n",
251+
" elif n == \"skip\":\n",
252252
" pass # This is the RIGHT way to do this, so you catch if someone mis-typed something\n",
253253
" else:\n",
254254
" raise ValueError(f\"Expected add, multiply, or skip, got {n}\")\n",
@@ -266,11 +266,11 @@
266266
"# Note also that this does NOT require an index variable i - it's all handled for you\n",
267267
"my_list_result = [] # Set list back to empty\n",
268268
"for n, lhs, rhs in zip(my_list_todos, my_list_lhs_numbers, my_list_rhs_numbers):\n",
269-
" if n is \"add\":\n",
269+
" if n == \"add\":\n",
270270
" my_list_result.append(lhs + rhs)\n",
271-
" elif n is \"multiply\":\n",
271+
" elif n == \"multiply\":\n",
272272
" my_list_result.append(lhs * rhs)\n",
273-
" elif n is \"skip\":\n",
273+
" elif n == \"skip\":\n",
274274
" pass \n",
275275
" else:\n",
276276
" # This is the RIGHT way to do this, so you catch if someone mis-typed something\n",

‎Week_1_data_structures/Lecture_data_structures.ipynb‎

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,138 @@
224224
"print(my_dict)"
225225
]
226226
},
227+
{
228+
"cell_type": "markdown",
229+
"metadata": {},
230+
"source": [
231+
"## Big picture\n",
232+
"\n",
233+
"Data structures are variables with more \"stuff\" in them than a single number or string. That way you don't have to label every single value you store. To get specific items out, it's always square brackets.\n",
234+
"\n",
235+
"var_name[stuff here]\n"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": 6,
241+
"metadata": {},
242+
"outputs": [],
243+
"source": [
244+
"# From Intro_assignment.ipynb, dictionary probelm\n",
245+
"# Creating one variable for each value you want to store\n",
246+
"\n",
247+
"name = \"Me\"\n",
248+
"age = 10\n",
249+
"first_grade = \"A\"\n",
250+
"second_grade = \"A-\"\n",
251+
"third_grade = \"B+\""
252+
]
253+
},
254+
{
255+
"cell_type": "code",
256+
"execution_count": null,
257+
"metadata": {},
258+
"outputs": [],
259+
"source": [
260+
"# Grades are a good candidate for a list - they're already numbered\n",
261+
"grades = [\"A\", \"A-\", \"B+\"]\n",
262+
"\n",
263+
"# Or you could do - it's the same as above\n",
264+
"grades = [first_grade, second_grade, third_grade]\n",
265+
"\n",
266+
"# Notice the [] in the print statement, and the 'A' instead of A\n",
267+
"print(f\"Grades (individual variables) {first_grade}, {second_grade}, {third_grade}\")\n",
268+
"print(f\"Grades (list) {grades}\")\n",
269+
"\n"
270+
]
271+
},
272+
{
273+
"cell_type": "code",
274+
"execution_count": null,
275+
"metadata": {},
276+
"outputs": [],
277+
"source": [
278+
"# Edit the variables\n",
279+
"# Make the first grade be an A+\n",
280+
"first_grade = first_grade + \"+\"\n",
281+
"\n",
282+
"# Do the same thing for the list - edit the first element (notice the [0])\n",
283+
"grades[0] = grades[0] + \"+\"\n",
284+
"\n",
285+
"# Re-create the list from the variables - remember that first_grade has changed\n",
286+
"grades_same_thing = [first_grade, second_grade, third_grade]\n",
287+
"\n",
288+
"# Notice that all the A's were changed to A+'s\n",
289+
"print(f\"Grades (individual variables) {first_grade}, {second_grade}, {third_grade}\")\n",
290+
"print(f\"Grades (list) {grades}\")\n"
291+
]
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": null,
296+
"metadata": {},
297+
"outputs": [],
298+
"source": [
299+
"# Adding another variable\n",
300+
"\n",
301+
"# If you're keeping individual variables, you have to make another variable\n",
302+
"fourth_grade = \"B\"\n",
303+
"\n",
304+
"# If you have a list, just append to the list\n",
305+
"grades.append(\"B\")\n",
306+
"\n",
307+
"# Notice that we have to add another {fourth_grade}\n",
308+
"print(f\"Grades (individual variables) {first_grade}, {second_grade}, {third_grade}, {fourth_grade}\")\n",
309+
"# This just works\n",
310+
"print(f\"Grades (list) {grades}\")\n"
311+
]
312+
},
227313
{
228314
"cell_type": "code",
229315
"execution_count": null,
230316
"metadata": {},
231317
"outputs": [],
232-
"source": []
318+
"source": [
319+
"# Now let's do the same thing for the dictionary\n",
320+
"# All three of these do the same thing, btw\n",
321+
"# Make the whole list using key:value, key:value, all in {}\n",
322+
"my_grade_stuff = {\"name\": \"Me\", \"age\": 10, \"grades\": [\"A\", \"A-\", \"B+\"]}\n",
323+
"print(f\"Grades version 1 {my_grade_stuff}\")\n",
324+
"\n",
325+
"# The same thing, but this time use the variables as the values\n",
326+
"# TODO: Why is this slightly different than the one above? Reminder that\n",
327+
"# we edited grades to have 4 grades...\n",
328+
"my_grade_stuff = {\"name\": name, \"age\": age, \"grades\": grades}\n",
329+
"print(f\"Grades version 2 {my_grade_stuff}\")\n",
330+
"\n",
331+
"# One more time, this time making an empty dictionary and adding stuff \n",
332+
"# in one at a time\n",
333+
"my_grade_stuff = {}\n",
334+
" # Notice that the [\"foo\"] is on the left-hand side - this *makes*\n",
335+
" # the key, value pair in the dictionary, instead of *getting* it\n",
336+
"my_grade_stuff[\"name\"] = name\n",
337+
"my_grade_stuff[\"age\"] = age\n",
338+
"my_grade_stuff[\"grades\"] = grades\n",
339+
"print(f\"Grades version 3 {my_grade_stuff}\")\n"
340+
]
341+
},
342+
{
343+
"cell_type": "code",
344+
"execution_count": 13,
345+
"metadata": {},
346+
"outputs": [],
347+
"source": [
348+
"# Now do an edit of the age\n",
349+
"# These do the same thing - but one edits the age variable, one\n",
350+
"# edits the age value in with the key \"age\"\n",
351+
"age = age + 3 # Add 3 to the age variable\n",
352+
"my_grade_stuff[\"age\"] = my_grade_stuff[\"age\"] + 3\n",
353+
"\n",
354+
"# You could do this, if it's helpful to think of it this way\n",
355+
"my_current_age = my_grade_stuff[\"age\"] # Get the current age\n",
356+
"my_current_age = my_current_age + 3\n",
357+
"my_grade_stuff[\"age\"] = my_current_age"
358+
]
233359
}
234360
],
235361
"metadata": {
@@ -248,7 +374,7 @@
248374
"name": "python",
249375
"nbconvert_exporter": "python",
250376
"pygments_lexer": "ipython3",
251-
"version": "3.7.4"
377+
"version": "3.12.7"
252378
}
253379
},
254380
"nbformat": 4,

‎Week_1_data_structures/lec_act_1_data_structures.ipynb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"\n",
9999
"# You might find numpy's count_nonzero useful here \n",
100100
"\n",
101-
"np.countnonzero(my_example < 0)\n",
101+
"\n",
102102
"# TODO Make a 100 x 200 array of numbers between 0 and 1 using the example above (numpy.random.random function)\n",
103103
"my_made_up_np_array = ...\n",
104104
"# TODO Use arithmatic to change numbers in the range 0 to 1 to be in the range -1 to 1 instead\n",
@@ -344,7 +344,7 @@
344344
"source": [
345345
"# Data for this problem - this takes the list and converts it to a numpy array\n",
346346
"# Use the variable window (click on variables above) to see the difference between test_list_one and\n",
347-
"# test_nparray - they should have the same values, they're just stored differently\n",
347+
"# test_nparra - they should have the same values, they're just stored differently\n",
348348
"test_nparray = np.array(test_list_one)"
349349
]
350350
},

‎Week_2_functions/Lecture_functions.ipynb‎

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
" @param b is a number\n",
2222
" @return a-b (a number)\"\"\"\n",
2323
" ret_val = a - b\n",
24-
" return ret_val"
24+
" return ret_val\n",
25+
"\n"
2526
]
2627
},
2728
{
@@ -44,7 +45,7 @@
4445
"# Calling the function with variables\n",
4546
"a_here = 10.2\n",
4647
"b_here = 12.2\n",
47-
"ret_here = my_func1(a_here, b_here)\n",
48+
"ret_here = my_func1(a_here / 3, b_here)\n",
4849
"print(f\"return value for a_here - b_here is {ret_here}\")"
4950
]
5051
},
@@ -72,6 +73,18 @@
7273
"print(f\"Incorrect for a_here - b_here {ret_here_wrong}\")"
7374
]
7475
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"# Fix for that - label the input variables \n",
83+
"ret_here_correct = my_func1(b=b_here, a=a_here)\n",
84+
"\n",
85+
"ret_foo = my_func1(a=7, b=\"hi\")"
86+
]
87+
},
7588
{
7689
"cell_type": "markdown",
7790
"metadata": {},
@@ -84,7 +97,7 @@
8497
},
8598
{
8699
"cell_type": "code",
87-
"execution_count": null,
100+
"execution_count": 14,
88101
"metadata": {},
89102
"outputs": [],
90103
"source": [
@@ -161,67 +174,6 @@
161174
"print(f\"All numbers with min {np.min(np.sin(t_values))}\")"
162175
]
163176
},
164-
{
165-
"cell_type": "markdown",
166-
"metadata": {},
167-
"source": [
168-
"# Example 2: plot functions\n",
169-
"\n",
170-
"Plot is a very, very versitile function - it can plot a lot of things and has a lot of \"default\" behavior. It also has a *lot* of parameters that can be set in order to change what happens"
171-
]
172-
},
173-
{
174-
"cell_type": "code",
175-
"execution_count": null,
176-
"metadata": {},
177-
"outputs": [],
178-
"source": [
179-
"import matplotlib.pyplot as plt\n",
180-
"\n",
181-
"# This \"magic\" command tells matplotlib to make in-line images of plots.\n",
182-
"%matplotlib inline"
183-
]
184-
},
185-
{
186-
"cell_type": "code",
187-
"execution_count": null,
188-
"metadata": {},
189-
"outputs": [],
190-
"source": [
191-
"# Give it one list of values and it will plot that with 1,2,3 etc on the x axis\n",
192-
"plt.plot(np.sin(t_values))"
193-
]
194-
},
195-
{
196-
"cell_type": "code",
197-
"execution_count": null,
198-
"metadata": {},
199-
"outputs": [],
200-
"source": [
201-
"#. Or set x and y - notice the x-axis has different values\n",
202-
"plt.plot(t_values, np.sin(t_values))"
203-
]
204-
},
205-
{
206-
"cell_type": "code",
207-
"execution_count": null,
208-
"metadata": {},
209-
"outputs": [],
210-
"source": [
211-
"#. Adjust color and line style - the string is a very concise shorthand for - line style, x markers, rec\n",
212-
"plt.plot(t_values, np.sin(t_values), \"-xr\")"
213-
]
214-
},
215-
{
216-
"cell_type": "code",
217-
"execution_count": null,
218-
"metadata": {},
219-
"outputs": [],
220-
"source": [
221-
"#. Adjust color and line style - setting each parameter by hand\n",
222-
"plt.plot(t_values, np.sin(t_values), linestyle='dashed', color='darkred', marker=\"x\", markersize='10')"
223-
]
224-
},
225177
{
226178
"cell_type": "markdown",
227179
"metadata": {},
@@ -239,7 +191,7 @@
239191
},
240192
{
241193
"cell_type": "code",
242-
"execution_count": 2,
194+
"execution_count": 18,
243195
"metadata": {},
244196
"outputs": [],
245197
"source": [
@@ -248,7 +200,7 @@
248200
},
249201
{
250202
"cell_type": "code",
251-
"execution_count": 4,
203+
"execution_count": 19,
252204
"metadata": {},
253205
"outputs": [],
254206
"source": [
@@ -275,7 +227,7 @@
275227
},
276228
{
277229
"cell_type": "code",
278-
"execution_count": 5,
230+
"execution_count": 23,
279231
"metadata": {},
280232
"outputs": [],
281233
"source": [
@@ -288,7 +240,7 @@
288240
},
289241
{
290242
"cell_type": "code",
291-
"execution_count": 7,
243+
"execution_count": 24,
292244
"metadata": {},
293245
"outputs": [],
294246
"source": [
@@ -305,7 +257,7 @@
305257
},
306258
{
307259
"cell_type": "code",
308-
"execution_count": 11,
260+
"execution_count": 25,
309261
"metadata": {},
310262
"outputs": [],
311263
"source": [
@@ -347,7 +299,7 @@
347299
],
348300
"metadata": {
349301
"kernelspec": {
350-
"display_name": "Python 3",
302+
"display_name": "base",
351303
"language": "python",
352304
"name": "python3"
353305
},
@@ -361,7 +313,7 @@
361313
"name": "python",
362314
"nbconvert_exporter": "python",
363315
"pygments_lexer": "ipython3",
364-
"version": "3.11.4"
316+
"version": "3.12.4"
365317
}
366318
},
367319
"nbformat": 4,

0 commit comments

Comments
 (0)