python编程:从入门到实践----第六章:字典>练习
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
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
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
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
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
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.")
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'}
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'}
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
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
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编程:从入门到实践----第六章:字典>练习的更多相关文章
- #Python编程从入门到实践#第四章笔记
#Python编程从入门到实践#第四章笔记 操作列表 1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...
- python编程:从入门到实践----第六章>字典
一.一个简单的字典:alien_0存储外星人的颜色和点数,使用print打印出来 alien_0 = {'color': 'green','points': 5} print(alien_0['col ...
- Python:从入门到实践--第六章--字典--练习
#1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市.将字典中的每项信息都打印出来 friend = { 'last_name':'马', 'first_name':'脑壳', ' ...
- 《Python编程从入门到实践》第二章_变量和简单数据类型
什么是变量呢? 举例: >>> message = "Hello,Python!" >>> print (message) Hello,Pyth ...
- #Python编程从入门到实践#第三章笔记
列表简介 1.什么是列表 列表:由一系列按也顶顺序排列的元素组成.元素之间可以没有任何关系. 列表:用方括号[]表示,并用逗号分隔其中元素.名称一般为复数 2.访问元素 (1)列表是有序集合 ...
- Python编程从入门到实践笔记——异常和存储数据
Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...
- Python编程从入门到实践笔记——文件
Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...
- Python编程从入门到实践笔记——类
Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...
- Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
随机推荐
- HDU 4819 二维线段树
13年长春现场赛的G题,赤裸裸的二维线段树,单点更新,区间查询 不过我是第一次写二维的,一开始写T了,原因是我没有好好利用行段,说白一点,还是相当于枚举行,然后对列进行线段树,那要你写二维线段树干嘛 ...
- msf中arp_sweep使用报错:usbmon1:ERROR while getting interface flags:no such device
在许多的工具使用中,会出现很多的错误,要养成先思考再去寻找帮助的习惯 在用use命令使用arp_sweep模块的时候爆出错误:usbmon1:ERROR while getting interface ...
- oracle中判断"非"
在oracle中判断为"非"最常见的两种情况,一个是"不等于",一个的"非空". 通过查找资料得知,oracle中判断不等于的方法有好多种: ...
- nosql的介绍以及和关系型数据库的区别
一直对非关系型数据库和关系型数据库的了解感觉不太深入,在网上收集了一些关于sql和nosql的区别和优缺点分享给大家. Nosql介绍 Nosql的全称是Not Only Sql,这个概念早起就有人提 ...
- kibana下载与安装
目录 简介 下载 安装 测试 简介 Kibana是一个为ElasticSearch 提供的数据分析的 Web 接口.可使用它对日志进行高效的搜索.可视化.分析等各种操作.安装之前有话说: 安装路径不要 ...
- CCCC L3-015. 球队“食物链”(dfs+剪枝)
题意: 某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席突发奇想,希望从中找出一条包含所有球队的 ...
- Session简单介绍
Session 会话 , Session是基于Cookie的一种会话机制. Cookie是服务器返回一小份数据给客户端,并且存放在客户端上. Session是,数据存放在服务器端. 常用API //得 ...
- mini2440 裸机程序下载到 sdram 不能运行。
今天在 写了个简单的 led 的汇编程序,下载到 mini2440 的 nand flash 里面可以正常运行,但是下载到 sdram 里面不能运行. 后来发现有几个注意点, 要在 sdram 中运行 ...
- 七牛云存储javascript-sdk和java-sdk的使用
自己做项目使用的是一台阿里云最便宜的服务器,存储空间只有40G,静态资源和动态资源都放在一起.听说七牛云存储注册认证即送10G的免费存储,想着把静态资源放在七牛云上,分担一下阿里云服务器的存储压力. ...
- StringBuffer和StringBuilder类
对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类(String类是不可改变的,一旦创建了String对象,那它的值就无法改变了). 和String类不同的是,St ...