第六章课后作业
2018-03-22 17:34
288 查看
Chapter Six
6-1 人
james = { 'first_name': "James", 'last_name' : "Brown", 'age' : 23, 'live_city' : "Hong Kong" } print("First name:\t" + james['first_name']) print("Last name:\t" + james['last_name']) print("Age:\t\t" + str(james['age'])) print("Live city:\t" + james['live_city'])
6-2 喜欢的数字
favorite_nums = { 'Yamato': 23, "Fubuki": 35, "Sakura": 10086, "Shimakaze": 2333, "Saratoga": 666 } for name, num in favorite_nums.items(): print(name + "'s favorite number is " + str(num))
6-3 词汇表
vocabulary_table = { 'python': "A popular programing language", 'list': "A kind of structure which linked data with index", 'ergodic': "Travel all stored values in a structure in order", 'tuple': "A kind of structure like a unmodified list", 'output': "an amount of something produced by a person, machine, factory, country, etc" } print("python:\t" + vocabulary_table['python']) print("list:\t" + vocabulary_table['list']) print("ergodic:\t" + vocabulary_table['ergodic']) print("tuple:\t" + vocabulary_table['tuple']) print("output:\t" + vocabulary_table['output'])
6-4 词汇表2
vocabulary_table = { 'python': "A popular programing language", 'list': "A kind of structure which linked data with index", 'ergodic': "Travel all stored values in a structure in order", 'tuple': "A kind of structure like a unmodified list", 'output': "an amount of something produced by a person, machine, factory, country, etc" } for vocabulary, explanation in vocabulary_table.items(): print(vocabulary + ":\t" + explanation)
6-5 河流
dict_river = { 'nile': 'Egypt', 'amazon': 'Colombia', 'changjiang': 'China' } for river, country in dict_river.items(): print("The " + river + " river runs through " + country) for river in dict_river.keys(): print(river) for country in dict_river.values(): print(country)
6-6 调查
research_list = ["Zhang San", "Li Si", "Wang Wu", "Zhou Liu"] research_result = { "Zhang San": 13, "Wang Wu": 25, "Zhou Liu": 33 } for name in research_list: if name in research_result.keys(): print(name + ", thank you for joining our research.") else: print(name + ", would you like to join our research?")
6-7 人
james = { 'first_name': "James", 'last_name' : "Brown", 'age': 23, 'live_city' : "Hong Kong" } tom = { 'first_name': "Tom", 'last_name': "Smith", 'age': 19, 'lived_city': "Guangzhou" } jack = { 'first_name': "jack", 'last_name': "Green", 'age': 25, 'lived_city': "Shanghai" } people_list = [james, jack, tom] for people in people_list: print("First name:\t" + james['first_name']) print("Last name:\t" + james['last_name']) print("Age:\t\t" + str(james['age'])) print("Live city:\t" + james['live_city']) print()
6-8 宠物
dabai = { 'name': "Dabai", 'type': "cat", 'owner': "Zhang San" } erhuang = { 'name': "Erhuang", 'type': "dog", 'owner': "Li Si" } ahua = { 'name': "Ahua", 'type': "bird", 'owner': "Wang Wu" } pets = [dabai, erhuang, ahua] for pet in pets: print("Name:\t" + pet['name'] + "\nType:\t" + pet['type'] + "\nOwner:\t" + pet['owner'] + "\n")
6-9 喜欢的地方
favorite_places = { 'Zhang San': ['Shanghai', 'Beijing', 'New York'], 'Li Si': ['Chongqing', 'Guangzhou'], 'Wang Wu': ['Singapo', 'Tokyo'] } for people, places in favorite_places.items(): print("Name:\t" + people) print("Favorite Place:\t") for place in places: print("\t" + place) print()
6-10 喜欢的数字
favorite_nums = { 'Yamato': [23, 35], "Fubuki": [6, 17], "Sakura": [1024], "Shimakaze": [2333, 666, 888], "Saratoga": [9420] } for name, nums in favorite_nums.items(): if len(nums) > 1: print(name + "'s favorite numbers are:") else: print(name + "'s favorite number is:") for num in nums: print("\t" + str(num))
6-11 城市
cities = { 'Shanghai': { 'country': 'China', 'population': 2.420e7, 'fact': 'Shanghai is a big city' }, 'New York': { 'country': 'USA', 'population': 8.51e6, 'fact': 'New York belongs to USA' }, 'Beijing':{ 'country': 'China', 'population': 2.173e7, 'fact': "Beijing is the capital of China" } } for city, info in cities.items(): print(city) print('Country:\t' + info['country']) print('Population:\t' + str(info['population'])) print('Describe:\t' + info['fact']) print()