6-1 人:使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。该字典应包含键first_name 、last_name 、age 和city 。将存储在该字典中的每项信息都打印出来。
friends = {'first name':'Zou','last name':'Li Peng','age':27,'city':'Shenzhen'}
print('first name:' + friends[ 'first name'])
print('last name:' + friends[ 'last name'])
print('age:' + str(friends['age']))
print('city:' + friends['city']) #输出结果:
first name:Zou
last name:Li Peng
age:27
city:Shenzhen
 
6-2 喜欢的数字:使用一个字典来存储一些人喜欢的数字。请想出5个人的名字,并将这些名字用作字典中的键;想出每个人喜欢的一个数字,并将这些数字作为值存储在字典中。打印每个人的名字和喜欢的数字。为让这个程序更有趣,通过询问朋友确保数据是真实的。
favorite_numbers ={ 'Mary':2, 'Jim':15, 'Lucy':20, 'Crystal':28, 'Panda':68}
print('Mary:' + str(favorite_numbers['Mary']))
print('Jim:' + str(favorite_numbers['Jim']))
print('Lucy:' + str(favorite_numbers['Lucy']))
print('Crystal:' + str(favorite_numbers['Crystal']))
print('Panda:' + str(favorite_numbers['Panda'])) #输出结果:
Mary:2
Jim:15
Lucy:20
Crystal:28
Panda:68
 
6-3 词汇表 :Python字典可用于模拟现实生活中的字典,但为避免混淆,我们将后者称为词汇表。
a.想出你在前面学过的5个编程词汇,将它们用作词汇表中的键,并将它们的含义作为值存储在词汇表中。
b.以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加上一个冒号,再打印词汇的含义;也可在一行打印词汇,再使用换行符(\n )插入一个空行,然后在下一行以缩进的方式打印词汇的含义。
words={
'hello':'a greeting',
'word':'used to express agreement',
'meet':'suitable; fit; proper',
'fruit':'a male homosexual',
'bird':'a person of a specified kind or character'
}
print('hello:' + words['hello'])
print('word:' + words['word'])
print('meet:' + words['meet'])
print('fruit:' + words['fruit'])
print('bird:' + words['bird'])
#输出结果:
hello:a greeting
word:used to express agreement
meet:suitable; fit; proper
fruit:a male homosexual
bird:a person of a specified kind or character

  

6-4 词汇表2 :既然你知道了如何遍历字典,现在请整理你为完成练习6-3而编写的代码,将其中的一系列print 语句替换为一个遍历字典中的键和值的循环。
words={
'hello':'a greeting',
'word':'used to express agreement',
'meet':'suitable; fit; proper',
'fruit':'a male homosexual',
'bird':'a person of a specified kind or character',
}
for key, value in words.items(): #items方法:返回一个键-值列表
print("\nkey:" +key)
print("Value:" +value) #输出结果:
key:hello
Value:a greeting key:word
Value:used to express agreement key:meet
Value:suitable; fit; proper key:fruit
Value:a male homosexual key:bird
Value:a person of a specified kind or character

  

6-5 河流 :创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt' 。
a.使用循环为每条河流打印一条消息,如“The Nileruns throughEgypt.”。
b.使用循环将该字典中每条河流的名字都打印出来。
c.使用循环将该字典包含的每个国家的名字都打印出来。

rivers ={
'yangtze':'china',
'danube':'germany',
'nile':'egypt'
}
for key ,value in rivers.items():
print("The "+key.title() +" runs through " + value.title())
for river in rivers.keys():
print(river.title())
for river in rivers.values():
print(river.title()) #输出结果:
The Yangtze runs through China
The Danube runs through Germany
The Nile runs through Egypt
Yangtze
Danube
Nile
China
Germany
Egypt

  

 6-6 调查 :在6.3.1节编写的程序favorite_languages.py中执行以下操作。
a.创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。
b.遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与调查的人,打印一条消息邀请他参与调查。
favorite_language = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
Investigated_person = {
'jen':'python',
'sarah':'c',
}
for persons in favorite_language.keys():
if persons in Investigated_person:
print(persons + ",thank you to attend investigation.")
else:
print(persons + ",please attend in the investigation.")
6-7 人:在为完成练习6-1而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为people 的列表中。遍历这个列表,将其中每个人的所有
信息都打印出来。
friends1 = {'first name':'Crystal','last name':'Wang','age':29,'city':'Shenzhen'}
friends2 = {'first name':'Zou','last name':'Li','age':27,'city':'Hongkong'}
friends3 = {'first name':'James','last name':'Wu','age':28,'city':'Beijing'}
friends = [friends1,friends2,friends3]
for friend in friends:
print(friend) #输出结果:
{'first name': 'Crystal', 'last name': 'Wang', 'age': 29, 'city': 'Shenzhen'}
{'first name': 'Zou', 'last name': 'Li', 'age': 27, 'city': 'Hongkong'}
{'first name': 'James', 'last name': 'Wu', 'age': 28, 'city': 'Beijing'}
 
6-8 宠物 :创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets
的列表中,再遍历该列表,并将宠物的所有信息都打印出来。
Black ={ 'kind':'dog','host':'Crystal'}
Mini ={ 'kind':'cat','host':'Zou'}
White ={ 'kind':'bird','host':'James'}
pets = [Black,Mini,White]
for pet in pets:
print(pet)
#输出结果:
{'kind': 'dog', 'host': 'Crystal'}
{'kind': 'cat', 'host': 'Zou'}
{'kind': 'bird', 'host': 'James'}
 
6-9 喜欢的地方 :创建一个名为favorite_places 的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。为让这个练
习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
favorite_places = {
'crystal':['hainan','xinjiang','xizang'],
'james':['beijing','taiwan','hongkong'],
'zou':['jiangshu','shanghai','yunnan'],
}
for name,favorite_place in favorite_places.items():
print("\n"+name.title() + "'s favorite places are:")
for place in favorite_place:
print("\t"+ place.title()) #输出结果:
Crystal's favorite places are:
Hainan
Xinjiang
Xizang James's favorite places are:
Beijing
Taiwan
Hongkong Zou's favorite places are:
Jiangshu
Shanghai
Yunnan  
 
6-10 喜欢的数字 :修改为完成练习6-2而编写的程序,让每个人都可以有多个喜欢的数字,然后将每个人的名字及其喜欢的数字打印出来。

favorite_numbers ={
'mary':[2,10,15],
'jim':[3,4,18],
'lucy':[6,8,12],
'crystal':[9,16,25],
'panda':[21,28,68]
}
for name,favorite_number in favorite_numbers.items():
print("\n" + name.title() + "'s favorite numbers are:")
for numbers in favorite_number:
print(str(numbers))
#输出结果:
Mary's favorite numbers are:
2
10
15 Jim's favorite numbers are:
3
4
18 Lucy's favorite numbers are:
6
8
12 Crystal's favorite numbers are:
9
16
25 Panda's favorite numbers are:
21
28
68
 
6-11 城市 :创建一个名为cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该
城市的事实。在表示每座城市的字典中,应包含country 、population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来。

cities ={
'haikou':{
'country':'China',
'population':'25万',
'fact':'At the southernmost tip of China',
},
'beijing':{
'country':'China',
'population':'60万',
'fact':'Capital of China',
},
'shanghai':{
'country':'China',
'population':'65万',
'fact':'Financial city',
}
}
for city,city_info in cities.items():
print("\nCity:"+ city)
Country = city_info['country']
Population = city_info['population']
Fact = city_info['fact']
print('Country:'+Country.title())
print('Population:'+Population.title())
print('Fact:'+Fact.title())
#输出结果:
City:haikou
Country:China
Population:25万
Fact:At The Southernmost Tip Of China City:beijing
Country:China
Population:60万
Fact:Capital Of China City:shanghai
Country:China
Population:65万
Fact:Financial City
 
 

 

 
 

python编程:从入门到实践----第六章:字典>练习的更多相关文章

  1. #Python编程从入门到实践#第四章笔记

    #Python编程从入门到实践#第四章笔记   操作列表 ​​​1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...

  2. python编程:从入门到实践----第六章>字典

    一.一个简单的字典:alien_0存储外星人的颜色和点数,使用print打印出来 alien_0 = {'color': 'green','points': 5} print(alien_0['col ...

  3. Python:从入门到实践--第六章--字典--练习

    #1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市.将字典中的每项信息都打印出来 friend = { 'last_name':'马', 'first_name':'脑壳', ' ...

  4. 《Python编程从入门到实践》第二章_变量和简单数据类型

    什么是变量呢? 举例: >>> message = "Hello,Python!" >>> print (message) Hello,Pyth ...

  5. #Python编程从入门到实践#第三章笔记

      列表简介 ​​​1.什么是列表 列表:由一系列按也顶顺序排列的元素组成.元素之间可以没有任何关系. 列表:用方括号[]表示,并用逗号分隔其中元素.名称一般为复数 2.访问元素 (1)列表是有序集合 ...

  6. Python编程从入门到实践笔记——异常和存储数据

    Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...

  7. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

  8. Python编程从入门到实践笔记——类

    Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...

  9. Python编程从入门到实践笔记——函数

    Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...

  10. Python编程从入门到实践笔记——用户输入和while循环

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

随机推荐

  1. JAVA中添加jar包

    右键点击工程文件,选择构建路径>添加外部归档.选择包的路径即可

  2. centos7搭建kafka集群

    一.安装jdk 1.下载jdk压缩包并移动到/usr/local目录 mv jdk-8u162-linux-x64.tar.gz /usr/local 2.解压 tar -zxvf jdk-8u162 ...

  3. linux系统pid的最大值研究

    内核源码探查 通过对linux内核源码的追踪,可以看到对pid最大值的限定最终集中到include/linux/threads.h文件中的PID_MAX_DEFAULT上了,代码如下: /* * Th ...

  4. Python pip设置为清华镜像

    设置为默认镜像 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

  5. P 1026 程序运行时间

    转跳点:

  6. UVA - 1645 Count (统计有根树)(dp)

    题意:输入n(n <=1000),统计有多少个n结点的有根树,使得每个深度中所有结点的子结点数相同.输出数目除以109+7的余数. 分析: 1.dp[i],i个结点的有根树个数 2.假设n=7, ...

  7. UVA - 1615 Highway(高速公路)(贪心+区间选点)

    题意:给定平面上n(n<=105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里德距离不超过D. 分析: 1.根据D可以算出每个点在x轴上的可选区 ...

  8. 人人嫌丑的iPhoneX刘海屏为何会被手机厂商竞相模仿

    不得不提到的是,苹果的iPhone自发布以来就始终引领着智能手机发展的方向.比如iPhone一代出现之后,就慢慢将键盘手机赶下历史舞台,让触屏手机成为主流.此外,iPhone的指纹识别.金属机身.玻璃 ...

  9. org.apache.jasper.JasperException: /WEB-INFO/jsp/product/edit.jsp(168,45)

    PWC6038:"${empty data.code?'001':fn:substring(data.code,0,8)}" contains invalid expression ...

  10. python格式字符