您的位置:首页 > 编程语言 > Python开发

《Python编程》第六章部分课后练习题

2018-03-22 17:15 393 查看
#6-3 词汇表:
代码:#6-3 词汇表

dic = {
'<vector>': 'Vectors are sequence containers representing arrays that can change in size.',
'<stack>': 'Stacks are a type of container adaptor, specifically designed to operate in '
'a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.',
'<map>': 'Maps are associative containers that store elements formed by a combination of a '
'key value and a mapped value, following a specific order.',
'<set>': 'Sets are containers that store unique elements following a specific order.',
'<queue>': 'queues are a type of container adaptor, specifically designed to operate in a'
' FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.',
}
for key, value in dic.items():
print(key, ':', value, '\n')
输出:<vector> : Vectors are sequence containers representing arrays that can change in size.

<stack> : Stacks are a type of container adaptor, specifically designed to operate in a LI
FO context (last-in first-out), where elements are inserted and extracted only from one en
d of the container.

<map> : Maps are associative containers that store elements formed by a combination of a k
ey value and a mapped value, following a specific order.

<set> : Sets are containers that store unique elements following a specific order.

<queue> : queues are a type of container adaptor, specifically designed to operate in a FI
FO context (first-in first-out), where elements are inserted into one end of the container
and extracted from the other.



#6-5 河流:
代码:#6-5 河流

rivers = {
'nile': 'egypt',
'amazon': 'brazil',
'thames': 'england',
}
for river, country in rivers.items():
message = 'The ' + river.title() + ' runs through ' + country.title()
print(message)
print('\n')

for river in rivers.keys():
print(river.title())
print('\n')

for country in rivers.values():
print(country.title())
输出:The Nile runs through Egypt
The Amazon runs through Brazil
The Thames runs through England

Nile
Amazon
Thames

Egypt
Brazil
England

#6-8 宠物:
代码:#6-8 宠物

dogs = {
'Affenpinscher': 'Smiths',
'Afghan Hound': 'Bob',
'Aidi': 'Peter',
}
cats = {
'Bobtail': 'Lory',
'American Shorthair': 'Peter',
'Bombay': 'Tom',
}
birds = {
'Canary': 'Lory',
'Budgy': 'Tom',
'Cockatiel': 'Bob',
}
pets = [dogs, cats, birds]
for pet in pets:
for name, owner in pet.items():
message = owner + ' has '
if name[0] in 'AOEIU':
message += 'an '
else:
message += 'a '
message += name
print(message)
输出:Smiths has an Affenpinscher
Bob has an Afghan Hound
Peter has an Aidi
Lory has a Bobtail
Peter has an American Shorthair
Tom has a Bombay
Lory has a Canary
Tom has a Budgy
Bob has a Cockatiel

#6-11 城市:
代码:#6-11 城市

Los_Angeles = {
'country': 'USA',
'population': '3,971,883',
'fact': 'It is home to the Wilshire Grand Center, California’s tallest Building.',
}
Guangzhou = {
'country': 'China',
'population': '13,501,100',
'fact': '2008 The 49th World Table Tennis Championships.',
}
Tokyo = {
'country': 'Japan',
'population': '9,262,046',
'fact': 'In 2016, it was to be replaced by the New National Stadium.',
}
cities = {
'Los Angeles': Los_Angeles,
'Guangzhou': Guangzhou,
'Tokyo': Tokyo,
}
for city, dic in cities.items():
message = '\n' + city + ':'
print(message)
for key, value in dic.items():
message = ' ' + key + ': ' + value
print(message) 输出:Los Angeles:
country: USA
population: 3,971,883
fact: It is home to the Wilshire Grand Center, California’s tallest Building.

Guangzhou:
country: China
population: 13,501,100
fact: 2008 The 49th World Table Tennis Championships.

Tokyo:
country: Japan
population: 9,262,046
fact: In 2016, it was to be replaced by the New National Stadium.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: