Python第三周第二次作业
2018-03-21 23:33
375 查看
本章学习字典的使用
6-3 词汇表alphabet = {
'cin': 'to input some datas',
'cout': 'to output some datas',
'print': 'to output some datas',
'upper': 'to make letter become upper',
'lower': 'to make letter become lower',
}
for k, v in alphabet.items():
print(k + ': ' + v)
6-5 河流
先打印键与值,再分别打印键和值(只有打印键时可以省略key())rivers = {
'yellowRiver': 'China',
'nile': 'Egypt',
'mississipi': 'America',
}
for river, country in rivers.items():
print('The ' + river.title() + ' runs through ' + country + '.')
for river in rivers:
print(river)
for country in rivers.values():
print(country)
6-8 宠物
似乎不能将声明的变量直接当成字符串打印出来?如果可以就好了……meow = {'name': 'meow', 'kind': 'cat', 'master': 'Alice'}
woof = {'name': 'woof', 'kind': 'dog', 'master': 'Bob'}
tweet = {'name': 'tweet', 'kind': 'bird', 'master': 'Cathy'}
pets = [meow, woof, tweet]
for pet in pets:
print(pet['name'].title() + ' is a ' +
pet['kind'] + ', its master is ' +
pet['master'] + '.')
6-11 城市
做这一题时我才发现:之前写print('\n')会变成换两行,是因为print默认带有换行……
print('\n', end = '')也可以直接写成print('')
cities = {
'Guangzhou': {'country': 'China', 'population': 15, 'fact': 'Beautiful!'},
'London': {'country': 'England', 'population': 8, 'fact': 'Wonderful!'},
'NewYork': {'country': 'America', 'population': 12, 'fact': 'Amazing!'},
}
for city, message in cities.items():
print(city + '---', end = '')
for key, value in message.items():
print(key + ': ' + str(value) + ' ', end = '')
print('\n', end = '')
6-3 词汇表alphabet = {
'cin': 'to input some datas',
'cout': 'to output some datas',
'print': 'to output some datas',
'upper': 'to make letter become upper',
'lower': 'to make letter become lower',
}
for k, v in alphabet.items():
print(k + ': ' + v)
6-5 河流
先打印键与值,再分别打印键和值(只有打印键时可以省略key())rivers = {
'yellowRiver': 'China',
'nile': 'Egypt',
'mississipi': 'America',
}
for river, country in rivers.items():
print('The ' + river.title() + ' runs through ' + country + '.')
for river in rivers:
print(river)
for country in rivers.values():
print(country)
6-8 宠物
似乎不能将声明的变量直接当成字符串打印出来?如果可以就好了……meow = {'name': 'meow', 'kind': 'cat', 'master': 'Alice'}
woof = {'name': 'woof', 'kind': 'dog', 'master': 'Bob'}
tweet = {'name': 'tweet', 'kind': 'bird', 'master': 'Cathy'}
pets = [meow, woof, tweet]
for pet in pets:
print(pet['name'].title() + ' is a ' +
pet['kind'] + ', its master is ' +
pet['master'] + '.')
6-11 城市
做这一题时我才发现:之前写print('\n')会变成换两行,是因为print默认带有换行……
print('\n', end = '')也可以直接写成print('')
cities = {
'Guangzhou': {'country': 'China', 'population': 15, 'fact': 'Beautiful!'},
'London': {'country': 'England', 'population': 8, 'fact': 'Wonderful!'},
'NewYork': {'country': 'America', 'population': 12, 'fact': 'Amazing!'},
}
for city, message in cities.items():
print(city + '---', end = '')
for key, value in message.items():
print(key + ': ' + str(value) + ' ', end = '')
print('\n', end = '')
相关文章推荐
- Python第三周第二次作业
- Python学习第三周第二次作业——字典
- 第二周第二次作业Python4.1-4.15
- Python自学 第二次作业
- 马哥2016全新Linux+Python高端运维班第三周作业
- 全新Linux+Python高端运维班第二次作业
- 马哥2016全新Linux+Python高端运维班第三周作业
- megeedu Linux+Python高级运维班 3期 第三周作业
- Linux+Python高端运维班第二次作业
- 第三周第二次作业
- 马哥2016全新Linux+Python高端运维班第三周作业
- 作业第三周第二次
- Python第二周第二次作业
- 马哥2016全新Linux+Python高端运维班第三周作业作答
- 高级编程技术 Python 第三周作业
- Python学习第四周第二次作业——函数
- [作业] 马哥2016全新Linux+Python高端运维班第三周作业
- [python作业] [第三周] [周三]
- 马哥2016全新Linux+Python高端运维班第三周作业
- Python第四周第二次作业