Python第五次作业
参考书目:《Python编程从入门到实践》
2018/03/22
6-1. 人 : 使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。该字典应包含键first_name 、last_name 、age 和city。将存储在该字典中的每项信息都打印出来
person = {"first_name":"John", "last_name": "Mike", "age": 18,"city": "New York"}
print(person)
6-2. 喜欢的数字 :使用一个字典来存储一些人喜欢的数字。请想出5个人的名字,并将这些名字用作字典中的键;想出每个人喜欢的一个数字,并将这些数字作为值存储在字典中。 打印每个人的名字和喜欢的数字。为让这个程序更有趣,通过询问朋友确保数据是真实的。
favorite_number = {"John": 13,"Jim": 6, "Mike": 3, "Amy": 2, "Tim":1}
print(favorite_number)
6-3. 词汇表 :Python字典可用于模拟现实生活中的字典,但为避免混淆,我们将后者称为词汇表。
想出你在前面学过的5个编程词汇,将它们用作词汇表中的键,并将它们的含义作为值存储在词汇表中。
以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加上一个冒号, 再打印词汇的含义;也可在一行打印词汇,再使用换行符(\n )插入一个空行,然后在下一行以缩进的方式打印词汇的含义。
Explain = {'print': 'show on the screen','in': 'check the element is include in the list',
'for': 'loop', 'if': 'determineif the condition is true', 'while': 'loop'}
print("print:",Explain['print'])
print("in:",Explain['in'])
print("for:",Explain['for'])
print("if:",Explain['if'])
print("while:",Explain['while'])
6-4. 词汇表2 :既然你知道了如何遍历字典,现在请整理你为完成练习6-3而编写的代码,将其中的一系列print 语句替换为一个遍历字典中的键和值的循环。确定该循环正确无误后,再在词汇表中添加5个Python术语。当你再次运行这个程序时,这些新术语及其含义将自动包含在输出中。
Explain = {'print': 'show on the screen','in': 'check the element is include in the list',
'for': 'loop', 'if': 'determine if the condition is true', 'while':'loop'}
for key,value in Explain.items():
print(key,":",value)
6-5. 河流 :创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt' 。
使用循环为每条河流打印一条消息,如“The Nile runs throughEgypt.”。
使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来。
rivers = {'Yangtze river': 'China', 'Nileriver': 'Egypt', 'Amazon': 'Brazil'}
for river,country in rivers.items():
print(river,"runs through",country)
print("\n\n")
for river in rivers.keys():
print(river,end = " ")
print("\n\n")
for country in rivers.values():
print(country,end= " ")
6-6. 略
6-7. 人 :在为完成练习6-1而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为people 的列表中。遍历这个列表,将其中每个人的所有信息都打印出来。
person1 = {"first_name":"John", "last_name": "Mike",
"age": 18, "city": "New York"}
person2 = {'first_name': 'Amy','last_name': 'Mike',
'age': 20, 'city': 'New York'}
person3 = {'first_name': 'Bob','last_name': 'Mike',
'age': 15, 'city': 'New York'}
people = [person1, person2, person3]
for person in people:
print(person)
6-8. 宠物 :创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets的列表中, 再遍历该列表,并将宠物的所有信息都打印出来。
cat = {'host': 'Bob', 'type': 'cat'}
dog = {'host': 'John', 'type': 'dog'}
bird = {'host': 'Mike', 'type': 'bird'}
pets = [cat, dog, bird]
for pet in pets:
print(pet)
6-9. 喜欢的地方 :创建一个名为favorite_places的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。 为让这个练习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
place1 = ['China', 'America']
place2 = ['Australia', 'Japan']
place3 = ['China', 'Brazil']
favorite_places = {'John': place1, 'Mike':place2, 'cow': place3}
for key,value in favorite_places.items():
print(key,":",value)
6-10. 喜欢的数字 :修改为完成练习6-2而编写的程序,让每个人都可以有多个喜欢的数字,然后将每个人的名字及其喜欢的数字打印出来。
favorite_numeber1 = [13,4]
favorite_numeber2 = [2,6]
favorite_numeber3 = [3,7]
favorite_numebers = {'John':favorite_numeber1, 'Mike': favorite_numeber2, 'Jim': favorite_numeber3}
for key, value infavorite_numebers.items():
print(key,":",value)
6-11. 城市 :创建一个名为cities的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含country 、 population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
New_York = {'country': 'America','population': '4 billion', 'fact': 'developed country'}
Beijing = {'country':'China', 'population':'14 billion', 'fact': 'developing country'}
Tokyo = {'country': 'Japan', 'population':'1 billion', 'fact': 'developed country'}
cities = {'New York': New_York, 'Beijing':Beijing, 'Tokyo': Tokyo}
for key, value in cities.items():
print(key,":",value)