书中代码:
这节习题,我们将使用 for循环来创建并打印一些列表。使用 for 循环之前,你需要在某个位置存放循环的结果。最好的方法是使用列表(list),顾名思义,列表就是一个按顺序存放东西的容器。
书中代码:

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']# this first kind of for-loop goes through a list
for number in the_count:print "This is count %d" % number# same as above
for fruit in fruits:print "A fruit of type: %s" % fruit# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:print "I got %r" % i# we can also build lists, first start with an empty one
elements = []# then use the range function to do 0 to 5 counts
for i in range(0, 6):print "Adding %d to the list." % i# append is a function that lists understandelements.append(i)# now we can print them out too
for i in elements:print "Element was: %d" % i

代码修改为:

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']# this first kind of for-loop goes through a list
for number in the_count:print("This is count %d " % number)# same as above
for fruit in fruits:print("A fruit of type: %s" % fruit)# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:print("I got %r" % i)# we can also build lists, first start with an empty one
elements = []# then use the range function to do 0 to 5 counts
for i in range(0, 6):print("Adding %d to the list." % i)# append is a function that lists understandelements.append(i)# now we can print them out too
for i in elements:print("Element was: %d" % i)

输出结果:

附加题:
1、 注意一下 range 的用法。查一下 range 函数并理解它。
参考http://www.runoob.com/python/python-func-range.html
2、在第 22 行,你是否可以直接将 elements 赋值为 range(0,6) ,而无需使用 for 循环?
可以

python系统学习日记 L32 循环和列表相关推荐

  1. python系统学习日记 L13 参数, 解包, 变量

    书中代码: from sys import argvscript, first, second, third = argvprint "The script is called:" ...

  2. python系统学习日记 L22~L24 练习

    L22~L23:无 L24: 书中代码: print "Let's practice everything." print 'You\'d need to know \'bout ...

  3. python系统学习日记 L26 修正改错

    书中L26是改错习题,对一段有问题的代码进行改正 书中代码: def break_words(stuff):"""This function will break up ...

  4. Python编程语言学习:for循环中常用方法经验技巧(利用enumerate函数对列表实现自带索引等)之详细攻略

    Python编程语言学习:for循环中常用方法经验技巧(利用enumerate函数对列表实现自带索引等)之详细攻略 目录 for循环中常用方法经验技巧 1.利用enumerate函数对列表实现for循 ...

  5. python自学流程-Python系统学习流程图,教你一步步学习python

    对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...

  6. 用python设计一个管理系统思路_Python大佬一个月打造的Python系统学习流程图!

    对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...

  7. python逻辑运算的一些流程图_Python大牛历时一个月打造的Python系统学习流程图,超详细!...

    对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...

  8. 13位Python大牛历时一个月打造的Python系统学习流程图,超详细!

    对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! #Pyth ...

  9. python逻辑流程图_Python大牛一个月打造的Python系统学习流程图!

    对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...

最新文章

  1. beautifulsoup里面的find()和findall()小代码测试
  2. 【转】[C# 基础知识系列]专题四:事件揭秘
  3. java no cache_java – 在我的部署中设置Cache-Control no-cache,no-store是什么?
  4. Python.exe下载太慢怎么办?
  5. linux java进程消失_Linux系统下的Java进程无故消失怎么办?
  6. geth 以太坊钱包_以太坊Geth节点RPC API中文文档
  7. 树莓派能直接运行python程序_树莓派怎么运行python程序
  8. java 通道 双向原理_Java-NIO(四):通道(Channel)的原理与获取
  9. python绘制如下图形、小三角形边长20_python二级操作题与分析(7)
  10. 【嵌入式工程师面试高频问题】你知道SPI吗
  11. CentOS/Ubuntu制作自动安装arm iso镜像
  12. python生成json接口_基于python的Flask框架写json接口并且结合eolinker进行接口测试
  13. centos6.4 安装wireless驱动
  14. 关于android中的armeabi、armeabi-v7a、arm64-v8a及x86等用splits用指定打包
  15. [ERP]ERP原理与应用试题(附答案)
  16. 数量积、向量积、混合积
  17. 米兰理工计算机博士申请,意大利博士申请:米兰理工大学16个博士/博士后项目招生中~...
  18. Unity制作卡牌游戏
  19. Linux CentOS 系统实战笔记-基础篇
  20. YCbCr(YUV)与RGB转换公式以及常用颜色的yuv值

热门文章

  1. JavaScript入门教学
  2. 谈谈游戏动画的一些基础知识
  3. VBox虚拟机Ubuntu共享文件夹设置自动挂载
  4. 关于 《IT 专业应该怎么学》
  5. Default Boot Device Missing or Boot Failed解决方法
  6. confirm的意思中文翻译_confirm是什么意思_confirm的翻译_音标_读音_用法_例句_爱词霸在线词典...
  7. JavaScript之form(表单)
  8. 上海贝尔无线猫破解 电信“我的e家”无线猫路由上网破解全教程
  9. elasticsearch 基础介绍及使用 (high-level-client)
  10. 天助网教你如何培养气场