PythonCrashCourse 第六章习题
使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。该字典应包含键first_name 、last_name 、age 和city 。将存储在该字典中 的每项信息都打印出来
person = {
'first_name' : 'Higos',
'last_name' : 'Jess',
'age' : 24,
'city' : 'shanghai',
}
print(person['first_name'])
print(person['last_name'])
print(person['age'])
print(person['city'])
使用一个字典来存储一些人喜欢的数字。请想出5个人的名字,并将这些名字用作字典中的键;想出每个人喜欢的一个数字,并将这些数字作为值存 储在字典中。打印每个人的名字和喜欢的数字。为让这个程序更有趣,通过询问朋友确保数据是真实的
favorite_numbers ={
'chandler' : 69,
'joey' : 23,
'ross' : 85,
'rachel' : 12,
'phoebe' : 2
}
for key,value in favorite_numbers.items():
print(f"{key}'s favorite numbers is : {value}.")
Python字典可用于模拟现实生活中的字典,但为避免混淆,我们将后者称为词汇表
想出你在前面学过的5个编程词汇,将它们用作词汇表中的键,并将它们的含义作为值存储在词汇表中。
以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加上一个冒号,再打印词汇的含义;也可在一行打印词汇,再使用换行符(\n )插 入一个空行,然后在下一行以缩进的方式打印词汇的含义。
glossary = {
'string' : 'A series of characters.',
'comment' :'A note in a program that the Python interpreter ignores.',
'list' : 'A collection of items in a particular order.',
'loop' : 'Work through a collection of items,one at a time.',
'dictionary' : 'A collection of key-value pairs.',
}
for key,value in glossary.items():
print(f"{key.title()} :\n\t{value}")
既然你知道了如何遍历字典,现在请整理你为完成练习6-3而编写的代码,将其中的一系列print 语句替换为一个遍历字典中的键和值的循环。确定该 循环正确无误后,再在词汇表中添加5个Python术语。当你再次运行这个程序时,这些新术语及其含义将自动包含在输出中
glossary = {
'string' : 'A series of characters.',
'comment' :'A note in a program that the Python interpreter ignores.',
'list' : 'A collection of items in a particular order.',
'loop' : 'Work through a collection of items,one at a time.',
'dictionary' : 'A collection of key-value pairs.',
'key' : 'The first item in a key-value in a dictionary.',
'value' : 'An item associated with a key in a dictionary.',
'conditional test' : 'A comparsion between two values',
'float' : 'A numerical value with a decimal component.',
'boolean expression' : 'An expression that evaluates to True or False',
}
for key,value in glossary.items():
print(f"{key.title()} :\n\t{value}")
创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt'
- 使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。
- 使用循环将该字典中每条河流的名字都打印出来。
- 使用循环将该字典包含的每个国家的名字都打印出来。
rivers = {
'Nile' : 'Egypt',
'Yellow River' : 'China',
'Mekong' : 'Vietnam',
'Amazon River' : 'Peru',
'Mississippi' : 'American',
}
for river,country in rivers.items():
print(f"The {river} runs through {country}")
for river in rivers:
print(f"The river's named {river}")
for country in rivers.values():
print(f"The country was {country}.")
在6.3.1节编写的程序favorite_languages.py中执行以下操作
- 创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。
- 遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与调查的人,打印一条消息邀请他参与调查。
favorite_languages = {
'jen' : "python",
'sarah' : 'c',
'edward' : 'ruby',
'phil' : 'python',
}
for name,language in favorite_languages.items():
print(f"{name.title()}'s favorite language is {language.title()}.")
print("\n")
friends =['jen','phil','chandler','phoebe']
for name in friends:
if name in favorite_languages.keys():
print(f"{name.title()}, thank you for taking the poll.")
else:
print(f"{name.title()}, please take our poll!")
在为完成练习6-1而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为people 的列表中。遍历这个列表,将其中每个人的所有 信息都打印出来
person_1 = {
'first_name' : 'Higos',
'last_name' : 'Jess',
'age' : 24,
'city' : 'shanghai',
}
person_2 = {
'first_name' : 'Chandler',
'last_name' : 'Bing',
'age' : 30,
'city' : 'Los Angeles',
}
person_3 = {
'first_name' : 'Ross',
'last_name' : 'Geller',
'age' : 30,
'city' : 'Los Angeles',
}
people =[person_1,person_2,person_3]
for person in people:
full_name = person['first_name']+' '+person['last_name']
age = person['age']
city = person['city']
print(f"{full_name.title()}'s age is {age},and he/she lives in {city}")
创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets 的列表中,再遍历该列表,并将宠物的所有信息都打印出来
pet_0 = {
'pet' :'Husky',
'owner' : 'chandler',
}
pet_1 = {
'pet' : 'monkey',
'owner' : 'ross',
}
pet_2 = {
'pet' : 'cat',
'owner' : 'rachel',
}
pets =[pet_0,pet_1,pet_2]
for pet in pets:
print(f"{pet['owner']}'s pet is a {pet['pet']}")
创建一个名为favorite_places 的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。为让这个练 习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来
favorite_places = {
'chandler' : {"shanghai","xian","beijing"},
'rachel' : {"chengdu","wuhan","hangzhou1"},
'joey' : {"haerbin","shenzhen","dongguan"},
}
for name,places in favorite_places.items():
print(f"{name.title()}'s favorite places are:")
for place in places:
print(f"\t{place.title()}")
修改为完成练习6-2而编写的程序,让每个人都可以有多个喜欢的数字,然后将每个人的名字及其喜欢的数字打印出来
favorite_numbers ={
'chandler' : [69,389],
'joey' : [23,21],
'ross' : [85,4],
'rachel' : [12,43,594,183],
'phoebe' : [2],
}
for name,numbers in favorite_numbers.items():
print(f"{name.title()}'s favorite numbers are:")
for number in numbers:
print(f"\t{number}")
创建一个名为cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含country 、population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来
cities ={
'shanghai' : {
'country' : 'China',
'population' : '20 million',
'fact' : 'The rich city of China',
},
'Los Angeles' :{
'country' : 'American',
'population' : '40 million',
'fact' : 'the city fill with COVID-19',
},
'Hangzhou' :{
'country' : 'China',
'population' : '15 million',
'fact' : 'The most beautiful city of China',
}
}
for city,informations in cities.items():
print("The city name is :"+ city)
country = informations['country'].title()
population = informations['population']
fact = informations['fact']
print("\t"+city.title()+" belongs to "+country+".")
print('\t' +'there are about '+ population +' people to lives')
print('\t' +'the fact of ' +city.title() +" is: "+fact)
PythonCrashCourse 第六章习题的更多相关文章
- PythonCrashCourse 第三章习题
PythonCrashCourse 第三章习题 3.1 将一些朋友的姓名存储在一个列表中,并将其命名为names.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来 names = ['lih ...
- Perl语言入门:第六章习题:处理用户所指定的名字并汇报相应的姓。
37 print "\n----------------------------------_exercise_6_1--------------------------\n"; ...
- PythonCrashCourse 第四章习题
Python 从入门到实践第四章习题 4.1想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅 ...
- C和指针 第六章 习题
6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...
- C和指针 第十六章 习题
16.8 计算平均年龄 #include <stdlib.h> #include <stdio.h> #define MAX_LEN 512 int main() { int ...
- 学习opencv 第六章 习题十三
用傅里叶变换加速卷积,直接上代码,Mat版是Copy他人的. CvMat版 #include "stdafx.h" #include "cv.h" #inclu ...
- java编程思想第四版第六章习题
(略) (略) 创建两个包:debug和debugoff,他们都包含一个相同的类,该类有一个debug()方法,第一个版本显示发送给控制台的String参数,而第二版本什么也不做,使用静态import ...
- Learning Perl 第六章习题第一题
按照first name找last name 知识点: 1. hash的使用和初始化 2. 使用exists函数检测hash中的键是否存在
- PythonCrashCourse 第七章习题
编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如"Let me see if I can find you a Subaru" car =input("Wha ...
随机推荐
- 一年经验Java开发0713面试
@ 目录 介绍一下你做的某些模块,有些什么比较复杂的地方? 你们的文件怎么存储的? 怎么没有用文件服务器? 文件存储有没有做备份? 在项目上有没有什么搞不定的问题? 对搞不定的问题你是怎么处理的? 你 ...
- w10查看wifi密码
1.选择网络和Internet设置 右键单击网络连接图标,选择“打开网络和Internet设置”. 2.选择网络和共享中心
- python如何支持并发?
由于GIL(Global Interpreter Lock)的存在使得在同一时刻Python进程只能使用CPU的一个核心,也就是对应操作系统的一个 内核线程,对于一个Python web程序,如果有个 ...
- java基础(九)--方法重载
如System.out.println()方法即是方法重载的. 以下举例说明自定义sum()方法的重载 package cnblogs; public class TestBase09MathRelo ...
- 【评价指标】详解F1-score与多分类MacroF1&MicroF1
文章来自:一个宝藏微信公众号[机器学习炼丹术] 基本概念 首先,要背住的几个概念就是:accuracy,precision,recal, TP,FP,TN,FN TP:true positive.预测 ...
- PHP fscanf() 函数
定义和用法 fscanf() 函数根据指定的格式对来自打开的文件的输入进行解析. 语法 fscanf(file,format,mixed) 参数 描述 file 必需.规定要检查的文件. format ...
- PHP xml_set_external_entity_ref_handler() 函数
定义和用法 xml_set_external_entity_ref_handler() 函数规定当解析器在 XML 文档中找到外部实体时被调用的函数. 如果成功,该函数则返回 TRUE.如果失败,则返 ...
- luogu P5161 WD与数列 SAM 线段树合并 启发式合并
LINK:WD与数列 这道题可谓妙绝 我明白了一个增量统计的原理. 原本的想法是:差分之后 显然长度为1的单独统计 长度为2的以及更多就是字符串之间的匹配问题了. 对差分序列建立SAM 由于第一个是一 ...
- 账本APP服务器端开发
账本APP开发 好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 上一篇文章我们聊 ...
- ipa包如何打包?ios打包ipa的四种方法分享
今天带来的内容是ios打包ipa的四种方法.总结一下,目前.app包转为.ipa包的方法有以下几种,下面一起来看看吧! 1.Apple推荐的方式,即实用xcode的archive功能 Xco ...