Python3练习题系列(02)
题目:
思考循环结构,看看它是怎样运行的,对我们认识程序有何益处。
知识点:
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)的更多相关文章
- Python3练习题系列(09)——物以类聚,人以群分
目标: 用类管理同类事物 解析: 用到“class”的编程语言被称作“Object Oriented Programming(面向对象编程)”语言.首先你需要做出“东西”来,然后你“告诉”这些东西去完 ...
- Python3练习题系列(10)——项目骨架构建
目标: 如何创建<项目“骨架”目录> 包含:项目文件布局.自动化测试代码,模组,以及安装脚本. 由于编写一个Python文件可以作为一个模块,一个带__init__.py的目录算一个包. ...
- Python3练习题系列(06)——各种符号总结
Python3中的各种符号总结 1关键字 import keyword print(keyword.kwlist, end='\t') ['False', 'None', 'True', 'and', ...
- Python3练习题系列(01)
2018-06-13 题目: 根据用户回答做出相应的判断,完成一个“回答-判断”的小游戏 Python3知识点: if, else, elif 实例代码: print("You enter ...
- Python3练习题系列(07)——列表操作原理
目标: 理解列表方法的真实含义. 操作: list_1.append(element) ==> append(list_1, element) mystuff.append('hello') 这 ...
- Python3练习题系列(08)——代码阅读方法及字典跳转表理解
问题:分析下面代码 cities['_find'] = find_city city_found = cities['_find'](cities, state) 分析过程: 一个函数也可以作为一个变 ...
- Python3练习题系列(05)——设计和调试规则
If 语句的常见规则 1. 每一个“if 语句”必须包含一个else: 2. 如果这个else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做die 的函数,让 ...
- Python3练习题系列(04)
题目: 制作一个游戏 知识点: 函数.if_elif_else, while, exit 游戏图谱: 游戏代码: from sys import exit def gold_room(): print ...
- Python3练习题系列(03)
题目: 思考While循环,看看它的特点是什么? 知识点: while循环 分析: 特点:while-loop(while 循环).while-loop 会一直执行它下面的代码片段,直到它对应的布尔表 ...
随机推荐
- 转载CSDN博客步骤
在参考“如何快速转载CSDN中的博客”后,由于自己不懂html以及markdown相关知识,所以花了一些时间来弄明白怎么转载博客,以下为转载CSDN博客步骤和一些知识小笔记. 参考博客原址:http: ...
- Pytorch 资料汇总(持续更新)
1. Pytorch 论坛/网站 PyTorch 中文网 python优先的深度学习框架 Pytorch中文文档 Pythrch-CN文档地址 PyTorch 基礎篇 2. Pytorch 书籍 深度 ...
- ES系列一、CentOS7安装ES 6.3.1、集成IK分词器
Elasticsearch 6.3.1 地址: wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3. ...
- VS "以下文件中的行尾不一致,要将行尾标准化吗?"
原文地址:http://www.cnblogs.com/yymn/p/6852857.html 这是由Windows和Unix不同的标准引起的...即“回车”和“换行”的问题... “回车”和“换行” ...
- centos7和centos6.5环境rpm方式安装mysql5.7和mysql5.6详解
centos环境安装mysql5.7 其实不建议安装mysql5.7 语法和配置可能和以前的版本区别较大,多坑,慎入 1.yum方式安装(不推荐) a.安装mysql5.7 yum源 centos6: ...
- nagios系列(三)之nagios被动监控模式之添加系统负载load、swap、磁盘iostat及memory内存监控详解
环境: nagios server:192.168.8.42 host_name:node4.chinasoft.com nagios client:192.168.8.41 host_name:no ...
- 安装xcache3.0.3/3.2,为php加速
安装xcache,为php加速 1.安装 # tar xf xcache-3.0.3.tar.bz2 # cd xcache-3.0.3 # /usr/local/php/bin/phpize # ...
- (三)使用CXF开发WebService客户端
前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程: 首先建一个Maven项目,项目名字,WS_Client: 然后我们要用CXF给我们 ...
- 集合List和ArrayList的示例
package chapter09; import java.util.ArrayList;import java.util.List; /* * List * ArrayList底层是数组 * 特点 ...
- Myeclipse调试模式下自动提示变量值设置
1.Window->Preferences->Java->Editor->Hovers 将[Variable Values]选择即可,如果第一个[Combined Hover] ...