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 会一直执行它下面的代码片段,直到它对应的布尔表 ...
随机推荐
- 【转】Linux中包管理与定时任务
[转]Linux中包管理与定时任务 第1章 软件查询 1.1 查询软件是否安装 rpm -qa |grep cron 查询是否安装了这个软件. [root@znix ~]# rpm -qa |grep ...
- Pycharm 字体大小调整
Pycharm 字体大小调整 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/awyyauqpmy/article/details/79334496P ...
- k64 datasheet学习笔记1---概述
1.前言 k64 datasheet描述了Freescale MCU的特性.架构和编程模型,主要是面向使用MCU的系统架构和软件应用开发人员. 2.模块划分 datasheet主要按功能对模块进行划分 ...
- u盘的一些理解
U盘是由主控板+FLASH+外壳组成的,当主控板焊接上空白FLASH后插入电脑,因为没有相应的数据, 量产工具 电脑只能识别到主控板,而无法识别到FLASH,所以这时候电脑上显示出U盘盘符,但是双击 ...
- centos重启报错Umounting file systems:umount:/opt:device is busy
系统重启报错: Umounting file systems:umount:/opt:device is busy 只能硬关机,回想一下最近刚安装了nod32 for linux x64的杀毒软件,开 ...
- 使用Eclipse创建Web Services
正文: 项目源文件: 百度云盘/博客园/project/wsServerExample/wsServerExample.rar 参考文献: http://www.ibm.com/developerwo ...
- Makefile 隐含规则,模式规则,常见变量
隐含规则复杂的Makefile一般会使用隐含规则内的变量来简化编译处理.将隐含规则中使用的变量分成两种:一种是命令相关的,如“CC”:一种是参数相关的,如“CFLAGS”.这些变量都是大写表示. 常 ...
- Windows Mac地址伪装步骤
本文介绍Windows上Mac地址修改方法,适用于网络环境绑定了Mac地址需要修改上网的情况. 工具/原料 PC电脑一台 Windows系统 方法/步骤 点击右下角图标. 点击打开网络和共享中心. 点 ...
- java 运算符 与(&)、非(~)、或(|)、异或(^)逻辑操作符 与(&&) 或(||) 非(!)
按位与&: 只要对应的二个二进位都为1时,结果位就为1 按位或|:只要对应的二个二进位有一个为1时,结果位就为1 按位异或^:0⊕0=0,1⊕0=1,0⊕1=1,1⊕1=0(同为0,异为1) ...
- python接口自动化测试十七:使用bs4框架进行简单的爬虫
安装:beautifulsoup4 from bs4 import BeautifulSoup yoyo = open('yoyo.html', 'r') # 以读的方式打开“yoyo.html” ...