1 python_vocabulary = { 'import this': 'Zen of python', 2 'insert': '插入', 3 'range': '范围', 4 'popped': '术语弹出' 5 } 6 print(python_vocabulary) 7 8 for vocabulary, means in python_vocabulary.items(): # 第二部分包含字典名和方法items() 9 print('\nvocabulary: ' + vocabulary) 10 print('means: ' + means) 11 12 python_vocabulary['append'] = '向列表中添加元素' 13 python_vocabulary['set()'] = '剔除重复的值' 14 python_vocabulary['keys()'] = '字典键' 15 python_vocabulary['values()'] = '字典值' 16 python_vocabulary['items()'] = '无法描述(重点)' 17 18 for key,values in python_vocabulary.items(): 19 print('\nVocabulary: ' + key) 20 print('\nmeans: ' + values) 21 22 rivars = { 'China':'changjiang','nile':'egypt','brazil':'amazon'} 23 for country,rivar in rivars.items(): 24 print('The '+ rivar.title() + ' runs through ' + country.title() + '.') 25 26 # for country in rivars.items(): #.keys()与.value() 位置和输入错误 下文订正 27 # print (rivars.key()) 28 # for rivar in rivars.items(): 29 # print (rivars.value()) 30 for country in rivars.keys(): 31 print (country.title()) 32 for rivar in rivars.values(): 33 print (rivar.title()) #.title()少输入括号后 显示异常 34 #35 # 36 # 37 # 38 # 39 # 40 41 aliens = [] 42 for alien_number in range(30): 43 new_alien = { 'color': 'green', 'points': 5, 'speed': 'slow'} 44 aliens.append(new_alien) 45 for alien in aliens[:5]: 46 print(alien) 47 print('...') 48 print(str(len(aliens))) 49 50 for alien in aliens[0:3]: # 切片语法还未熟练 51 if alien['color'] == 'green': 52 alien['color'] = 'yellow' 53 alien['speed'] == 'medium' 54 alien['points'] = 10 55 56 for alien in aliens[:5]: 57 print(alien) # 多打s则变为打印列表 58 59 favorite_languages = { 60 'jen': ['python', 'ruby'], 61 'sarah': ['c'], 62 'edward': ['ruby', 'go'], 63 'phil': ['python', 'haskell'], 64 } 65 for name, languages in favorite_languages.items(): 66 if len(languages) >= 2: 67 print("\n" + name.title() + "'s favorite languages are:") 68 else: 69 print("\n" + name.title() + "'s favorite languages is:") 70 for language in languages:#此处for循环不可与上个for循环缩进相同 71 print("\t" + language.title()) 72 73 # 练习6-1 6-1 人 :使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。该字典应包含键 74 # first_name 、last_name 、age 和city 。将存储在该字典中的每项信息都打印出来。 75 76 shiitakeimoto = { 'first_name': 'ying', 'last_name': 'ying', 'age': 22, 'city': 'fuzhou'} 77 ergou = { 'first_name': 'lin', 'last_name': 'zhu', 'age': 24, 'city': 'shanghai'} 78 erzi = { 'first_name': 'junqin', 'last_name': 'cai', 'age': 25, 'city': 'kunming'} 79 80 people = [shiitakeimoto, ergou, erzi] 81 for name in people: 82 print(name) 83 #错误例子 84 # nike = {'zhonglei':'taidi','zhuern':'ergou'} 85 # doudou = {'zhonglei':'yingduan','zhuren':'momoe'} 86 # pingtou = {'zhonglei':'shuilan','zhuren':'erzi'} 87 88 pets = { 'nike': { 'zhonglei': 'taidi', 'zhuern': 'ergou'}, 'doudou': { 'zhonglei': 'yingduan', 'zhuren': 'momoe'}, 89 'pingtou': { 'zhonglei': 'shuilan', 'zhuren': 'erzi'}, } 90 91 # pets = {nike,doudou,pingtou} 92 print(pets) 93 94 users = { 'aeinstein': { 'first': 'albert', 'last': 'einstein', 'location': 'princeton', }, 95 'mcurie': { 'first': 'marie', 'last': 'curie', 'location': 'paris', }, } 96 print(users) 97 98 favorite_places = { 'ergou':['suzhou','shanxi','shenzhen'],'erzi':['osaka','kyoto'],'momoe':['liancang']} 99 favorite_places['shiitakeimoto'] = ['taiba','suizokukan','jinnjya']100 for name,places in favorite_places.items():101 if len(places) >= 2:102 print(name.title() + '\'s favorite places are:' )103 else:104 print(name.title() + '\'s favorite place')105 for place in places:106 print('\t' + place.title())107 108 109 message = input("Tell me something, and I will repeat it back to you: ")110 print(message)