Introduction to Python

davidbpython.com




Project Warmup Exercises, Session 4



EXERCISES RELATED TO list building assignment

 
Ex. 4.1 Create an empty list. Opening and looping through the file student_db.txt, split each line into elements, and append just the state values (the 4th value in each row) to a list. Print the list object. (Hint: make sure the list is initialized before the loop begins and is printed after the loop ends. If either statement is found inside the loop, it will be executed multiple times.
Expected Output:
['state', 'NY', 'NY', 'NY', 'PA', 'NY', 'NJ', 'NJ']
 
Ex. 4.2 Extend the previous program by omitting the first line from the file. One way to do this is to call line = next(fh) on the file handle (assuming this is the variable name you used for the filehandle). This should be done before the loop begins, and will advance the file pointer to the next line.
Expected Output:
['NY', 'NY', 'NY', 'PA', 'NY', 'NJ', 'NJ']
 
Ex. 4.3 Starting with an empty list, and then opening and looping through student_db.txt, append the 1st field (a student ID) to a list only if the state field is NY. Print the list object.
Expected Output:
['jk43', 'axe99', 'jab44', 'ap172']
 
Ex. 4.4 Opening and reading through revenue.csv, build a list from the 3rd field (the floating point value), then sum up the values using sum(). (Hint: sum() will not work unless you convert each element to a float value before you add it to the list, so do that by calling float() on each item.)
Expected Output:
662.01

# may have a tiny remainder, i.e., 662.0100000000001
 
Ex. 4.5 Extending the previous program, use len() to determine the length of the list, then use the formula sum(flist) / len(flist) to produce an average (where flist is the list of floats you compiled).
Expected Output:
94.5728571429

# your floating point remainder may be slightly different
 
Ex. 4.6 Given the following code:
values = [1, 3, 4, 10, 15]

Determine and print the median (middle) value without mentioning any values directly. (Hint: use len() to determine the length, then use that value to figure out the "halfway" index. Make sure your calculated index is an integer).

Expected Output:
4
 
Ex. 4.7 Given the following code:
values = [3, 1, 10, 15, 4]

Again, determine and print the median value. This time you'll need to use the sorted() function, which takes an unsorted list and returns a sorted list.

Expected Output:
4
 
Ex. 4.8 Given the following code:
values = [30, 20, 10, 40, 50, 60]

Determine the median value, which in an even-number of elements is halfway between the two "middle" values in a sorted list. (Hint: again use the len() of the list to calculate the indices of the middle two values.)

Expected Output:
35
 

EXERCISES RELATED TO word count project

 
Ex. 4.9 Open the pyku.txt file and use the file read() method to read it into a string. Print the length of this string using len()
Expected Output:
80
 
Ex. 4.10 Building on the previous program, count the number of times the word spam occurs in the file. (Hint: read() the file into a string and use the str count() method.)
Expected Output:
4
 
Ex. 4.11 Open the pyku.txt file and use the file read() method to read it into a string. Use str splitlines() to split the string into a list of lines. Print the type of the list returned from splitlines(), then print the first and last line from the file using list subscripts.
Expected Output:
<class 'list'>
We're out of gouda.
Spam, spam, spam, spam, spam.
 
Ex. 4.12 Open file 'pyku.txt' and use the file read() method to read it into a string. Use the string split() method to split the entire string into a list of words. Print the type of the list returned from split(), then print the first and last word from the file using list subscripts.
Expected Output:
<class 'list'>
We're
spam.
 
Ex. 4.13 Starting with this list:
var = ['a', 'b', 'c', 'd']

print the length of the list. (Hint: do not use a loop and a counter. Use len())

Expected Output:
4
 

EXERCISES RELATED TO spell check program

 
Ex. 4.14 Given a list containing duplicate values, initialize an empty set() (note that you cannot use empty braces because these are reserved for dicts -- use set()) and add each element to it to produce a set of unique values. Print the whole set. (Note: just for practice, don't pass the list directly to the set() constructor -- loop through the list and add each element one at a time.)
dupvals = [1, 3, 1, 1, 2, 3, 2, 1, 3]
Expected Output:
{1, 2, 3}
 
Ex. 4.15 Opening the revenue.csv text file, loop through and print each line of the file, but make sure there are no blank lines printed (hint: use rstrip() on each line to remove the newline character; print() itself already prints a newline at the end of every line).
Expected Output:
Haddad's,PA,239.50
Westfield,NJ,53.90
The Store,NJ,211.50
Hipster's,NY,11.98
Dothraki Fashions,NY,5.98
Awful's,PA,23.95
The Clothiers,NY,115.20
 
Ex. 4.16 Given the following list:
mylist = [-5, -2, 1, -3, 1.5, 7, 9]

Loop through mylist and build a new list that is only positive numbers (i.e., greater than 0). Print the list.

Expected Output:
[1, 1.5, 7, 9]
 
Ex. 4.17 Given the following code:
sentence = "I could; I wouldn't.  I might?  Might I!"

Split this sentence into words (without altering or processing them). Print each word on a line.

Expected Output:
I
could;
I
wouldn't.
I
might?
Might
I!
 
Ex. 4.18 Extending the previous code, use a single call to .rstrip() to remove any punctuation. (Hint: you can place any punctuation characters to be removed together in a single string argument to rstrip(); any single character within the string will be removed.)
Expected Output:
I
could
I
wouldn't
I
might
Might
I
 
Ex. 4.19 Extending the previous code, add an additional statement to lowercase each word.
Expected Output:
i
could
i
wouldn't
i
might
might
i
 
Ex. 4.20 Extending the previous code, add each word to a set(), then print the entire set.
Expected Output:
{'i', 'could', 'might', "wouldn't"}
 
Ex. 4.21 Open the file pyku.txt. .rstrip() the line. Loop through and print each line from the file.
Expected Output:
We're out of gouda.

This parrot has ceased to be.

Spam, spam, spam, spam, spam.
 
Ex. 4.22 Extending the previous solution, again strip each word of punctuation and lowercase each word and add each to a set. Print the set.
Expected Output:
{'be', 'has', 'parrot', 'to', 'this', 'of',
 'spam', 'ceased', "we're", 'gouda', 'out'}
 
Ex. 4.23 Start with the following code:
text = "we're certainly out of gouda but Python is great."

Now split this text into words (no need to strip or lowercase). Removing the printing of the set and adding to the end of the code, loop through each word in text and check the word against the set created earlier (use the in operator to check for membership). If the word is not in the set, print it.

Expected Output:
certainly
but
Python
is
great.
 
[pr]