Skip to content

Commit

Permalink
update slides for Day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjiangshu committed Nov 12, 2024
1 parent b7474fc commit 8497b36
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 34 deletions.
98 changes: 86 additions & 12 deletions lectures/Day_3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
}
},
"source": [
"## Tuples (Q 1&2)\n",
"### Tuples (Q 1&2)\n",
"\n",
"__1. Which of the following variables are of the type tuple?__ \n",
"`a = (1, 2, 3, 4)` \n",
Expand All @@ -104,7 +104,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
"slide_type": "slide"
}
},
"source": [
Expand All @@ -129,7 +129,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"myList[2] = 4\n",
Expand All @@ -139,7 +143,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"myTuple[2] = 4"
Expand Down Expand Up @@ -721,12 +729,12 @@
}
},
"source": [
"## Day 3, Exercise 1 (~20 min)\n",
"## Day 3, Exercise 1 (~30 min)\n",
"### Find the number of unique genres in the file `250.imdb`\n",
"\n",
"<img src=\"img/header_imdb.png\" alt=\"Drawing\" style=\"width: 1000px;\"/> \n",
"\n",
"- Canvas -> Modules -> Day 3 -> IMDb exercise -> 1 \n",
"- Canvas -> Modules -> Day 3 -> IMDb exercise - day 3 -> 1 \n",
"\n",
"\n",
"\n",
Expand Down Expand Up @@ -815,7 +823,7 @@
"\n",
"- A dictionary is an unordered, mutable collection of key-value pairs. \n",
"- Dictionaries are mutable\n",
"- Each key in a dictionary must be unique and immutable, while the values associated with keys can be of any data type and can be duplicated\n",
"- Each key in a dictionary must be unique and immutable (the same as `set`), while the values associated with keys can be of any data type and can be duplicated\n",
"\n",
"<br>\n",
"<img src=\"img/key_values.png\" alt=\"Drawing\" style=\"width: 1000px;\"/> "
Expand Down Expand Up @@ -946,6 +954,13 @@
" 'biography': 25}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -1003,7 +1018,7 @@
"- #### (Extra) What is the average length of the movies (in hours and minutes) in each genre?\n",
"\n",
"\n",
"- Canvas -> Modules -> Day 3 -> IMDb exercise -> 2&3 \n",
"- Canvas -> Modules -> Day 3 -> IMDb exercise - day 3 -> 2&3 \n",
"___\n",
"#### Take a break after the exercise (~10 min)\n",
"\n",
Expand Down Expand Up @@ -1116,7 +1131,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "skip"
}
Expand Down Expand Up @@ -1286,7 +1300,34 @@
"# Calculate the average duration of movies in the genre 'drama'\n",
"genre = \"drama\"\n",
"average = sum(genreDict[genre])/len(genreDict[genre]) # calculate average length per genre\n",
"hours = int(average/3600) # format seconds to hours\n",
"hours = int(average/3600) # format seconds to hours\n",
"minutes = (average - (3600*hours))/60 # format seconds to minutes\n",
"reformattedTime = str(hours)+'h'+str(round(minutes))+'min'\n",
"print('The average length for movies in genre '+ genre +\\\n",
" ' is '+ reformattedTime)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Now let convert this code into a function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Calculate the average duration of movies in the genre 'drama'\n",
"genre = \"drama\"\n",
"average = sum(genreDict[genre])/len(genreDict[genre]) # calculate average length per genre\n",
"hours = int(average/3600) # format seconds to hours\n",
"minutes = (average - (3600*hours))/60 # format seconds to minutes\n",
"reformattedTime = str(hours)+'h'+str(round(minutes))+'min'\n",
"print('The average length for movies in genre '+ genre +\\\n",
Expand Down Expand Up @@ -1753,7 +1794,7 @@
}
},
"source": [
"### Try out `sys.argv`\n",
"### Try out `sys.argv` (~5 min)\n",
"\n",
"Python script is called `print_argv.py` and can be found in the downloads folder\n",
"\n",
Expand All @@ -1765,6 +1806,39 @@
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### If you don't have the file print_argv.py, copy the following code and save it in a file called print_argv.py "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"#!/usr/bin/env python\n",
"import sys\n",
"\n",
"print(\"size of sys.argv = \", len(sys.argv))\n",
"print(\"program_name = \", sys.argv[0])\n",
"\n",
"counter = 1\n",
"while counter < len(sys.argv):\n",
" print(\"arg\" + str(counter) + \" = \", sys.argv[counter])\n",
" counter += 1"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -2082,7 +2156,7 @@
"### Day 3, Exercise 4 (~30 min)\n",
"- #### Restructure and write the output to a new file\n",
"\n",
"- Canvas -> Modules -> Day 3 -> IMDb exercise -> 4\n",
"- Canvas -> Modules -> Day 3 -> IMDb exercise - day 3 -> 4\n",
"- Work in pairs\n",
"\n",
"#### PyQuiz 3.2 \n",
Expand Down
102 changes: 91 additions & 11 deletions lectures/Day_3.slides.embedded.html

Large diffs are not rendered by default.

Loading

0 comments on commit 8497b36

Please sign in to comment.