题目:

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

知识点:

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. 【转】Linux中包管理与定时任务

    [转]Linux中包管理与定时任务 第1章 软件查询 1.1 查询软件是否安装 rpm -qa |grep cron 查询是否安装了这个软件. [root@znix ~]# rpm -qa |grep ...

  2. Django 聚合与查询集API实现侧边栏

    本文从Django官方文档总结而来,将聚合的主要用法和查询集的常见方法做一归纳. 聚合 1. 聚合的产生来源于django数据库查询,通常我们使用django查询来完成增删查改,但是有时候需要更复杂的 ...

  3. Libevent源码分析—从使用Libevent库开始

    练习libevent库的使用,主要是几个API的调用顺序.根据event.h的开头注释部分可知,要使用libevent库,主要的几个API及调用顺序为:         event_base()初始化 ...

  4. scp -r拷贝目录不会拷贝软连接

    scp -r拷贝目录,不会拷贝 软连接的 解决方法: 使用rsync拷贝 参考:rsync本地及远程复制备份[原创] - paul_hch - 博客园 https://www.cnblogs.com/ ...

  5. ES系列十八、FileBeat发送日志到logstash、ES、多个output过滤配置

    一.FileBeat基本概念 简单概述 最近在了解ELK做日志采集相关的内容,这篇文章主要讲解通过filebeat来实现日志的收集.日志采集的工具有很多种,如fluentd, flume, logst ...

  6. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061),mysql服务已启动

    1 前言 在mysql服务已启动,用命令行进入或者heidisql工具都提示ERROR 2003 (HY000): Can't connect to MySQL server on 'localhos ...

  7. sklearn调参(验证曲线,可视化不同参数下交叉验证得分)

     一 . 原始方法: 思路: 1. 参数从 0+∞ 的一个 区间 取点, 方法如: np.logspace(-10, 0, 10) , np.logspace(-6, -1, 5) 2. 循环调用cr ...

  8. python----面向对象(2)

    反射 class ClassA: name = 'xiaoming' def __init__(self): self.y = 'y' #先执行 __setattr__ 在执行__getattribu ...

  9. DFMZ-开发过程中遇到的错误-01

    未能加载文件或程序集“H2F, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载格式不正确的程序. 原因分析:由 ...

  10. JDBC事务,银行转账,货物进出库等等。

    1:转账业务 转账必须执行2个sql语句(update更新)都成功的情况下,提交事务,如果有一个失败,则2个都回滚事务2:事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACI ...