Introduction to Python
davidbpython.com
Project Warmup Exercises, Session 7
FUNCTIONS |
|
Ex. 7.1 | What is the correct way to call this function? |
def do(arg):
darg = arg * 2
return darg
|
|
Ex. 7.2 | What is the correct way to call this function? |
import random
def do():
num = random.randint(1, 10)
return num
|
|
Ex. 7.3 | What is the correct way to call this function? |
def do(arg, otherarg):
svals = arg + otherarg
return svals
|
|
Ex. 7.4 | What is the correct way to call this function? |
def do(arg, otherarg):
darg = arg * 2
dotherarg = otherarg * 2
return darg, dotherarg
|
|
Ex. 7.5 | Write a function addme() that takes two arguments, adds them together and returns the two arguments added / concatenated. Call it thusly: |
# your code here
x = addme(4, 5)
print(x)
y = addme('hey', 'you')
print(y)
|
|
Expected Output:
9 heyyou |
|
SET COMPARISONS |
|
Ex. 7.6 | Use the set .difference() method to show which of the submitted addresses is not on the whitelist. |
whitelist = {'joe@me.com', 'marie@there.com'}
submitted = {'joe@me.com', 'dark@lord.au', 'marie@there.com'}
|
|
Ex. 7.7 | Use the set .intersection() method to show which of the submitted addresses is on the blacklist. |
blacklist = ['dark@lord.au', 'skeevy@guy.ny', 'bad@news.eu']
submitted = ['joe@me.com', 'dark@lord.au', 'marie@there.com']
|
|
Ex. 7.8 | Use the set .union() method to show the complete set of clients between the NE_clients and NW_clients list. |
NE_clients = {'Alpha Industries', 'Beta Engrams, Inc.',
'Gamma Products, Ltd.'}
NW_clients = {'Alpha Industries', 'Gamma Products, Ltd.',
'Epsilon Publications'}
|
|
LIST COMPREHENSIONS |
|
Ex. 7.9 | Run the below code to see that the list comprehension returns a list of the items from the 'source list'. |
x = [1, 2, 3, 4]
y = [ item for item in x ]
|
|
Ex. 7.10 | Modify the list comprehension to double each value in the source list. |
x = [1, 2, 3, 4]
y = [ item for item in x ]
|
|
Ex. 7.11 | Write a list comprehension that halves each value in the source list. |
x = [1, 2, 3, 4]
|
|
Ex. 7.12 | Write a list comprehension that uppercases each string in the source list. |
x = ['hi', 'there', 'pythonistas']
|
|
Ex. 7.13 | Write a list comprehension that builds a list of only positive values (greater than 0). |
x = [7, -2, 9, -1, 0, 1, -3, 9, 3]
|
|
Ex. 7.14 | Write a list comprehension that builds a list of only names starting with underscore ('_'). |
dirlist = [ '__str__', '__repr__', 'upper', 'lower' ]
|
|
Ex. 7.15 | This list comprehension iterates over each line in the file (line for line in fh). Apply .rstrip() to each line to produce a list of stripped lines. |
fh = open('../revenue.csv')
lines = [ line for line in fh ]
fh.close()
|
|
Ex. 7.16 | Write a list comprehension that iterates over each line in the FF_tiny.txt file and slices out the year from each line. |
fh = open('../FF_tiny.txt')
fh.close()
|
|
Ex. 7.17 | Write a list comprehension that splits each line, producing a list for each line in the file. |
fh = open('../FF_tiny.txt')
fh.close()
|
|
Ex. 7.18 | Continuing with the solution from previous exercise, subscript the split() so that only one item from each list is passed to the resulting list. |
Ex. 7.19 | Given the code below, write a list comprehension that only returns words longer than 3 letters - print the resulting list. |
words = [ 'Hello', 'my', 'dear', 'the', 'heart', 'is',
'a', 'child.' ]
|
|
Expected Output:
['Hello', 'dear', 'heart', 'child'] |
|
Ex. 7.20 | Build on the previous exercise - modify the list comprehension so that the returned words are uppercased. |
Expected Output:
['HELLO', 'DEAR', 'HEART', 'CHILD.'] |
|
Ex. 7.21 | Open ../student_db.txt. Use a list comprehension to open the file and load it into a list as we did in previous exercises. Print the resulting list. (Hint: a list comprehension loops through any iterable (object that can be looped or iterated through), so you can place the call to open() right in the list comprehension and it will loop over that (or rather, the file object returned from it.) |
Expected Output:
['id:address:city:state:zip\n', 'jk43:23 Marfield Lane:Plainview:NY:10023\n', 'ZXE99:315 W. 115th Street, Apt. 11B:New York:NY:10027\n', 'jab44:23 Rivington Street, Apt. 3R:New York:NY:10002\n', 'ak9:234 Main Street:Philadelphia:PA:08990\n', 'ap172:19 Boxer Rd.:New York:NY:10005\n', 'JB23:115 Karas Dr.:Jersey City:NJ:07127\n', 'jb29:119 Xylon Dr.:Jersey City:NJ:07127\n'] |
|
Ex. 7.22 | Continuing the previous exercise, use the list comprehension to remove the newline from each line in the file. Print the resulting list - you should see each line of the file as before, but without the newline. |
Expected Output:
['id:address:city:state:zip', 'jk43:23 Marfield Lane:Plainview:NY:10023', 'ZXE99:315 W. 115th Street, Apt. 11B:New York:NY:10027', 'jab44:23 Rivington Street, Apt. 3R:New York:NY:10002', 'ak9:234 Main Street:Philadelphia:PA:08990', 'ap172:19 Boxer Rd.:New York:NY:10005', 'JB23:115 Karas Dr.:Jersey City:NJ:07127', 'jb29:119 Xylon Dr.:Jersey City:NJ:07127'] |
|
Ex. 7.23 | Continuing the previous exercise, exclude the 1st line (the header line) from the output. (Hint: file.readlines() returns a list of lines, and you can slice that list. But since we can't assign open() to a filehandle, we can simply treat open() as if it were the filehandle (because it returns it); thus we can call the readlines() method on open(), and we can slice any list returned form a method, so we can attach a slice directly to the call to readlines(). |
Expected Output:
['jk43:23 Marfield Lane:Plainview:NY:10023', 'ZXE99:315 W. 115th Street, Apt. 11B:New York:NY:10027', 'jab44:23 Rivington Street, Apt. 3R:New York:NY:10002', 'ak9:234 Main Street:Philadelphia:PA:08990', 'ap172:19 Boxer Rd.:New York:NY:10005', 'JB23:115 Karas Dr.:Jersey City:NJ:07127', 'jb29:119 Xylon Dr.:Jersey City:NJ:07127'] |
|