CS 0008: Fall 2013 Lab 9 YOUR NAME: YOUR PARTNER'S NAME: To get credit on this lab, attend recitation, work with your partner to answer the questions, and give "reasonable" (if not all correct) answers. Each of you should hand in a hardcopy of your own solution, due at the beginning of next week's recitation. This lab will give you practice with slicing and dicing strings and lists, accessing elements of nested lists, comparing mutable versus immutable objects, and working with dictionaries. You will be working with the shell. Please write down (next to the *s) what you think will be printed and then enter the code. If your answer was wrong, please be sure to understand the code before moving to the next question. Remember, number the positions between the string and list elements. That way, the slices will make sense >>> s1 = "I poured Spot remover on my dog." >>> s2 = "Now he's gone." >>> s1[4:9] * >>> s2[-1] * >>> s2[:7] * >>> s1[6:] * >>> s1[:3] + s1[4:] * >>> lst = [1,2,3] >>> help(list.extend) Help on method_descriptor: extend(...) L.extend(iterable) -- extend list by appending elements from the iterable >>> lst.extend([44,55]) >>> lst * >>> lst.extend(range(2,5)) >>> lst * >>> lst.extend("Dog") >>> lst * >>> lst.remove(44) >>> lst.remove(55) >>> lst * >>> lst = [] >>> lst1 = [0,1,2] >>> lst2 = ["Spot","remover"] >>> lst3 = [[3,4],[5,6]] >>> lst.append(lst1) >>> lst * >>> lst.append(lst2) >>> lst * >>> lst.append(lst3) >>> lst * >>> lst[1] * >>> lst[2] * >>> lst[3] * >>> lst[2] * >>> lst[2][0] * >>> lst[2][0][1] * >>> Write code that inputs an int between 1 and 5 from the user, and creates a list called "lst" that contains that many empty lists. For example, if the user enters 3, your code should create [[],[],[]]. (You will have deja vu on Assignment 4.) * * * * >>> lst * >>> board = [] >>> for i in range(3): ... row = [] ... for col in range(3): ... row.append("*") ... board.append(row) ... >>> board What is printed here? * >>> >>> lst = [2,3,4] >>> lst.remove(3) >>> lst * >>> s = "Death is hereditary" >>> s[0] = "d" * >>> lst[0] = 0 >>> lst * >>> def fun1(s): ... s * 3 ... return None ... >>> s1 = "You're unique like everyone else" >>> fun1(s1) >>> s1 * >>> def fun2(s): ... return s * 3 ... >>> s1 = fun2(s1) >>> s1 * >>> def fun3(lst): ... lst.append("construction") ... >>> x = [3,4,5] >>> fun3(x) >>> x * >>> >>> name = "Darwin" >>> caps = name.upper() >>> caps * >>> name * >>> >>> d = [["knock knock","who's there"],["cash","cash who?"],["No thanks, but I would like a peanut instead","Groan"]] >>> person1 = 0 >>> person2 = 1 >>> for i in d: ... print("P1:",i[person1]) ... print("P2:",i[person2]) ... print() ... * * * * * * >>> birds = {"canada goose":3,"northern fulmar":1} >>> birds["northern fulmar"] * >>> birds.get("northern fulmar") * >>> birds.get("dove",-1) * >>> birds.get("dove",0) * >>> birds["dove"] * >>> for x in birds: ... print(x,birds[x]) ... * * >>> if "dove" in birds: ... del birds["dove"] ... >>> birds * >>> if "canada goose" in birds: ... del birds["canada goose"] ... >>> birds * >>> File birds.txt: Canada Goose Giant Rubber Duck Robin Wren Wren Giant Rubber Duck Robin Robin Robin Hummingbird infile = open("birds.txt",'r') birds = {} for line in infile: # Get rid of the \n at the end name = line.strip() if not name in birds: birds[name] = 1 else: birds[name] += 1 for b in birds: print(b,birds[b]) infile.close() What is the output? (Note: you won't know the order in which the birds are printed) * * * * * Replace the if-then-else statement with one single line. Use dictionary method "get". * *