# 1)人:使用一个字典来存储一个人的信息,包括名、姓、年龄和居住的城市。该字典应包含键first_name、
# last_name、age和city。将存储在该字典中的每项信息都打印出来。
person = {'first_name': 'zhang', 'last_name': 'san', 'age': 18, 'city': 'beijing'}
print(person['first_name'])
print(person['last_name'])
print(person['age'])
print(person['city'])
# 2)喜欢的数1:使用一个字典来存储一些人喜欢的数。请想出5个人的名字,并将这些名字用作字典中的键。
# 再想出每个人喜欢的一个数,并将这些数作为值存储在字典中。打印每个人的名字和喜欢的数。为了让这个
# 程序更有趣,通过询问朋友确保数据是真实的。
favorite_numbers = {'zhangsan': 1, 'lisi': 2, 'wangwu': 3, 'zhaoliu': 4, 'tianqi': 5}
print(favorite_numbers['zhangsan'])
print(favorite_numbers['lisi'])
print(favorite_numbers['wangwu'])
print(favorite_numbers['zhaoliu'])
print(favorite_numbers['tianqi'])
# 3)词汇表1:Python字典可用于模拟现实生活中的字典。为避免混淆,我们将后者称为词汇表。
# 3.1)相处你在前面学过的五个编程术语,将它们用作词汇表中的键,并将他们的含义作为值存储再词汇表中。
# 3.2)以整洁的方式打印每个术语及其含义。为此,既可以先打印术语,在它后面加上一个冒号,再打印其含义;
# 也可以先在一行里打印术语,再使用换行符\n打印一个空行,然后在下一行里以缩进的方式打印其含义。
glossary = {
'variable': '存储数据的容器',
'function': '执行特定任务的代码块',
'loop': '重复执行代码的结构',
'dictionary': '存储键值对的数据结构',
'list': '存储多个值的列表',
}
for i in glossary:
print(i + ": " + glossary[i])
# 4)词汇表2:现在你知道了如何遍历字典,请整理你在第三题的代码,将其中的一系列函数调用print()替换为一个遍历字典中键和值的循环。确保该循环正确无误后,再在词汇表中添加5个python术语。当你再次运行这个程序时,这些新术语及其含义将自动包含在输出中。
# 这一题应该不用怎么写了吧,上面的其实稍微改改就可以用
# 5)河流:创建一个字典,在其中存储三条河流及其途径的国家。例如,一个键值对可能是'nile': 'egypt'。
# 5.1)使用循环为每条河流打印一条消息,如下所示。The Nile runs through Egypt.
# 5.2)使用循环将该字典中每条河流的名字打印出来
# 5.3)使用循环将该字典中包含的每个国家的名字打印出来
rivers = {
'nile': 'egypt',
'amazon': 'brazil',
'yangtze': 'china',
}
for i in rivers:
print("The " + i + " runs through " + rivers[i])
for i in rivers:
print(i)
for i in rivers.values():
print(i)
# 6)宠物:创建多个表示宠物的字典,每个字典都包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets的列表中,再遍历该列表,并将有关每个宠物的所有信息打印出来
pets = [
{'type': 'dog', 'owner': 'zhangsan'},
{'type': 'cat', 'owner': 'lisi'},
{'type': 'fish', 'owner': 'wangwu'},
]
for i in pets:
print(i['type'] + " " + i['owner'])
# 7)喜欢的地方:创建一个名为favorite_places的字典,并包含至少三个人,以及每个人喜欢的地方。将这个字典存储在一个名为favorite_places.py的文件中,并导入这个文件。然后,使用循环将每个人喜欢的地方打印出来。
favorite_places = {
'zhangsan': ['beijing', 'shanghai'],
'lisi': ['guangzhou', 'shenzhen'],
'wangwu': ['hangzhou', 'ningbo'],
}
for i in favorite_places:
print(i + ": " + str(favorite_places[i])) # str()是把列表转换成字符串
# 8)喜欢的数2:修改第二题的代码,让每个人都可以有多个喜欢的数字,然后将每个人的名字及其喜欢的数打印出来
favorite_numbers = {
'zhangsan': [1, 2, 3],
'lisi': [4, 5, 6],
'wangwu': [7, 8, 9],
}
for i in favorite_numbers:
print(i + ": " + str(favorite_numbers[i]))
# 9)城市:创建一个名为cities的字典,并包含至少三个城市的信息。每个城市的信息都包含itypes,包括城市名、国家名、人口数和 approximate_population_growth。使用键值对将这些信息存储在字典中。
cities = {
'beijing': {
'country': 'china',
'population': 21500000,
'approximate_population_growth': 1.1,
},
'shanghai': {
'country': 'china',
'population': 24250000,
'approximate_population_growth': 1.2,
},
'guangzhou': {
'country': 'china',
'population': 13500000,
'approximate_population_growth': 1.3,
},
}
for city, city_info in cities.items():
print("City: " + city)
country = city_info['country']
population = city_info['population']
approximate_population_growth = city_info['approximate_population_growth']
print("\tCountry: " + country)
print("\tPopulation: " + str(population))
print("\tApproximate population growth: " + str(approximate_population_growth))