题目:

思考循环结构,看看它是怎样运行的,对我们认识程序有何益处。

知识点:

list, for-loop, range

练习代码:

练习1

the_count = [1, 2, 3, 4, 5]

# this first kind of for-loop goes through a list
for number in the_count:
print('This is count %d' % number)

结果:

This is count 1
This is count 2
This is count 3
This is count 4
This is count 5

练习2

fruits = ['apples', 'oranges', 'pears', 'apricots']

for fruit in fruits:
print('A fruit of type: %s' % fruit)

结果:

A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots

练习3

change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
for i in change:
print('I got %r' % i)
# notice we have to use %r since we don't know what's in it.

结果:

I got 1
I got 'pennies'
I got 2
I got 'dimes'
I got 3
I got 'quarters'

练习4

elements = []
for i in range(0, 6):
print('Adding %d to the list.' % i)
elements.append(i)

结果:

Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
for i in elements:
print('Element was: %d' % i)

结果:

Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

来自《笨办法学Python》

Python3练习题系列(02)的更多相关文章

  1. Python3练习题系列(09)——物以类聚,人以群分

    目标: 用类管理同类事物 解析: 用到“class”的编程语言被称作“Object Oriented Programming(面向对象编程)”语言.首先你需要做出“东西”来,然后你“告诉”这些东西去完 ...

  2. Python3练习题系列(10)——项目骨架构建

    目标: 如何创建<项目“骨架”目录> 包含:项目文件布局.自动化测试代码,模组,以及安装脚本. 由于编写一个Python文件可以作为一个模块,一个带__init__.py的目录算一个包. ...

  3. Python3练习题系列(06)——各种符号总结

    Python3中的各种符号总结 1关键字 import keyword print(keyword.kwlist, end='\t') ['False', 'None', 'True', 'and', ...

  4. Python3练习题系列(01)

    2018-06-13 题目: 根据用户回答做出相应的判断,完成一个“回答-判断”的小游戏 Python3知识点: if, else, elif 实例代码: print("You enter ...

  5. Python3练习题系列(07)——列表操作原理

    目标: 理解列表方法的真实含义. 操作: list_1.append(element) ==> append(list_1, element) mystuff.append('hello') 这 ...

  6. Python3练习题系列(08)——代码阅读方法及字典跳转表理解

    问题:分析下面代码 cities['_find'] = find_city city_found = cities['_find'](cities, state) 分析过程: 一个函数也可以作为一个变 ...

  7. Python3练习题系列(05)——设计和调试规则

    If 语句的常见规则 1. 每一个“if 语句”必须包含一个else: 2. 如果这个else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做die 的函数,让 ...

  8. Python3练习题系列(04)

    题目: 制作一个游戏 知识点: 函数.if_elif_else, while, exit 游戏图谱: 游戏代码: from sys import exit def gold_room(): print ...

  9. Python3练习题系列(03)

    题目: 思考While循环,看看它的特点是什么? 知识点: while循环 分析: 特点:while-loop(while 循环).while-loop 会一直执行它下面的代码片段,直到它对应的布尔表 ...

随机推荐

  1. Software development skills for data scientists

    Software development skills for data scientists Data scientists often come from diverse backgrounds ...

  2. 优秀的gdb图形化前端调试器

    目前我自己最喜欢的还是 ddd . gdbgui 和 vim-vebugger插件或vimgdb插件 三种. You could try using Insight a graphical front ...

  3. ES系列九、ES优化聚合查询之深度优先和广度优先

    1.优化聚合查询示例 假设我们现在有一些关于电影的数据集,每条数据里面会有一个数组类型的字段存储表演该电影的所有演员的名字. { "actors" : [ "Fred J ...

  4. Android:注册登录

    注册登录的实现 先在layout里新建一个xml文件: //login.xml <?xml version="1.0" encoding="utf-8"? ...

  5. 使用console进行 性能测试 和 计算代码运行时间

    原文:http://www.tuicool.com/articles/JrARVjv 对于前端开发人员,在开发过程中经常需要监控某些表达式或变量的值,如果使用用 debugger 会显得过于笨重,最常 ...

  6. SpringMVC(4.2):Controller接口控制器详解(2)

    原文出处: 张开涛 4.5.ServletForwardingController 将接收到的请求转发到一个命名的servlet,具体示例如下: package cn.javass.chapter4. ...

  7. CentOS中在/etc/rc.local添加开机自启动项启动失败

    应项目要求需要在开机的时候启动自己的Agent程序,想当然的直接就往/etc/rc.local当中添加启动命令,结果重启之后发现什么都没有发生....一开始还以为是Python路径的问题,结果改成绝对 ...

  8. cf1061D 贪心+multiset 好题!

    cf上的思维题真好! 本题是在模拟的基础上贪心即可:将n段时间按照左端点(右端点为第二关键字)从小到大排序,然后遍历每一个时间段. 对于每一个时间段[li,ri],先找到multiset中最靠近li但 ...

  9. StackOverflowError的原因

    package chapter04; /** 如果两个方法出现互相调用的时候会出现StackOverflowError*/ public class C06_Method { public stati ...

  10. JavaScript常见的真值

    值 说明 var a =true  值等于true: var a = 1 非0的数字 var a =“hello” 有内容的字符串 var a=20/5 运算结果非0 var a='true' 有内容 ...