Skip to content

Commit 2033775

Browse files
committed
updates to assignments 4-5 and world series winners ... go sox
1 parent f271ffe commit 2033775

5 files changed

Lines changed: 931 additions & 264 deletions

File tree

assignments/answers/assignment3_answers.ipynb

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,62 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"### Answers for Assignment 3 - Python Fundamentals I\n",
7+
"### Answers to Assignment 3 - Python Fundamentals I\n",
88
"\n",
9-
"Complete the tasks below. You should turn in a single Jupyter notebook named `3_first_last.ipynb` (substitute your first and last name); please run Kernel > Restart & Run All on your notebook before turning in. Also turn in the input CSV file used in question B3. For questions where Python scripts are used instead of the Jupyter notebook (questions A1, B1, B2), instructions are provided about pasting output/scripts into Markdown cells in your notebook."
9+
"Complete the tasks below. You should turn in a single Jupyter notebook named `3_first_last.ipynb` (substitute your first and last name); please run Kernel > Restart & Run All on your notebook before turning in. Also turn in the input CSV file used in question B3. For questions where Python scripts are used instead of the Jupyter notebook (questions A1, B1, B2), instructions are provided about pasting output/scripts into Markdown cells in your notebook.\n",
10+
"\n",
11+
"#### A. Python basics, strings, printing (Shaw Exercises 1–10)\n",
12+
"\n",
13+
"1. Take the file of Python code you generated in Exercise 1, called `ex1.py`, and run it on the command line. Copy the command and output from your terminal and paste it in a Markdown cell in your Jupyter notebook. (Ex1)\n",
14+
"2. Print a sentence that contains a phrase in double quotes; print a sentence that contains an apostrophe. (Ex1)\n",
15+
"3. Deliberately enter five incorrect commands (in separate cells) and interpret the error messages (in Markdown cells). (Ex1)\n",
16+
"4. Add comments to your code in \\#2 and \\#3 explaining what is happening. (Ex2)\n",
17+
"5. Write and evaluate five mathematical expressions. (Ex3)\n",
18+
"6. Assign values to three numeric and three string variables. (Ex4)\n",
19+
"7. Print values of the six variables you assigned in \\#6. (Ex4–5)\n",
20+
"8. Print two strings in raw format (%r formatter) and two strings in general/string format (%s formatter) with formatting characters. (Ch6)\n",
21+
"9. Concatenate two strings into a third string, then find the length of all three strings.\n",
22+
"10. Print the three strings from \\#9 with a tab between the first and second and a newline between the second and third.\n",
23+
"11. Print the three strings from \\#9 with a stored formatter. (Ch7–8)\n",
24+
"12. Print a piece of text with five lines using both newline characters and a text block. (Ex9)\n",
25+
"13. Print a string containing a backslash, single-quote, double-qoute, newline, and tab. (Ex10)\n",
26+
"\n",
27+
"#### B. Taking input, reading and writing files, functions (Shaw Exercises 11-26) \n",
28+
"\n",
29+
"1. Write some code, without using functions, that calculates the average of 5 numbers. Do it three different ways:\n",
30+
"\n",
31+
" * Write a .py file that takes input from the command line using `input()`. After the script works, paste the text of the file into your Jupyter notebook.\n",
32+
" * Write a .py file that takes input from the command line using `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
33+
" * Enter code into two Jupyter notebooks cells: the first stores value as variables, and the second computes the average.\n",
34+
"\n",
35+
"2. Using functions, write some code that takes two strings, prints them with the first letter capitalized, prints them with all letters capitalized, prints the first and last letter of each, prints the length of each, and then prints the concatenation of the two strings. Do it two different ways:\n",
36+
"\n",
37+
" * Write a .py file that uses `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
38+
" * In your Jupyter notebook, comment out the `argv` portions and hard code in the values of your strings. Then make sure the code runs the same.\n",
39+
"\n",
40+
"3. Using a text editor, create a comma-separated values (CSV) file with 5 columns and 5 rows. Save it in the same directory as your Jupyter notebook. In the Jupyter notebook, read and print the file in different ways, and write new files, as follows:\n",
41+
"\n",
42+
" * Read your CSV file using `read()`, `readline()`, or `readlines()`, and print the output to the screen (`print()` command is optional in notebooks!).\n",
43+
" * Do the same but use a `with` block and a different one of `read()`, `readline()`, or `readlines()`.\n",
44+
" * Using either of the two above methods to read in the file, then change one row of data (i.e., modify the string or list), then write your csv data to a new file.\n",
45+
" * Read your CSV file using Pandas and display the resulting DataFrame.\n",
46+
" * Save your DataFrame to a new file using Pandas.\n"
1047
]
1148
},
1249
{
1350
"cell_type": "markdown",
1451
"metadata": {},
1552
"source": [
16-
"#### A. Python basics, strings, printing (Shaw Exercises 1–10)\n",
53+
"---"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"#### Answer to A. Python basics, strings, printing (Shaw Exercises 1–10)\n",
1761
"\n",
18-
"**1.** Take the file of Python code you generated in Exercise 1, called `ex1.py`, and run it on the command line. Copy the command and output from your terminal and paste it in a markdown cell in your Jupyter notebook. (Ex1)"
62+
"1. Take the file of Python code you generated in Exercise 1, called `ex1.py`, and run it on the command line. Copy the command and output from your terminal and paste it in a markdown cell in your Jupyter notebook. (Ex1)"
1963
]
2064
},
2165
{
@@ -75,7 +119,7 @@
75119
"cell_type": "markdown",
76120
"metadata": {},
77121
"source": [
78-
"**2.** Print a sentence that contains a phrase in double quotes; print a sentence that contains an apostrophe. (Ex1)"
122+
"2. Print a sentence that contains a phrase in double quotes; print a sentence that contains an apostrophe. (Ex1)"
79123
]
80124
},
81125
{
@@ -110,7 +154,7 @@
110154
"cell_type": "markdown",
111155
"metadata": {},
112156
"source": [
113-
"**3.** Deliberately enter five incorrect commands (in separate cells) and interpret the error messages. (Ex1)"
157+
"3. Deliberately enter five incorrect commands (in separate cells) and interpret the error messages. (Ex1)"
114158
]
115159
},
116160
{
@@ -261,7 +305,7 @@
261305
"cell_type": "markdown",
262306
"metadata": {},
263307
"source": [
264-
"**4.** Add comments to your code in \\#2 and \\#3 explaining what is happening. (Ex2)"
308+
"4. Add comments to your code in \\#2 and \\#3 explaining what is happening. (Ex2)"
265309
]
266310
},
267311
{
@@ -275,7 +319,7 @@
275319
"cell_type": "markdown",
276320
"metadata": {},
277321
"source": [
278-
"**5.** Write and evaluate five mathematical expressions. (Ex3)"
322+
"5. Write and evaluate five mathematical expressions. (Ex3)"
279323
]
280324
},
281325
{
@@ -444,7 +488,7 @@
444488
"cell_type": "markdown",
445489
"metadata": {},
446490
"source": [
447-
"**6.** Assign values to three numeric and three string variables. (Ex4)"
491+
"6. Assign values to three numeric and three string variables. (Ex4)"
448492
]
449493
},
450494
{
@@ -472,7 +516,7 @@
472516
"cell_type": "markdown",
473517
"metadata": {},
474518
"source": [
475-
"**7.** Print values of the six variables you assigned in \\#6. (Ex4–5)"
519+
"7. Print values of the six variables you assigned in \\#6. (Ex4–5)"
476520
]
477521
},
478522
{
@@ -505,7 +549,7 @@
505549
"cell_type": "markdown",
506550
"metadata": {},
507551
"source": [
508-
"**8.** Print two strings in raw format (%r formatter) and two strings in general/string format (%s formatter) with formatting characters. (Ch6)"
552+
"8. Print two strings in raw format (%r formatter) and two strings in general/string format (%s formatter) with formatting characters. (Ch6)"
509553
]
510554
},
511555
{
@@ -538,7 +582,7 @@
538582
"cell_type": "markdown",
539583
"metadata": {},
540584
"source": [
541-
"**9.** Concatenate two strings into a third string, then find the length of all three strings."
585+
"9. Concatenate two strings into a third string, then find the length of all three strings."
542586
]
543587
},
544588
{
@@ -575,7 +619,7 @@
575619
"cell_type": "markdown",
576620
"metadata": {},
577621
"source": [
578-
"**10.** Print the three strings from \\#9 with a tab between the first and second and a newline between the second and third.\n"
622+
"10. Print the three strings from \\#9 with a tab between the first and second and a newline between the second and third.\n"
579623
]
580624
},
581625
{
@@ -607,7 +651,7 @@
607651
"cell_type": "markdown",
608652
"metadata": {},
609653
"source": [
610-
"**11.** Print the three strings from \\#9 with a stored formatter. (Ch7–8)"
654+
"11. Print the three strings from \\#9 with a stored formatter. (Ch7–8)"
611655
]
612656
},
613657
{
@@ -640,7 +684,7 @@
640684
"cell_type": "markdown",
641685
"metadata": {},
642686
"source": [
643-
"**12.** Print a piece of text with five lines using both newline characters and a text block. (Ex9)"
687+
"12. Print a piece of text with five lines using both newline characters and a text block. (Ex9)"
644688
]
645689
},
646690
{
@@ -705,7 +749,7 @@
705749
"cell_type": "markdown",
706750
"metadata": {},
707751
"source": [
708-
"**13.** Print a string containing a backslash, single-quote, double-qoute, newline, and tab. (Ex10)"
752+
"13. Print a string containing a backslash, single-quote, double-qoute, newline, and tab. (Ex10)"
709753
]
710754
},
711755
{
@@ -737,13 +781,13 @@
737781
"cell_type": "markdown",
738782
"metadata": {},
739783
"source": [
740-
"#### B. Taking input, reading and writing files, functions (Shaw Exercises 11-26) \n",
784+
"#### Answer to B. Taking input, reading and writing files, functions (Shaw Exercises 11-26) \n",
741785
"\n",
742-
"**1.** Write some code, without using functions, that calculates the average of 5 numbers. Do it three different ways:\n",
786+
"1. Write some code, without using functions, that calculates the average of 5 numbers. Do it three different ways:\n",
743787
"\n",
744-
"* Write a .py file that takes input from the command line using `input()`. After the script works, paste the text of the file into your Jupyter notebook.\n",
745-
"* Write a .py file that takes input from the command line using `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
746-
"* Enter code into two Jupyter notebooks cells: the first stores value as variables, and the second computes the average."
788+
" * Write a .py file that takes input from the command line using `input()`. After the script works, paste the text of the file into your Jupyter notebook.\n",
789+
" * Write a .py file that takes input from the command line using `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
790+
" * Enter code into two Jupyter notebooks cells: the first stores value as variables, and the second computes the average."
747791
]
748792
},
749793
{
@@ -852,10 +896,10 @@
852896
"cell_type": "markdown",
853897
"metadata": {},
854898
"source": [
855-
"**2.** Using functions, write some code that takes two strings, prints them with the first letter capitalized, prints them with all letters capitalized, prints the first and last letter of each, prints the length of each, and then prints the concatenation of the two strings. Do it two different ways:\n",
899+
"2. Using functions, write some code that takes two strings, prints them with the first letter capitalized, prints them with all letters capitalized, prints the first and last letter of each, prints the length of each, and then prints the concatenation of the two strings. Do it two different ways:\n",
856900
"\n",
857-
"* Write a .py file that uses `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
858-
"* In your Jupyter notebook, comment out the `argv` portions and hard code in the values of your strings. Then make sure the code runs the same."
901+
" * Write a .py file that uses `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
902+
" * In your Jupyter notebook, comment out the `argv` portions and hard code in the values of your strings. Then make sure the code runs the same."
859903
]
860904
},
861905
{
@@ -931,13 +975,13 @@
931975
"cell_type": "markdown",
932976
"metadata": {},
933977
"source": [
934-
"**3.** Using a text editor, create a comma-separated values (CSV) file with 5 columns and 5 rows. Save it in the same directory as your Jupyter notebook. In the Jupyter notebook, read and print the file in different ways, and write new files, as follows:\n",
978+
"3. Using a text editor, create a comma-separated values (CSV) file with 5 columns and 5 rows. Save it in the same directory as your Jupyter notebook. In the Jupyter notebook, read and print the file in different ways, and write new files, as follows:\n",
935979
"\n",
936-
"* Read your CSV file using `read()`, `readline()`, or `readlines()`, and print the output to the screen (`print()` command is optional in notebooks!).\n",
937-
"* Do the same but use a `with` block and a different one of `read()`, `readline()`, or `readlines()`.\n",
938-
"* Using either of the two above methods, then change one row of data, and write your data to a new CSV file.\n",
939-
"* Read your CSV file using Pandas and display the resulting DataFrame.\n",
940-
"* Save your DataFrame to a new file using Pandas."
980+
" * Read your CSV file using `read()`, `readline()`, or `readlines()`, and print the output to the screen (`print()` command is optional in notebooks!).\n",
981+
" * Do the same but use a `with` block and a different one of `read()`, `readline()`, or `readlines()`.\n",
982+
" * Using either of the two above methods, then change one row of data, and write your data to a new CSV file.\n",
983+
" * Read your CSV file using Pandas and display the resulting DataFrame.\n",
984+
" * Save your DataFrame to a new file using Pandas."
941985
]
942986
},
943987
{

assignments/assignment4.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22

33
Complete the tasks below. Please turn in a single Jupyter notebook named `4_first_last.ipynb` (substitute your first and last name). Please run Kernel > Restart & Run All on your notebook before turning in.
44

5-
**1.** Lists and loops.
5+
#### A. Lists and loops
66

7-
* Create a list of 10 random integers in the range of -100 to 100.
8-
* Loop through that list using a for loop. Put the non-negative (positive or zero) integers into a new list. If negative integers are encountered, print a message saying that the value is negative and printing that value.
7+
1. Create a list of 10 random integers in the range of -100 to 100.
8+
2. Loop through that list using a for loop. Put the non-negative (positive or zero) integers into a new list. If negative integers are encountered, print a message saying that the value is negative and printing that value.
99

10-
**2.** Functions, lists, and loops.
10+
#### B. Functions, lists, and loops
1111

12-
* Write a function that does what you did in Question 1 but with the following differences:
12+
1. Write a function that does what you did in part A but with the following differences:
1313
- The input list can be any list of numbers.
1414
- The function should take as a parameter a threshold for being included in the new list (e.g., if you want non-negative integers, the threshold would be 0, and the function would check if values are >= to this value).
1515
- The printed message for failing to be included should also report the threshold parameter.
1616
- The new list should be returned by the function.
1717

18-
**3.** Dictionaries and list enumeration.
18+
#### C. Dictionaries and list enumeration
1919

20-
* Create a two-dimensional list with 3 'rows' and 4 'columns' and a mixture of strings and integers.
21-
* Loop through each element of the list and check if each element is a string or an integer.
22-
* Save the strings as a dictionary with the index (row, column) as the key and the string as the value.
23-
* Save the integers as a dictionary with the index (row, column) as the key and the integer as the value.
20+
1. Create a two-dimensional list with 3 'rows' and 4 'columns' and a mixture of strings and integers.
21+
2. Loop through each element of the list and check if each element is a string or an integer.
22+
3. Save the strings as a dictionary with the index (row, column) as the key and the string as the value.
23+
4. Save the integers as a dictionary with the index (row, column) as the key and the integer as the value.
2424

25-
**4.** List comprehension and saving files.
25+
#### D. List comprehension and saving files
2626

27-
* Create a list of 5 strings that are first and last names, e.g. `'Jon Doe'`.
28-
* Use a list comprehension (a single-line command) to get the first initial from each name and store each string (e.g. `'J'`) in a new list.
29-
* Repeat but store both the first and last initial (e.g. `'JD'`) in a new list.
30-
* Save this second list to a text file.
27+
1. Create a list of 5 strings that are first and last names, e.g. `'Jon Doe'`.
28+
2. Use a list comprehension (a single-line command) to get the first initial from each name and store each string (e.g. `'J'`) in a new list.
29+
3. Repeat but store both the first and last initial (e.g. `'JD'`) in a new list.
30+
4. Save this second list to a text file.
3131

32-
**5.** Reading files, sets, and saving files.
32+
#### E. Reading files, sets, and saving files
3333

34-
* Download this text file of past [World Series winners](https://github.com/cuttlefishh/python-for-data-analysis/blob/master/data/world_series_winners.txt).
35-
* Read in the lines of the file to a list, so that each line is an element of the list.
36-
* Create a new list with just those list elements that contain 'New York'; print that list, making sure there's not an extra newline character between each line.
37-
* Use the set class to convert the list of all World Series winners to a list of unique values.
38-
* Write the output to a new file.
34+
1. Download this text file of past [World Series winners](https://github.com/cuttlefishh/python-for-data-analysis/blob/master/data/world_series_winners.txt).
35+
2. Read in the lines of the file to a list, so that each line is an element of the list.
36+
3. Create a new list with just those list elements that contain 'New York'; print that list, making sure there's not an extra newline character between each line.
37+
4. Use the `set` class to convert the list of all World Series winners to a list of unique values.
38+
5. Write the output to a new file.

0 commit comments

Comments
 (0)