#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:从入门到实践--第六章--字典--练习的更多相关文章

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

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

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

    6-1 人:使用一个字典来存储一个熟人的信息,包括名.姓.年龄和居住的城市.该字典应包含键first_name .last_name .age 和city .将存储在该字典中的每项信息都打印出来. f ...

  3. Python:从入门到实践--第四章--列表操作--练习

    #1.想出至少三种你喜欢的水果,将其名称存储在一个列表中,再使用for循环将每种水果的名称都打印出来. #要求:(1)修改这个for循环,使其打印包含名称的句子,而不是仅仅是水果的名称.对于每种水果, ...

  4. Python:从入门到实践--第三章--列表简介--练习

    #1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...

  5. python 从入门到实践 第三章

    在第3章,你将学习如何在被称为列表的变量中存储信息集,以及如何通过遍历列表来操作其中的信息 写注释 # 代码越长 标识好代码的重要性 越来越重要要求习惯:在代码中编写清晰,简洁的注释开始研究更复杂的主 ...

  6. Python:从入门到实践--第十一章--测试代码--练习

    #1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名. #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.将这个函数 #存储在一个名为city ...

  7. Python:从入门到实践--第五章--if语句--练习

    #1.编写一系列条件测试:将每个测试以及结果打印出来 car = '宝马' if car == "宝马": print("预测正确") print(car) e ...

  8. Python:从入门到实践--第七章--用户输入和while循环-练习

    #1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...

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

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

随机推荐

  1. Spring Boot:Thymeleaf 使用详解

    Thymeleaf 介绍 简单说,Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1 ...

  2. linux IPtable防火墙 禁止和开放端口

    1.关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放.下面是命令实现: iptables -P INPUT DROPiptables -P FORWARD DROPiptable ...

  3. Jsp的table导出Excel

    var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.in ...

  4. [数]补题ver.

    上次补题好像把两次训练混在一起了,总之先按时间顺序补完这一次|ू・ω・` ) HDU-6301 不会的东西不能逃避.jpg 红小豆非常讨厌构造题,因为非常不会,并且非常逃避学习这类题,因为总也搞不清楚 ...

  5. Unable to determine the device handle for GPU 0000:01:00.0: GPU is lost.问题排坑

    在运行maskrcnn时,会碰到训练不动的问题,就卡在这儿 UserWarning: Converting sparse IndexedSlices to a dense Tensor of unkn ...

  6. Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load

    Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load 2018-06-07 00:514730原创GoldenGate 作者: leo 本文链接:https://w ...

  7. 14. Longest Common Prefix ★

    题目内容:Write a function to find the longest common prefix string amongst an array of strings 题目分析:本题目利 ...

  8. 【转】git - 简易指南

    原文链接:http://www.bootcss.com/p/git-guide/ 作者:罗杰·杜德勒 感谢:@tfnico, @fhd and Namics 其他语言 english, deutsch ...

  9. Excel--------实用功能(数据对比)

    --excel数据在sql中查询展示出来 SELECT * FROM (SELECT '101001' as code ,'上海宝山站' as name union allSELECT '102083 ...

  10. 《JavaScript Dom 编程艺术》读书笔记-第11章

    本章简单介绍了HTML5,并推荐了一个好工具Modernizr,用于检测浏览器可能支持的各种特性. HTML5的新特性包括: 可以用来在文档中绘制矢量及位图的<canvas>元素: 可以在 ...