Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "copyright", "credits" or "license()" for more information. >>> def get_2_n_plus_7(length): output = [] for n in range(1,length+1): output.append(2 * n +7) return(output) >>> get_2_n_plus_7(7) [9, 11, 13, 15, 17, 19, 21] >>> [2 * n + 7 for n in range(1,11)] [9, 11, 13, 15, 17, 19, 21, 23, 25, 27] >>> get_2_n_plus_7(11) [9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29] >>> get_2_n_plus_7(10) [9, 11, 13, 15, 17, 19, 21, 23, 25, 27] >>> [2 * n + 7 for n in range(1,11)] [9, 11, 13, 15, 17, 19, 21, 23, 25, 27] >>> [n for n in [3,4,5,6,7] if n > 4] [5, 6, 7] >>> def edit_list(inlist,inmin): result = [] for n in inlist: if n >inmin: result.append(n) SyntaxError: expected an indented block >>> def edit_list(inlist,inmin): result = [] for n in inlist: if n >inmin: result.append(n) return(result) >>> edit_list([3,4,5,6,7],4) [5, 6, 7] >>> [n for n in [3,4,5,6,7] if n > 4] [5, 6, 7] >>> [[n,2 * n+7] for n in range(8) if n%2==0] [[0, 7], [2, 11], [4, 15], [6, 19]] >>> output = [[n,2 * n+7] for n in range(8) if n%2==0] >>> output [[0, 7], [2, 11], [4, 15], [6, 19]] >>> for item in [[n,2 * n+7] for n in range(8) if n%2==0]: print(item) [0, 7] [2, 11] [4, 15] [6, 19] >>> my_dictionary = {'eat' : 'verb', 'book' : 'noun', 'orange' : 'adjective'} >>> my_dictionary {'orange': 'adjective', 'book': 'noun', 'eat': 'verb'} >>> my_dictionary['orange'] 'adjective' >>> 'strange'[3] 'a' >>> my_dictionary['eat'] 'verb' >>> my_dictionary['cheese'] Traceback (most recent call last): File "", line 1, in my_dictionary['cheese'] KeyError: 'cheese' >>> my_dictionary['cheese'] = 'noun' >>> my_dictionary['cheese'] 'noun' >>> my_dictionary {'orange': 'adjective', 'cheese': 'noun', 'book': 'noun', 'eat': 'verb'} >>> 'chocolate' in my_dictionary False >>> 'cheese' in my_dictionary True >>> def lookup(key,dictionary): if key in dictionary: return(dictionary[keyt]) else: return('NOT IN DICTIONARY') >>> lookup('cheese') Traceback (most recent call last): File "", line 1, in lookup('cheese') TypeError: lookup() takes exactly 2 positional arguments (1 given) >>> lookup('cheese', my_dictionary) Traceback (most recent call last): File "", line 1, in lookup('cheese', my_dictionary) File "", line 3, in lookup return(dictionary[keyt]) NameError: global name 'keyt' is not defined >>> def lookup(key,dictionary): if key in dictionary: return(dictionary[key]) else: return('NOT IN DICTIONARY') >>> lookup('cheese', my_dictionary) 'noun' >>> lookup('chocolate', my_dictionary) 'NOT IN DICTIONARY' >>> my_dictionary['find']='verb' >>> my_dictionary {'orange': 'adjective', 'cheese': 'noun', 'book': 'noun', 'eat': 'verb', 'find': 'verb'} >>> for key in my_dictionary: print(key+':',my_dictionary[key]) orange: adjective cheese: noun book: noun eat: verb find: verb >>> dictionary.keys() Traceback (most recent call last): File "", line 1, in dictionary.keys() NameError: name 'dictionary' is not defined >>> my_dictionary.keys() dict_keys(['orange', 'cheese', 'book', 'eat', 'find']) >>> for key in my_dictionary.keys(): print(key) orange cheese book eat find >>> my_dictionary.values() dict_values(['adjective', 'noun', 'noun', 'verb', 'verb']) >>> my_dictionary.items() dict_items([('orange', 'adjective'), ('cheese', 'noun'), ('book', 'noun'), ('eat', 'verb'), ('find', 'verb')]) >>> list('acbc') ['a', 'c', 'b', 'c'] >>> list(my_dictionary.keys()) ['orange', 'cheese', 'book', 'eat', 'find'] >>> def print_dictionary_in_order(dictionary): list_of_keys = list(dictionary.keys()) list_of_keys.sort() for key in list_of_keys: print(key+':',dictionary[key]) >>> print_dictionary_in_order(my_dictionary) book: noun cheese: noun eat: verb find: verb orange: adjective >>> my_dictionary.update([['bear','common_noun'],['friendly','adjective']]) >>> print_dictionary_in_order(my_dictionary) bear: common_noun book: noun cheese: noun eat: verb find: verb friendly: adjective orange: adjective >>> my_dictionary['eat','transitive verb'] Traceback (most recent call last): File "", line 1, in my_dictionary['eat','transitive verb'] KeyError: ('eat', 'transitive verb') >>> my_dictionary['eat']='transitive verb' >>> my_dictionary {'cheese': 'noun', 'friendly': 'adjective', 'bear': 'common_noun', 'find': 'verb', 'book': 'noun', 'orange': 'adjective', 'eat': 'transitive verb'} >>> my_dictionary2 = {}.fromkeys(['see','do','leave'],'verb') >>> my_dictionary2 {'leave': 'verb', 'do': 'verb', 'see': 'verb'} >>> ================================ RESTART ================================ >>> >>> interactive_add_telephone_number() Enter name or hit return if finished: John Enter the number: 212-123-4567 Enter name or hit return if finished: Mary Enter the number: 917-123-4567 Enter name or hit return if finished: Fido Enter the number: 333-444-5555 Enter name or hit return if finished: Sally Enter the number: 123-456-789 Enter name or hit return if finished: Sally Enter the number: 123-456-7890 Enter name or hit return if finished: Pat's Pizza Enter the number: 444-555-6667 Enter name or hit return if finished: >>> my_phonebook {'Sally': '123-456-7890', 'John': '212-123-4567', 'Fido': '333-444-5555', 'Mary': '917-123-4567', "Pat's Pizza": '444-555-6667'} >>> interactive_add_telephone_number() Enter name or hit return if finished: Spot Enter the number: 333-444-5555 Enter name or hit return if finished: >>> my_phonebook {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> ================================ RESTART ================================ >>> >>> create_inverse_phonebook(my_phonebook) >>> backwards_phonebook {} >>> ================================ RESTART ================================ >>> >>> create_inverse_phonebook(my_phonebook,backwards_phonebook) >>> backwards_phonebook {} >>> my_phonebook = {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> create_inverse_phonebook(my_phonebook,backwards_phonebook) >>> backwards_phonebook {'444-555-6667': "Pat's Pizza", '123-456-7890': 'Sally', '333-444-5555': 'Fido', '212-123-4567': 'John', '917-123-4567': 'Mary'} >>> backwards_phonebook['123-456-7890'] 'Sally' >>> ================================ RESTART ================================ >>> >>> my_phonebook = {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> create_inverse_phonebook(my_phonebook,backwards_phonebook) Pat's Pizza 444-555-6667 Spot 333-444-5555 Fido 333-444-5555 John 212-123-4567 Sally 123-456-7890 Mary 917-123-4567 >>> backwards_phonebook {'444-555-6667': "Pat's Pizza", '123-456-7890': 'Sally', '333-444-5555': 'Fido', '212-123-4567': 'John', '917-123-4567': 'Mary'} >>> 'Fido,Spot' 'Fido,Spot' >>> [Fido,Spot] Traceback (most recent call last): File "", line 1, in [Fido,Spot] NameError: name 'Fido' is not defined >>> ['Fido','Spot'] ['Fido', 'Spot'] >>> backwards_phonebook['333-444-5555'] 'Fido' >>> ================================ RESTART ================================ >>> >>> my_phonebook = {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> create_inverse_phonebook(my_phonebook,backwards_phonebook) Pat's Pizza 444-555-6667 Spot 333-444-5555 Fido 333-444-5555 John 212-123-4567 Sally 123-456-7890 Mary 917-123-4567 >>> backwards_phonebook['333-444-5555'] Traceback (most recent call last): File "", line 1, in backwards_phonebook['333-444-5555'] KeyError: '333-444-5555' >>> backwards_phonebook {} >>> ================================ RESTART ================================ >>> >>> my_phonebook = {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> create_inverse_phonebook(my_phonebook,backwards_phonebook) Traceback (most recent call last): File "", line 1, in create_inverse_phonebook(my_phonebook,backwards_phonebook) NameError: name 'backwards_phonebook' is not defined >>> create_inverse_phonebook(my_phonebook) Pat's Pizza 444-555-6667 Spot 333-444-5555 Fido 333-444-5555 John 212-123-4567 Sally 123-456-7890 Mary 917-123-4567 >>> backwards_phonebook Traceback (most recent call last): File "", line 1, in backwards_phonebook NameError: name 'backwards_phonebook' is not defined >>> ================================ RESTART ================================ >>> >>> my_phonebook = {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> create_inverse_phonebook(my_phonebook) Pat's Pizza 444-555-6667 Spot 333-444-5555 Fido 333-444-5555 John 212-123-4567 Sally 123-456-7890 Mary 917-123-4567 >>> backwards_phonebook {} >>> ================================ RESTART ================================ >>> >>> my_phonebook = {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> create_inverse_phonebook(my_phonebook) Pat's Pizza 444-555-6667 Spot 333-444-5555 Fido 333-444-5555 John 212-123-4567 Sally 123-456-7890 Mary 917-123-4567 >>> backwards_phonebook {'444-555-6667': "Pat's Pizza", '123-456-7890': 'Sally', '333-444-5555': 'Spot,Fido', '212-123-4567': 'John', '917-123-4567': 'Mary'} >>> backwards_phonebook['333-444-5555'] 'Spot,Fido' >>> backwards_phonebook['333-444-5555'].split(',') ['Spot', 'Fido'] >>> ================================ RESTART ================================ >>> >>> my_phonebook = {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> create_inverse_phonebook2(my_phonebook) Pat's Pizza 444-555-6667 Spot 333-444-5555 Fido 333-444-5555 John 212-123-4567 Sally 123-456-7890 Mary 917-123-4567 >>> backwards_phonebook {'444-555-6667': ["Pat's Pizza"], '123-456-7890': ['Sally'], '333-444-5555': ['Spot', 'Fido'], '212-123-4567': ['John'], '917-123-4567': ['Mary']} >>> backwards_phonebook['333-444-5555'] ['Spot', 'Fido'] >>> types_of_dog = {'dalmatian','setter','hound','pointer'} >>> types_of_dog1 = ['dalmatian','setter','hound','pointer'] >>> types_of_dog {'dalmatian', 'hound', 'pointer', 'setter'} >>> types_of_dog; {'dalmatian', 'hound', 'pointer', 'setter'} >>> types_of_dogl.append('dachshund') Traceback (most recent call last): File "", line 1, in types_of_dogl.append('dachshund') NameError: name 'types_of_dogl' is not defined >>> types_of_dog1.append('dachshund') >>> types_of_dog1 ['dalmatian', 'setter', 'hound', 'pointer', 'dachshund'] >>> types_of_dog.append('dachsund') Traceback (most recent call last): File "", line 1, in types_of_dog.append('dachsund') AttributeError: 'set' object has no attribute 'append' >>> types_of_dog = {'dalmatian','setter','hound','pointer','setter'} >>> types_of_dog {'dalmatian', 'hound', 'pointer', 'setter'} >>> types_of_dog1 = ['dalmatian','setter','hound','pointer','setter'] >>> types_of_dog1 ['dalmatian', 'setter', 'hound', 'pointer', 'setter'] >>> phonebook Traceback (most recent call last): File "", line 1, in phonebook NameError: name 'phonebook' is not defined >>> phone_book Traceback (most recent call last): File "", line 1, in phone_book NameError: name 'phone_book' is not defined >>> backwards_phonebook {'444-555-6667': ["Pat's Pizza"], '123-456-7890': ['Sally'], '333-444-5555': ['Spot', 'Fido'], '212-123-4567': ['John'], '917-123-4567': ['Mary']} >>> my_phonebook {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> dictionary_of_dictionaries = {'phonebook',my_phonebook,'backwards',backwards_phonebook} Traceback (most recent call last): File "", line 1, in dictionary_of_dictionaries = {'phonebook',my_phonebook,'backwards',backwards_phonebook} TypeError: unhashable type: 'dict' >>> dictionary_of_dictionaries = {'phonebook':my_phonebook,'backwards':backwards_phonebook} >>> dictionary_of_dictionaries {'phonebook': {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'}, 'backwards': {'444-555-6667': ["Pat's Pizza"], '123-456-7890': ['Sally'], '333-444-5555': ['Spot', 'Fido'], '212-123-4567': ['John'], '917-123-4567': ['Mary']}} >>> dictionary_of_dictionaries['phonebook'] {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'} >>> dictionary_of_dictionaries['bigdict':dictionary_of_dictionaries] Traceback (most recent call last): File "", line 1, in dictionary_of_dictionaries['bigdict':dictionary_of_dictionaries] TypeError: unhashable type >>> dictionary_of_dictionaries['bigdict': dictionary_of_dictionaries] Traceback (most recent call last): File "", line 1, in dictionary_of_dictionaries['bigdict': dictionary_of_dictionaries] TypeError: unhashable type >>> dictionary_of_dictionaries {'phonebook': {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'}, 'backwards': {'444-555-6667': ["Pat's Pizza"], '123-456-7890': ['Sally'], '333-444-5555': ['Spot', 'Fido'], '212-123-4567': ['John'], '917-123-4567': ['Mary']}} >>> dictionary_of_dictionaries['bigdict'] = dictionary_of_dictionaries >>> dictionary_of_dictionaries {'bigdict': {...}, 'phonebook': {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'}, 'backwards': {'444-555-6667': ["Pat's Pizza"], '123-456-7890': ['Sally'], '333-444-5555': ['Spot', 'Fido'], '212-123-4567': ['John'], '917-123-4567': ['Mary']}} >>> dictionary_of_dictionaries['bigdict'] {'bigdict': {...}, 'phonebook': {"Pat's Pizza": '444-555-6667', 'Spot': '333-444-5555', 'Fido': '333-444-5555', 'John': '212-123-4567', 'Sally': '123-456-7890', 'Mary': '917-123-4567'}, 'backwards': {'444-555-6667': ["Pat's Pizza"], '123-456-7890': ['Sally'], '333-444-5555': ['Spot', 'Fido'], '212-123-4567': ['John'], '917-123-4567': ['Mary']}} >>>