Python Crash Course dd 02-09-2013 Hour 4, Containers 1. Create a list that contains the names of 5 students of this class. (Do not ask for input to do that, simply create the list.) Print the list. Ask the user to input one more name and append it to the list. Print the list. Ask a user to input a number. Print the name that has that number as index. Add "John Smith" and "Mary Miller" at the beginning of the list (by using "+"). Print the list. 2. Continue with the script from 1: Print the list. Remove the last name from the list. Print the list. Ask a user to type a name. Check whether that name is in the list: if it is then delete it from the list. Otherwise add it at the end. Create a copy of the list in reverse order. Print the original list and the reverse list. 3. Explain the results for the functions if: li = [2,0,1,0] How to go from: li.count(0) a = ['A','B','c',None,3] to li.append(“A”) a = ['A','B','B','c',None,3] to 2 in li a = ['B','c',None] to li[1:-1] a = ['c','c'] to li[2] a = ['c','c','c'] t0 li.sort() a = ['c','c'] li.pop() len(li) 4. Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple. e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields [(2, 2), (1, 3), (3, 4, 5), (1, 7)] 5. Given a list of strings, return a list with the strings in sorted order, except group all the strings that begin with 'x' first. e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] 6. Write the result of each operation, where d = {1:”A”,3:”X”} len(d) d.has_key(3) d.setdefault(2,””) d.values() d.get(1) d.keys() d[3] = “B” d[“A”] print d['1'] print d.has_key('B') d.setdefault('C',3) 7. What dictionary method would we use to combine two dictionaries together? 8. We know that dictionary values can be arbitrary Python objects, but what about the keys? Try using different types of objects as the key other than numbers or strings. What worked for you and what didn't? As for the failures, why do you think they didn't succeed? 9. Dictionary and List Methods. a. Create a dictionary and display its keys alphabetically. b. Now display both the keys and values sorted in alphabetical order by the key. c. Same as part (b), but sorted in alphabetical order by the value. (Note: this generally has no practical purpose in dictionaries or hash tables in general because most access and ordering [if any] is based on the keys. This is merely an exercise.) 10. Given a pair of identically-sized lists, say, [1, 2, 3, …], and ['abc', 'def', 'ghi', …], process all that list data into a single dictionary that looks like: {1: 'abc', 2: 'def', 3: 'ghi', …}.