Python:从入门到实践--第六章--字典--练习
#1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市。将字典中的每项信息都打印出来
friend = {
'last_name':'马',
'first_name':'脑壳',
'age':'',
'live_city':'北京',
}
print('姓名:'+friend['last_name'] + friend['first_name'] + '\n'+ '年龄:'+friend['age'] +
'\n' + '居住城市:'+friend['live_city']) print('\n')
#2.喜欢的数字:使用一个字典来存储一些人喜欢的数字,打印每个人的名字和喜欢的数字。
datas = {'Mike':'','Lucy':'','Neko':'','Neo':''}
print('Mike favorite number is :' + datas['Mike'])
print('Lucy favorite number is :' + datas['Lucy'])
print('Neko favorite number is :' + datas['Neko'])
print('Neo favorite number is :' + datas['Neo']) print('\n')
#3.词汇表:Python字典可用于模拟现实生活中的字典,单位了避免混淆,我们将后者称为词汇表。
#将已学过的编程词汇作为词汇表中的键,并将它们的含义作为值存储在词汇表中
#以整洁的方式打印每个词汇及含义 words = {'for':'循环方式','print':'打印输出在屏幕显示','pop()':'从列表结尾中删除数据','sort':'按字母从小到大永久排序'} print('for:' + words['for'])
print('print:' + words['print'])
print('pop():' + words['pop()'])
print('sort:' + words['sort'])
#1.词汇表:Python字典可用于模拟现实生活中的字典,单位了避免混淆,我们将后者称为词汇表。
#将已学过的编程词汇作为词汇表中的键,并将它们的含义作为值存储在词汇表中
#以整洁的方式打印每个词汇及含义
#遍历并打印出整个字典的键和值 words = {'for':'循环方式','print':'打印输出在屏幕显示','pop()':'从列表结尾中删除数据','sort':'按字母从小到大永久排序'} for key,value in words.items():
print(key + ' : ' + value ) print('\n')
#2.河流:创建一个字典,在其中存储三条大河流及流经的国家
#使用循环打印出来 rivers = {'Changjianghuanghe':'China','Nile':'Egypt','Der Rhein':'Germany'} for river,country in rivers.items():
print('the ' + river + ' runs through ' + country) print('\n')
#3.调查:创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中
#遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与的人,打印一条消息邀请他参与调查 users = {'Mike':'apple','Neo':'banana','Lucy':'strawberry','Jan':'watermelon'}
new_users = ['Mk','Mv','Mike','Lucy','zhangsan'] for name in users.keys():
if name in new_users:
print(name + ',thank you for your cooperation!')
else:
print(name + ',can I invite you for a report?') #课本上例子改进后的程序 favorite_languages = {
'jen':['python','ruby'],
'sarah':['C'],
'edward':['ruby','go'],
'phil':['python','haskell'],
} for name,languages in favorite_languages.items():
if len(languages) <=1:
print('\n' + name.title() + "'s favorite languages is:")
for language in languages:
print("\t" + language.title())
else:
print('\n' + name.title() + "'s favorite languages are:")
for language in languages:
print("\t" + language.title()) print('\n')
#1.朋友:创建三个表示人的字典,然后将这三个字典都储存在一个命名为people的列表里
#遍历列表,并将每个人的信息打印出来
friend1 = {
'last_name':'马',
'first_name':'脑壳',
'age':'',
'live_city':'北京',
}
friend2 = {
'last_name':'梁',
'first_name':'小波',
'age':'',
'live_city':'上海',
}
friend3 = {
'last_name':'罗',
'first_name':'短命',
'age':'',
'live_city':'贵州',
} people = [friend1,friend2,friend3]
for friend in people:
print('姓名:' + friend['last_name'] + friend['first_name'] + '\n' +'年龄:' + friend['age']
+ '\n' + '城市:' + friend['live_city']) print('\n')
#城市:创建一个命名为cities的字典,其中将三个城市作为键;对于每个城市都创建一个字典,并在其中包含该城市
#所属于的国家,人口约数和该城市的事实,并将每座城市的这些信息打印出来
cities = {
'北京':{'country':'China','population':'十万','fact':"It's a beautiful city. "},
'旧金山':{'country':'Americ','population':'二十万','fact':"It's a good place for films."},
'巴黎':{'country':'France','population':'十万','fact':"It’s a romantic place."},
} for city,messages in cities.items():
print('城市:'+ city + '\n' + '属于:' + messages['country'] +'\n' + '人口:'+messages['population']
+ '\n' + '事实:' + messages['fact']) #课本上例子改进后的程序
favorite_languages = {'jen':['python','ruby'],'sarah':['C'],'edward':['ruby','go'],'phil':['python','haskell'],}
for name,languages in favorite_languages.items():if len(languages) <=1:print('\n' + name.title() + "'s favorite languages is:")for language in languages:print("\t" + language.title())else:print('\n' + name.title() + "'s favorite languages are:")for language in languages:print("\t" + language.title())
print('\n')#1.朋友:创建三个表示人的字典,然后将这三个字典都储存在一个命名为people的列表里#遍历列表,并将每个人的信息打印出来friend1 = {'last_name':'马','first_name':'脑壳','age':'22','live_city':'北京',}friend2 = {'last_name':'梁','first_name':'小波','age':'23','live_city':'上海',}friend3 = {'last_name':'罗','first_name':'短命','age':'21','live_city':'贵州',}people = [friend1,friend2,friend3]for friend in people:print('姓名:' + friend['last_name'] + friend['first_name'] + '\n' +'年龄:' + friend['age'] + '\n' + '城市:' + friend['live_city'])
print('\n')#城市:创建一个命名为cities的字典,其中将三个城市作为键;对于每个城市都创建一个字典,并在其中包含该城市#所属于的国家,人口约数和该城市的事实,并将每座城市的这些信息打印出来cities = {'北京':{'country':'China','population':'十万','fact':"It's a beautiful city. "},'旧金山':{'country':'Americ','population':'二十万','fact':"It's a good place for films."},'巴黎':{'country':'France','population':'十万','fact':"It’s a romantic place."},} for city,messages in cities.items():print('城市:'+ city + '\n' + '属于:' + messages['country'] +'\n' + '人口:'+messages['population']+ '\n' + '事实:' + messages['fact'])
Python:从入门到实践--第六章--字典--练习的更多相关文章
- python编程:从入门到实践----第六章>字典
一.一个简单的字典:alien_0存储外星人的颜色和点数,使用print打印出来 alien_0 = {'color': 'green','points': 5} print(alien_0['col ...
- python编程:从入门到实践----第六章:字典>练习
6-1 人:使用一个字典来存储一个熟人的信息,包括名.姓.年龄和居住的城市.该字典应包含键first_name .last_name .age 和city .将存储在该字典中的每项信息都打印出来. f ...
- Python:从入门到实践--第四章--列表操作--练习
#1.想出至少三种你喜欢的水果,将其名称存储在一个列表中,再使用for循环将每种水果的名称都打印出来. #要求:(1)修改这个for循环,使其打印包含名称的句子,而不是仅仅是水果的名称.对于每种水果, ...
- Python:从入门到实践--第三章--列表简介--练习
#1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...
- python 从入门到实践 第三章
在第3章,你将学习如何在被称为列表的变量中存储信息集,以及如何通过遍历列表来操作其中的信息 写注释 # 代码越长 标识好代码的重要性 越来越重要要求习惯:在代码中编写清晰,简洁的注释开始研究更复杂的主 ...
- Python:从入门到实践--第十一章--测试代码--练习
#1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名. #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.将这个函数 #存储在一个名为city ...
- Python:从入门到实践--第五章--if语句--练习
#1.编写一系列条件测试:将每个测试以及结果打印出来 car = '宝马' if car == "宝马": print("预测正确") print(car) e ...
- Python:从入门到实践--第七章--用户输入和while循环-练习
#1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...
- #Python编程从入门到实践#第四章笔记
#Python编程从入门到实践#第四章笔记 操作列表 1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...
随机推荐
- python day1 之三级菜单的正确姿势
看了几个同学有关三级菜单的实现,都是通过一级一级输出,是较为过程的实现.另外如果菜单(树形结构)更多级这样处理起来就比较麻烦了. 可以使用python强大的列表和字典,实现的更优美或简洁一些: 注:复 ...
- c迭代器与生成器
一:迭代器 1.什么是迭代? 1.重复 2.下一次重复是基于上一次的结果 # l=['a','b','c','d'] # count= # while count < len(l): # p ...
- unity中编辑器直接截屏代码
using UnityEngine; using System.Collections; using System.Windows.Forms; public class screenshots : ...
- 用jackson包实现json、对象、Map之间的转换
jackson API的使用 用jackson包实现json.对象.Map之间的转换
- 4ci
- 使用HM16.0对视频编码
1.编译HM16.0源码: 步骤参照:https://www.vcodex.com/hevc-and-vp9-codecs-try-them-yourself/(可设置pq等参数) [编译过程中遇到l ...
- .net正则匹配
char[] weixin = txtweixinhao.Text.Trim().ToCharArray(); for (int i = 0; i < weixin.Length; i++) i ...
- python学习笔记第一节
一.HelloWorld #!/usr/bin/env python #-*- coding:utf-8 -*- print("HelloWorld!") 二.用户交互 #!/us ...
- Hyperledger Fabric 架构梳理
区块链的数据结构 State数据结构 由peer维护,key/value store Ledger 记录了所有成功和不成功的状态更新交易.Ledger被ordering service构造,是一个全 ...
- Linux----Github环境搭建
前面介绍了在Windows环境上安转GitHub环境,原本以为打包成jar,发布到Linux不需要再安转Git,但是因为我们使用了Config-Server配置中心,远程配置来启动,所以需要在Linu ...