Python编程:从入门到实践(课后习题:第6章 字典)
coding=gbk
coding:utf-8!
6-1人
friend1 = {‘first_name’: ‘yao’, ‘last_name’: ‘hu’, ‘age’: 30, ‘city’:
‘shenzhen’,
}
print(friend1)
print(friend1[‘first_name’] + ', ’ + friend1[‘last_name’] + ', ’ +
str(friend1[‘age’]) + ', ’ + friend1[‘city’])
6-2喜欢的数字
favorite_nums = {‘john’: 2,
‘sarah’: 8,
‘nancy’: 9,
‘jenny’: 1,
‘josh’: 6,
}
print("\n")
print(favorite_nums)
for i, j in favorite_nums.items():
print(i.title() + "'s favorite number is " + str(j) + “.”)
注意items后的括号
print("\n")
6-3词汇表
dicts = {‘a’: ‘apple’, ‘b’: ‘boy’,
‘c’: ‘cat’, ‘d’: ‘dog’,
}
for key, value in dicts.items():
print(key + ": " + value)
为什么不能decode中文??? # 要在最前面加一个coding=gbk
print("\n")
6-4词汇表2
dicts = {‘a’: ‘apple’, ‘b’: ‘boy’,
‘c’: ‘cat’, ‘d’: ‘dog’,
}
for key, value in dicts.items():
print("Key: " + key)
print("Value: " + value)
print("\n")
6-5河流
三大河流
NO.1多瑙河Danube流经奥地利、斯洛伐克、保加利亚、匈牙利、克罗埃西亚、塞尔维亚、罗马尼亚、摩尔多瓦、乌克兰等10个国家,最终注入黑河
NO.2尼日尔河Niger流经贝宁、布基纳法索、喀麦隆、乍得、科特迪瓦、几内亚、马里、尼日尔、尼日利亚以及阿尔及利亚等10个国家,最终注入几内亚湾
NO.3尼罗河Nile流经卢旺达、布隆迪、坦桑尼亚、肯尼亚、乌干达、扎伊尔、苏丹、埃塞俄比亚和埃及等9个国家,最终流入地中海
rivers = {
‘nile’: ‘egypt’,
‘niger’: ‘algeria’,
‘danube’: ‘ukraine’,
}
for key, value in rivers.items():
print("The " + key.title() + " runs through " + value.title() + “.”)
for key, value in rivers.items():
print("River name: " + key.title())
print("Country name: " + value.title())
网上参考答案
for river in rivers.keys():
print(river)
for nation in rivers.values():
print(nation)
print("\n")
6-6调查
favorite_languages = {
‘jen’: ‘python’,
‘sarah’: ‘c’,
‘edward’: ‘ruby’,
‘phil’: ‘python’,
}
invited = [‘sarah’, ‘phil’, ‘jason’, ‘decula’]
for i in invited:
if i in favorite_languages.keys():
print(i.title() + “, thank you for accepting the investigation.”)
else:
print(i.title() + “, please accept your investigation.”)
print("\n")
6-7人
friend1 = {‘first_name’: ‘yao’, ‘last_name’: ‘hu’, ‘age’: 30, ‘city’:
‘shenzhen’,
}
friend2 = {‘first_name’: ‘john’, ‘last_name’: ‘edward’, ‘age’: 15, ‘city’:
‘england’,
}
friend3 = {‘first_name’: ‘sarah’, ‘last_name’: ‘nancy’, ‘age’: 20, ‘city’:
‘france’,
}
people = [friend1, friend2, friend3] # 这里用中括号,且前后不用空号
for p in people:
print§
网上参考答案
for friend in people:
for key, value in friend.items():
print(key + ‘: ’ + str(value))
print(’\n’)
6-8宠物
dianah = {‘name’: ‘dianah’, ‘type’: ‘persian’, ‘master’: ‘abbey’} #波斯猫
felicia = {‘name’: ‘felicia’, ‘type’: ‘birman’, ‘master’: ‘jane’} #伯曼猫
kiara = {‘name’: ‘kiara’, ‘type’: ‘toyger’, ‘master’: ‘callie’} #虎皮猫
lily = {‘name’: ‘lily’, ‘type’: ‘himalayan’, ‘master’: ‘fanny’} #喜马拉雅猫
pets = [dianah, felicia, kiara, lily]
for pet in pets:
for i, j in pet.items():
print(i + “: " + j)
print(”\n")
6-9喜欢的地方
favorite_places = {‘gabriel’: [‘chicago’, ‘phoneix’, ‘dall’],
‘kaley’: [‘detroit’, ‘houston’, ‘dallas’],
‘lana’: [‘seattle’, ‘austin’, ‘boston’],
}
for name, places in favorite_places.items():
print(name.title() + " favorite places are: “)
for place in places:
print(”\t" + place.title())
print("\n")
6-10喜欢的数字
favorite_nums = {‘john’: [2, 6, 8],
‘sarah’: [8, 9, 10],
‘nancy’: [9, 12, 18],
‘jenny’: [1, 22, 28],
‘josh’: [6, 9, 18],
}
for name, nums in favorite_nums.items():
print(name + “'s favorite numbers are: “)
for num in nums:
print(”\t” + str(num))
print("\n")
6-11城市
cities = {
‘new york’: {‘country’: ‘america’,
‘population’: ‘8.51 million people’,
‘fact’: ‘the lagest citi in the country’},
‘paris’: {‘country’: ‘france’,
‘population’: ‘2.24 million people’,
‘fact’: ‘capital of france’},
‘london’: {‘country’: ‘england’,
‘population’: ‘8.9 million people’,
‘fact’: “the world’s lagest financial center”},
}
for city, infos in cities.items():
print("city: " + city)
country = infos[‘country’]
population = infos[‘population’]
fact = infos[‘fact’]
print("country name: " + country)
print(“population: " + population)
print(“fact: " + fact)
print(”\n”)
网上参考答案
for city, informations in cities.items():
print(‘city: ’ + city)
for key, value in informations.items():
print(key + ‘: ’ + str(value))
print(’\n’)
Python编程:从入门到实践(课后习题:第6章 字典)相关推荐
- Python编程从入门到实践课后答案:第七章
7-1 汽车租赁 :编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如"LetmeseeifIcan find you a Subaru". 7-2 餐馆订位 :编写一个 ...
- Python编程从入门到实践(第三、四章的列表和元祖)
1.Python中列表用[]来表示,并用逗号分隔其中元素 2.访问列表元素,给出元素的索引值即可(索引从0开始) 3.修改,添加和删除元素 3.1修改时给出列表名和修改元素的索引,然后赋新值 3.2在 ...
- 《Python编程从入门到实践》习题答案及重点
发现自己对于python的基础掌握的并不是很牢实,利用几天时间把重点写下来,并打算把(<Python编程从入门到实践>试一试)的代码全部敲一遍,如果有不对的地方,请不吝赐教. 目录 第1章 ...
- 《Python编程从入门到实践》习题06
<Python编程从入门到实践>--动手试一试 函数 书本116页 8-1,8-2 #8-1 def dispaly_message():print("In this chapt ...
- 《Python编程从入门到实践》学习笔记6:字典
字典是Python中的唯一的映射类型,也是一个容器类型.Python的字典能够存储任意个数的Python对象,也包括其他容器类型.创建一个基本的字典,需要包括字典名称,以及用花括号扩起来的键值对,如下 ...
- python编程从入门到实践课后题答案-《Python编程:从入门到实践》课后习题及答案—第11章...
第11章 测试代码 11-1 城市和国家 :编写一个函数,它接受两个形参:一个城市名和一个国家名.这个函数返回一个格式为City, Country 的字符串,如Santiago, Chile .将这个 ...
- python编程从入门到实践课后题答案-python编程:从入门到实践习题5-8~5-11
5-8 以特殊方式跟管理员打招呼: 创建一个至少包含 5个用户名的列表,且其中一 个用户名为'admin'.想象你要编写代码,在每位用户登录网站后都打印一条问候消息. 遍历用户名列表,并向每位用户打印 ...
- Python编程:从入门到实践 课后习题-第十一章 测试代码
11-1 城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名.这个函数返回一个格式为 City, Country 的字符串,如 Santiago, Chile.将这个函数存储在一个名为 ...
- python从入门到实践答案第四章_python从入门到实践课后习题第四章
""" 4-1 比萨:想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for循环将每种比萨的名称都打印出来. 修改这个 for 循环,使其打印包含比萨名称的句子 ...
- Python编程从入门到实践 课后题 9-9电瓶升级
在本节最后一个electric_car.py版本中,给Battery 类添加一个名为upgrade_battery() 的方法.这个方法检查电瓶容量,如果它不是85,就将它 设置为85.创建一辆电瓶容 ...
最新文章
- dataframe,python,numpy 问题索引1
- java new string 图_Java中String直接赋字符串和new String的一些问题
- 鸿蒙os整体战略发布会,就在明天,华为将发布“鸿蒙OS”整体战略,能否用于手机?...
- 网络拓扑系列 - 网络拓扑的“管理”元素
- 第十章 使用机器学习的建议-机器学习老师板书-斯坦福吴恩达教授
- logback修改日志内容_巧用maven profile动态修改logback日志目录
- 重置样式表--HTML
- [Linux]Linux下安装和配置solr/tomcat/IK分词器 详细实例二.
- 1108D. Diverse Garland
- Windows Phone 7开发,进度条ProgressBar使用详解
- zabbix监控vsftp服务,发生故障并自动恢复
- 免费讲座:数据库工程实施中的性能保证
- pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
- 逻辑强化(03)真假推理 知识练习
- 网易AI平台开放多项技术,助力网易七鱼智能客服升级
- c语言打印地址的格式错误,printf()在c中以%p格式打印的地址是哪一个?
- 太阳光轨迹软件_轻松画太阳视运动轨迹
- 集群和均衡负载[摘]
- 复旦计算机专业分数线6,【数据】36所强基计划高校2020入围分数线与专业点评,报名参考!...
- 带标签的infogan及其代码解析