Skip to content

Commit 9c25e96

Browse files
committed
Added files for Intermediate Python Session 2
1 parent 351dfea commit 9c25e96

2 files changed

Lines changed: 392 additions & 0 deletions

File tree

content/Intermediate.ipynb

Lines changed: 389 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,389 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": "python",
5+
"display_name": "Python (Pyodide)",
6+
"language": "python"
7+
},
8+
"language_info": {
9+
"codemirror_mode": {
10+
"name": "python",
11+
"version": 3
12+
},
13+
"file_extension": ".py",
14+
"mimetype": "text/x-python",
15+
"name": "python",
16+
"nbconvert_exporter": "python",
17+
"pygments_lexer": "ipython3",
18+
"version": "3.8"
19+
}
20+
},
21+
"nbformat_minor": 5,
22+
"nbformat": 4,
23+
"cells": [
24+
{
25+
"id": "a53d547f-0175-406b-8c7c-2ba207876bcd",
26+
"cell_type": "markdown",
27+
"source": "# Libraries & Packages & Modules, Oh My!",
28+
"metadata": {}
29+
},
30+
{
31+
"id": "af194e4b-3dbf-4df9-be89-ce23f7455180",
32+
"cell_type": "code",
33+
"source": "import numpy as np # library\nprint(np.__version__)",
34+
"metadata": {
35+
"trusted": true
36+
},
37+
"outputs": [],
38+
"execution_count": null
39+
},
40+
{
41+
"id": "299260d8-3f17-4c1b-97dd-bcb582679262",
42+
"cell_type": "code",
43+
"source": "import random\nprint(random.randint(1, 10))\n\nfrom random import randint\nrandint(1, 10) # can now use randint without the qualifier in front",
44+
"metadata": {
45+
"trusted": true
46+
},
47+
"outputs": [],
48+
"execution_count": null
49+
},
50+
{
51+
"id": "fb09784d-42b3-4deb-b738-852dd690f8a3",
52+
"cell_type": "markdown",
53+
"source": "# Functions",
54+
"metadata": {}
55+
},
56+
{
57+
"id": "d7acb707-8214-46d5-a7e6-6ed2452cbb89",
58+
"cell_type": "code",
59+
"source": "def hello():\n\tprint('Hello, World!')\n\nhello() # this will print out the “Hello, World!” string",
60+
"metadata": {
61+
"trusted": true
62+
},
63+
"outputs": [],
64+
"execution_count": null
65+
},
66+
{
67+
"id": "55030f60-e61f-4843-90fc-e7b8e3990898",
68+
"cell_type": "code",
69+
"source": "def make_name(given_name, surname): # these are “positional” parameters\n\treturn f'{given_name} {surname}'\n\nprint(f'Hello, {make_name(\"Gregg\", \"Archer\")}!')",
70+
"metadata": {
71+
"trusted": true
72+
},
73+
"outputs": [],
74+
"execution_count": null
75+
},
76+
{
77+
"id": "2daf8d1f-ee39-4c0c-9e47-972dbdb49677",
78+
"cell_type": "code",
79+
"source": "def make_name(given_name='Bob', surname=None): # specify a default or None\n\treturn f'{given_name} {surname}'\n \nprint(f'Hello, {make_name(surname=\"Archer\", given_name=\"Gregg\")}!') # can be any order",
80+
"metadata": {
81+
"trusted": true
82+
},
83+
"outputs": [],
84+
"execution_count": null
85+
},
86+
{
87+
"id": "5c81b3c6-73ba-4362-a2cb-0a3c437ffe4b",
88+
"cell_type": "code",
89+
"source": "def make_name(given_name: str, surname: str) -> str: # enforce strings coming in and going out\n\treturn f'{given_name} {surname}'\n \nprint(f'Hello, {make_name(\"Gregg\", \"Archer\")}!')",
90+
"metadata": {
91+
"trusted": true
92+
},
93+
"outputs": [],
94+
"execution_count": null
95+
},
96+
{
97+
"id": "3b29b345-96df-405f-851d-b179a26f2ea9",
98+
"cell_type": "markdown",
99+
"source": "# Tuples",
100+
"metadata": {}
101+
},
102+
{
103+
"id": "409ea8ee-a2e7-4e6f-af77-178d8be343d9",
104+
"cell_type": "code",
105+
"source": "picture_size = (8, 10)\npicture_size",
106+
"metadata": {
107+
"trusted": true
108+
},
109+
"outputs": [],
110+
"execution_count": null
111+
},
112+
{
113+
"id": "8a5ef89c-34d8-4eff-8584-38b3eae93f09",
114+
"cell_type": "code",
115+
"source": "image_attributes = ('photo', 8, 10, 'color')\nimage_attributes",
116+
"metadata": {
117+
"trusted": true
118+
},
119+
"outputs": [],
120+
"execution_count": null
121+
},
122+
{
123+
"id": "2e30defb-d29c-4048-ac86-cb5507f39795",
124+
"cell_type": "code",
125+
"source": "image_attributes[1]",
126+
"metadata": {
127+
"trusted": true
128+
},
129+
"outputs": [],
130+
"execution_count": null
131+
},
132+
{
133+
"id": "4b24bfa3-55ef-4396-b016-613e27aee334",
134+
"cell_type": "code",
135+
"source": "(foo, bar, _, _) = image_attributes\nfoo, bar",
136+
"metadata": {
137+
"trusted": true
138+
},
139+
"outputs": [],
140+
"execution_count": null
141+
},
142+
{
143+
"id": "33bd413f-70b5-4d25-95f7-e86745170ee7",
144+
"cell_type": "markdown",
145+
"source": "# Dictionaries",
146+
"metadata": {}
147+
},
148+
{
149+
"id": "8883007d-6faa-4512-ad5b-bfc606d5980e",
150+
"cell_type": "code",
151+
"source": "picture = {\n 'pic_type': 'photo',\n 'width': 8,\n 'height': 10,\n 'color_type': 'color'\n}\n\nprint(picture['pic_type'])\nprint(picture['height'])",
152+
"metadata": {
153+
"trusted": true
154+
},
155+
"outputs": [],
156+
"execution_count": null
157+
},
158+
{
159+
"id": "fc4cc5fb-a221-4097-a277-d5c812f1c437",
160+
"cell_type": "code",
161+
"source": "for key, value in picture.items(): # loop through all items in the dictionary\n print(f'{key}: {value}')",
162+
"metadata": {
163+
"trusted": true
164+
},
165+
"outputs": [],
166+
"execution_count": null
167+
},
168+
{
169+
"id": "1dcac9d6-fd1a-4c40-a44b-3ed324a96373",
170+
"cell_type": "markdown",
171+
"source": "# Sets",
172+
"metadata": {}
173+
},
174+
{
175+
"id": "7ebeed43-b132-4788-8161-3be77b2bc2d8",
176+
"cell_type": "code",
177+
"source": "colors = {\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\"}\nif \"red\" in colors:\n print(\"It's in there!\")",
178+
"metadata": {
179+
"trusted": true
180+
},
181+
"outputs": [],
182+
"execution_count": null
183+
},
184+
{
185+
"id": "3840fac2-98d1-4188-b798-3ec2c1c259b0",
186+
"cell_type": "markdown",
187+
"source": "# Lists",
188+
"metadata": {}
189+
},
190+
{
191+
"id": "f903b538-20f5-454d-89e5-c4268e548e18",
192+
"cell_type": "code",
193+
"source": "colors = [\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\"]\nprint(colors[0]) # prints ‘red’\ncolors.append(\"purple\") # append at the end\ncolors.insert(0, \"brown\") # insert at the beginning because of the 0 parameter\ncolors",
194+
"metadata": {
195+
"trusted": true
196+
},
197+
"outputs": [],
198+
"execution_count": null
199+
},
200+
{
201+
"id": "9f94413f-0db9-45d7-bc44-8ac02e8338d0",
202+
"cell_type": "code",
203+
"source": "# list comprehension\nshort_colors = [x for x in colors if len(x) <= 4] # just colors with short names\nshort_colors",
204+
"metadata": {
205+
"trusted": true
206+
},
207+
"outputs": [],
208+
"execution_count": null
209+
},
210+
{
211+
"id": "cf4c0cc5-8e39-4010-adc5-b059ca779931",
212+
"cell_type": "markdown",
213+
"source": "# Slices and Indexing",
214+
"metadata": {}
215+
},
216+
{
217+
"id": "78fee427-1e37-4a09-aca5-7fc129e160ed",
218+
"cell_type": "code",
219+
"source": "colors = [\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\"]\nprint(colors[0:2]) # prints [‘red’, ‘orange’] & excludes the top index\nprint(colors[:2]) # same as above\nprint(colors[5:]) # prints [‘indigo’, ‘violet’]\nprint(colors[-1]) # prints violet because it counts from the end - note that this is not a list",
220+
"metadata": {
221+
"trusted": true
222+
},
223+
"outputs": [],
224+
"execution_count": null
225+
},
226+
{
227+
"id": "6a344240-f7d0-4896-9cea-ac8c15a6a305",
228+
"cell_type": "markdown",
229+
"source": "# String Operations",
230+
"metadata": {}
231+
},
232+
{
233+
"id": "2b69732c-a1c7-4b7b-bb2d-87336d1bf0b0",
234+
"cell_type": "code",
235+
"source": "# Characters within strings can be indexed\nname = 'Bob'\nprint(name[2]) # prints 'b'",
236+
"metadata": {
237+
"trusted": true
238+
},
239+
"outputs": [],
240+
"execution_count": null
241+
},
242+
{
243+
"id": "e9ceaf86-db09-4f3f-bdb9-c5cd6992d15f",
244+
"cell_type": "code",
245+
"source": "# Slicing works, too\nname = 'Bob'\nprint(name[0:2]) # prints ‘Bo’ – excludes top number\nprint(name[:1]) # prints ‘B’ - excludes top number\nprint(name[-1]) # prints ‘b’",
246+
"metadata": {
247+
"trusted": true
248+
},
249+
"outputs": [],
250+
"execution_count": null
251+
},
252+
{
253+
"id": "55f53f2d-c871-40d8-846f-c5e15b3ba609",
254+
"cell_type": "code",
255+
"source": "# A few other useful string functions and operations\nname = 'This is an example'\nprint(name.lower()) # to lowercase\nprint(name.upper()) # to uppercase\nprint(name.replace(' ','-')) # replace spaces with hyphens\nprint(name.split(' ')) # create a list of the words\nprint(name + '!') # concatenation\nprint(\"1234\".isdigit()) # True because all characters are digits",
256+
"metadata": {
257+
"trusted": true
258+
},
259+
"outputs": [],
260+
"execution_count": null
261+
},
262+
{
263+
"id": "a354b56f-f47b-406e-ac50-6c3812e1ee6f",
264+
"cell_type": "markdown",
265+
"source": "# File Input/Output",
266+
"metadata": {}
267+
},
268+
{
269+
"id": "28c08c68-d82c-4f54-85ed-34758b5dec7f",
270+
"cell_type": "code",
271+
"source": "with open(\"myfile.txt\", \"w\") as f: # use ‘a’ as 2nd param to append or ‘w’ to overwrite\n\tf.write(\"This will be written into the file\")\n# file is automatically closed when the ‘with’ block ends (due to outdenting)\n\nwith open(\"myfile.txt\", \"r\") as f: # ‘r’ to read, which is the default if missing\n\ttext = f.readline() # first line, or use f.read() to read entire file into 1 string\n\tprint(text)",
272+
"metadata": {
273+
"trusted": true
274+
},
275+
"outputs": [],
276+
"execution_count": null
277+
},
278+
{
279+
"id": "b86a4309-b6a5-45ee-b360-7e9e350203b5",
280+
"cell_type": "markdown",
281+
"source": "# Error Handling",
282+
"metadata": {}
283+
},
284+
{
285+
"id": "92f86490-079d-4b6f-bf5d-e58f2d1ab909",
286+
"cell_type": "code",
287+
"source": "import sys\ntry: # the code that might fail goes in the try section\n a = 5\n b = 0\n c = a / b\nexcept: # this section executed when the problem occurs\n print(f'Exception occurred: {sys.exception()}')\nelse: # if no problem, then the ‘else’ section is executed\n print('Everything is fine')\nfinally: # the ‘finally’ block is always executed at the end\n print('All done with the exception handling')",
288+
"metadata": {
289+
"trusted": true
290+
},
291+
"outputs": [],
292+
"execution_count": null
293+
},
294+
{
295+
"id": "18e89a3e-452f-456e-b666-ea46f88d8d45",
296+
"cell_type": "markdown",
297+
"source": "# Exercises",
298+
"metadata": {}
299+
},
300+
{
301+
"id": "5b4c0d77-11bd-4cf1-83e2-551fddd020fd",
302+
"cell_type": "markdown",
303+
"source": "## Exercise 1\nWrite a script that does the following:\n1. Create a function called reverse()\n2. Have it accept a keyword parameter called 'string'\n3. Have it return the parameter string in reverse order.\n4. Call the function, passing in 'forward' and getting back a return of 'drawrof'\n5. Print the 2nd through 4th characters of the returned string. This should be 'raw'.\n\n### Hints:\n1. You may use the len(string_value) to get the length of a string",
304+
"metadata": {}
305+
},
306+
{
307+
"id": "c13ec8bd-9cba-4070-bf78-1e73b39549a9",
308+
"cell_type": "code",
309+
"source": "# PUT YOUR SOLUTION HERE\n",
310+
"metadata": {
311+
"trusted": true
312+
},
313+
"outputs": [],
314+
"execution_count": null
315+
},
316+
{
317+
"id": "9ac1016a-6ec4-41d1-8bda-78f8d0b1781d",
318+
"cell_type": "code",
319+
"source": "",
320+
"metadata": {
321+
"trusted": true
322+
},
323+
"outputs": [],
324+
"execution_count": null
325+
},
326+
{
327+
"id": "d9c4d107-7b3d-412b-9c56-c7e9bca81e65",
328+
"cell_type": "markdown",
329+
"source": "### Possible solution to Exercise 1\nPlease don't unhide the cell below until you are ready to view the solution.",
330+
"metadata": {
331+
"jp-MarkdownHeadingCollapsed": true
332+
}
333+
},
334+
{
335+
"id": "410a842e-22b3-46b2-b5c7-8050c6773a87",
336+
"cell_type": "code",
337+
"source": "def reverse(string=None):\n result = ''\n i = len(string) - 1 # remember to subtract 1\n while i >= 0:\n result += string[i] # string indexing\n i -= 1\n return result\n\nresult = reverse(string='forward')\nprint(result[1:4]) # the slice returns indexes 1 through 3",
338+
"metadata": {
339+
"trusted": true
340+
},
341+
"outputs": [],
342+
"execution_count": null
343+
},
344+
{
345+
"id": "fed6feb8-43dc-4765-9afe-dd7c0ecbec34",
346+
"cell_type": "markdown",
347+
"source": "## Exercise 2\nRead in a comma-separated file and print out the 2nd and 3rd fields from each line\n1. Open the 'data.csv' file\n2. Read in the lines one-by-one\n3. Catch and handle the exception if a given line doesn't have at least 3 fields in it\n\n### Hints:\n1. When trying to access an index that is out of bounds, you will get an exception called IndexError\n2. The method to use to break up a string with a certain delimiter is called 'split()' and you pass the delimiter as the parameter",
348+
"metadata": {}
349+
},
350+
{
351+
"id": "97f8181f-1a4d-4e48-8be3-3f1245e77cfe",
352+
"cell_type": "code",
353+
"source": "# PUT YOUR SOLUTION HERE\n",
354+
"metadata": {
355+
"trusted": true
356+
},
357+
"outputs": [],
358+
"execution_count": null
359+
},
360+
{
361+
"id": "d310c915-8ae2-4583-b4fb-4abfd02cf755",
362+
"cell_type": "code",
363+
"source": "",
364+
"metadata": {
365+
"trusted": true
366+
},
367+
"outputs": [],
368+
"execution_count": null
369+
},
370+
{
371+
"id": "a74386ba-4be1-469c-9480-aeb1467077d3",
372+
"cell_type": "markdown",
373+
"source": "### Possible solution to Exercise 2\nPlease don't unhide the cell below until you are ready to view the solution.",
374+
"metadata": {
375+
"jp-MarkdownHeadingCollapsed": true
376+
}
377+
},
378+
{
379+
"id": "f13cbc52-0671-4a9a-af93-de5c9be36b4e",
380+
"cell_type": "code",
381+
"source": "with open(\"data.csv\", \"r\") as f:\n for line in f:\n fields = line.split(\",\")\n try:\n # print(fields[1:3]) # could use this, but it doesn't trigger the exception\n print(f'{fields[1]}, {fields[2]}')\n except IndexError:\n print(f\"Not enough fields → {fields}\")",
382+
"metadata": {
383+
"trusted": true
384+
},
385+
"outputs": [],
386+
"execution_count": null
387+
}
388+
]
389+
}

content/data.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1,New York, NY
2+
2,Los Angeles, CA
3+
3,Chicago

0 commit comments

Comments
 (0)