diff --git a/lectures/Day_3.ipynb b/lectures/Day_3.ipynb
index b024b94..438023b 100644
--- a/lectures/Day_3.ipynb
+++ b/lectures/Day_3.ipynb
@@ -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",
@@ -104,7 +104,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
}
},
"source": [
@@ -129,7 +129,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "slideshow": {
+ "slide_type": "fragment"
+ }
+ },
"outputs": [],
"source": [
"myList[2] = 4\n",
@@ -139,7 +143,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "slideshow": {
+ "slide_type": "fragment"
+ }
+ },
"outputs": [],
"source": [
"myTuple[2] = 4"
@@ -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",
" \n",
"\n",
- "- Canvas -> Modules -> Day 3 -> IMDb exercise -> 1 \n",
+ "- Canvas -> Modules -> Day 3 -> IMDb exercise - day 3 -> 1 \n",
"\n",
"\n",
"\n",
@@ -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",
"
\n",
" "
@@ -946,6 +954,13 @@
" 'biography': 25}"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
{
"cell_type": "markdown",
"metadata": {
@@ -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",
@@ -1116,7 +1131,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
- "scrolled": true,
"slideshow": {
"slide_type": "skip"
}
@@ -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",
@@ -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",
@@ -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": {
@@ -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",
diff --git a/lectures/Day_3.slides.embedded.html b/lectures/Day_3.slides.embedded.html
index e1680fc..62574d7 100644
--- a/lectures/Day_3.slides.embedded.html
+++ b/lectures/Day_3.slides.embedded.html
@@ -14704,13 +14704,13 @@
1. Which of the following variables are of the type tuple?
+
1. Which of the following variables are of the type tuple?
a = (1, 2, 3, 4)
a = ([1, 2], 'a', 'b')
250.imdb
¶250.imdb
¶dictionary
¶set
), while the values associated with keys can be of any data type and can be duplicated
+
# Calculate the average duration of movies in the genre 'drama' genre = "drama" average = sum(genreDict[genre])/len(genreDict[genre]) # calculate average length per genre -hours = int(average/3600) # format seconds to hours +hours = int(average/3600) # format seconds to hours +minutes = (average - (3600*hours))/60 # format seconds to minutes +reformattedTime = str(hours)+'h'+str(round(minutes))+'min' +print('The average length for movies in genre '+ genre +\ + ' is '+ reformattedTime) +
# Calculate the average duration of movies in the genre 'drama'
+genre = "drama"
+average = sum(genreDict[genre])/len(genreDict[genre]) # calculate average length per genre
+hours = int(average/3600) # format seconds to hours
minutes = (average - (3600*hours))/60 # format seconds to minutes
reformattedTime = str(hours)+'h'+str(round(minutes))+'min'
print('The average length for movies in genre '+ genre +\
@@ -15999,7 +16045,7 @@ How to use it
-Try out sys.argv
¶
Python script is called print_argv.py
and can be found in the downloads folder
+Try out sys.argv
(~5 min)¶
Python script is called print_argv.py
and can be found in the downloads folder
Run the following commands in the terminal¶
python print_argv.py
python print_argv.py 1
python print_argv.py arg1 arg2 arg3
@@ -16014,6 +16060,40 @@ Run the following commands i
+If you don't have the file print_argv.py, copy the following code and save it in a file called print_argv.py¶
+
+
+
+
+
+
+
+
+In [ ]:
+
+
+#!/usr/bin/env python
+import sys
+
+print("size of sys.argv = ", len(sys.argv))
+print("program_name = ", sys.argv[0])
+
+counter = 1
+while counter < len(sys.argv):
+ print("arg" + str(counter) + " = ", sys.argv[counter])
+ counter += 1
+
+
+
+
+
+
+
+
+
+
+
+
Naive code to copy a text file¶
@@ -16343,7 +16423,7 @@ Summary¶
Day 3, Exercise 4 (~30 min)¶
#### Restructure and write the output to a new file
-Canvas -> Modules -> Day 3 -> IMDb exercise -> 4
+Canvas -> Modules -> Day 3 -> IMDb exercise - day 3 -> 4
- Work in pairs
diff --git a/lectures/Day_3.slides.html b/lectures/Day_3.slides.html
index 7b502b7..52fe855 100644
--- a/lectures/Day_3.slides.html
+++ b/lectures/Day_3.slides.html
@@ -14721,14 +14721,14 @@ Quiz: Review Day 2
-
250.imdb
¶250.imdb
¶dictionary
¶set
), while the values associated with keys can be of any data type and can be duplicated
+
# Calculate the average duration of movies in the genre 'drama' genre = "drama" average = sum(genreDict[genre])/len(genreDict[genre]) # calculate average length per genre -hours = int(average/3600) # format seconds to hours +hours = int(average/3600) # format seconds to hours +minutes = (average - (3600*hours))/60 # format seconds to minutes +reformattedTime = str(hours)+'h'+str(round(minutes))+'min' +print('The average length for movies in genre '+ genre +\ + ' is '+ reformattedTime) +
# Calculate the average duration of movies in the genre 'drama'
+genre = "drama"
+average = sum(genreDict[genre])/len(genreDict[genre]) # calculate average length per genre
+hours = int(average/3600) # format seconds to hours
minutes = (average - (3600*hours))/60 # format seconds to minutes
reformattedTime = str(hours)+'h'+str(round(minutes))+'min'
print('The average length for movies in genre '+ genre +\
@@ -16117,7 +16167,7 @@ How to use it
-Try out sys.argv
¶
Python script is called print_argv.py
and can be found in the downloads folder
+Try out sys.argv
(~5 min)¶
Python script is called print_argv.py
and can be found in the downloads folder
Run the following commands in the terminal¶
python print_argv.py
python print_argv.py 1
python print_argv.py arg1 arg2 arg3
@@ -16126,6 +16176,42 @@ Run the following commands i
+
#!/usr/bin/env python
+import sys
+
+print("size of sys.argv = ", len(sys.argv))
+print("program_name = ", sys.argv[0])
+
+counter = 1
+while counter < len(sys.argv):
+ print("arg" + str(counter) + " = ", sys.argv[counter])
+ counter += 1
+
#### Restructure and write the output to a new file
Canvas -> Modules -> Day 3 -> IMDb exercise -> 4
+Canvas -> Modules -> Day 3 -> IMDb exercise - day 3 -> 4