第6章作业
习题6-1
>>> person = {'first_name':'Alice', 'last_name': 'Bob', 'age':18, 'city':'Guangzhou'}
>>> person
{'first_name': 'Alice', 'last_name': 'Bob', 'age': 18, 'city': 'Guangzhou'}
>>> print(person['first_name'])
Alice
>>> print(person['last_name'])
Bob
>>> print(person['age'])
18
>>> print(person['city'])
Guangzhou
习题6-6
favorite_languages = {
'jen': 'python',
'serah': 'c',
'edward': 'ruby',
'phil': 'python',
}
people = ['alice', 'jen', 'bob', 'phil']
for person in people:
if person not in favorite_languages.keys():
print(person + ', please take our poll!')
else:
print(person + ', thank you for taking the poll.')
Output:
alice, please take our poll!
jen, thank you for taking the poll.
bob, please take our poll!
phil, thank you for taking the poll.
习题6-8
favorite_places = {
'alice': ['Guangzhou', 'Shenzhen', 'Beijing'],
'bob': ['Hangzhou', 'Shanghai'],
'ben': ['Chengdu'],
}
for person, places in favorite_places.items():
print('Person: ' + person)
print('Places:')
for i in places:
print(i)
print('')
Output
Person: alice
Places:
Guangzhou
Shenzhen
Beijing
Person: bob
Places:
Hangzhou
Shanghai
Person: ben
Places:
Chengdu
本文通过几个具体的Python编程示例介绍了如何使用字典来存储个人信息、记录人们的最爱语言及地点,并展示了如何遍历字典进行信息检索和处理。
187

被折叠的 条评论
为什么被折叠?



