习题38 列表的操作

# create a mapping of state to an abbreviation
states = {'Oregon': 'OR','Florida': 'FL','California': 'CA','New York': 'NY','Michigan': 'MI'
}# create a basic set of states and some cities in them
cities = {'CA': 'San Francisco','MI': 'Detroit','FL': 'Jacksonville'
}# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Poland'# print out some cities
print('-' * 10)
print("NY State has: ", cities['NY'])
print("OR State has: ", cities['OR'])# print some states
print('-' * 10)
print("Michigan's abbreviation is: ", states['Michigan'])
print("Florida's abbreviation is: ", states['Florida'])# do it by using the state then cities dict
print('-' * 10)
print("Michigan has: ", cities[states['Michigan']])
print("Florida has: ", cities[states['Florida']])# print every state abbreviation
print('-' * 10)
for state, abbrev in states.items():print("%s is abbreviated %s" % (state, abbrev))# print every city in state
print('-' * 10)
for abbrev, city in cities.items():print("%s has the city %s" % (abbrev, city))# now do both at the same time
print('-' * 10)
for state, abbrev in states.items():print("%s state is abbreviated %s and has city %s" % (state, abbrev, cities[abbrev]))print('-' * 10)
# safely get a abbreviation by state that might not be there
state = states.get('Texas', None)if not state:print("Sorry, no Texas.")# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print("The city for the state 'TX' is: %s" % city)

习题39 字典,可爱的字典

class Song(object):def __init__(self, lyrics):self.lyrics = lyricsdef sing_me_a_song(self):for line in self.lyrics:print(line)happy_birthday = Song(["Happy birthday to you", "I don't want to get sued", "So I'll stop right there"])bulls_on_parade = Song(["They really around the family", "With pockets full of shells"])happy_birthday.sing_me_a_song()bulls_on_parade.sing_me_a_song()

笨办法学Python3 习题39和习题40相关推荐

  1. 《笨办法学python3》再笨的人都能学会python,附PDF,拿走不谢

    <笨办法学python3>这本书的最终目标是让你起步python编程,虽然说是用"笨办法"学习写程序,但是其实并不是这样的. 所谓的"笨办法"就是指 ...

  2. 读书笔记 笨办法学python3

    读后评价 <笨办法学Python3>是一本适合Python3的初学者实用性书籍,以52个练习题为章节讲述了Python3的基础知识,包括基本语法.控制流程.列表.字典.元组.面向对象(类, ...

  3. 笨办法学python3在线阅读知乎_Python第三课——笨办法学Python

    需要学习资料的请直接看文末 <笨办法学Python>(Learn Python The Hard Way,简称LPTHW)是Zed Shaw编写的一本Python入门书籍.适合对计算机了解 ...

  4. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...

  5. 笨办法学python3.6 pdf_“笨办法”学Python3.pdf

    作 者 :(美)泽德 A.肖(Zed A. Shaw)著 出版发行 : 北京:人民邮电出版社 , 2018.06 ISBN号 :978-7-115-47881-8 页 数 : 255 原书定价 : 3 ...

  6. 笨办法学python3进阶篇下载_笨办法学Python 3 进阶篇

    书名:"笨办法"学Python 3:进阶篇 定价:59.0 ISBN:9787115505392 作者: 泽德·A. 肖 版次:第1版 出版时间:2020-06 内容提要: 本书是 ...

  7. 笨办法学python3进阶篇_笨办法学Python 3 进阶篇

    第 一部分 准备知识 1 如果不喜欢作者的个人流程怎么办 2 如果发现自己太糟糕怎么办 2 习题0 准备工作 3 程序员用的编辑器 3 Python 3.6 3 工作终端 4 pip和virtuale ...

  8. 笨办法学python3进阶篇下载_笨办法学Python 3:进阶篇

    第 一部分 准备知识 1 如果不喜欢作者的个人流程怎么办 2 如果发现自己太糟糕怎么办 2 习题0 准备工作 3 程序员用的编辑器 3 Python 3.6 3 工作终端 4 pip和virtuale ...

  9. 笨办法学Python3 习题16

    基于Python3.6 习题16 from sys import argvscript, filename = argvprint("We're going to erase %r.&quo ...

最新文章

  1. python 打包 .app 运行 控制台窗口_Python打包工具
  2. Python Module_os_操作系统
  3. 关于Eclipse创建Android项目时,会多出一个appcompat_v7的问题
  4. JS过滤表单数据中的特殊字符
  5. 机器学习:正负样本数据量不平衡处理方法
  6. NodeJS学习笔记—1.CommonJS规范
  7. [Docker]Docker拉取,上传镜像到Harbor仓库
  8. 第一百七十三节,jQuery,Ajax
  9. 阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第3节 综合案例_文件上传_5_综合案例_文件上传案例优化...
  10. WebService技术入门
  11. matlab抛物柱面画图,抛物柱面 - calculus的日志 - 网易博客
  12. python好找工作吗2017-2017年 Python工程师面试经历分享(七家)
  13. 17.3.13 多任务学习 Multi-task learning
  14. 4pycharm与jupyter使用对比
  15. Android 获取定位权限,获取位置信息(国家、地区、经纬)
  16. HTML结构及常用的标签
  17. 人在旅途——》张家界之旅:20180418
  18. 计算机软考软件设计师2019试题,最新2018年上半年软件设计师真题+答案解析上午选择+下午案例完整版(全国计算机软考)...
  19. 跨境电商如何做好客户沟通?
  20. 树莓派设置WiFi上网

热门文章

  1. 创建一个不带头结点的循环单链表并进行相关操作
  2. 几款心电ECG芯片ADS1292 KS1081 AD8232 BDM101的简单比较
  3. GPS定位模块返回数据的处理
  4. 一维振动信号的时域特征python
  5. 在线问答未来会朝什么趋势发展?
  6. 数据结构与算法(C++实现)
  7. 查看zookeeper状态报错`Error contacting service. It is propably not running`
  8. C语言 BMP图片的中值滤波
  9. win7 计算机 开不了,win7系统开不了机怎么办?云骑士教你解决开不了机的问题...
  10. java struts2 漏洞_struts2漏洞原理及解决办法