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 会一直执行它下面的代码片段,直到它对应的布尔表 ...
随机推荐
- VS2013+Win10+opencv3.0配置(包括opencv2.4.10版本)
在win下配置opencv3.0.0还是比较简单的,这里简单说一下配置过程:参考链接:http://blog.csdn.net/u010009145/article/details/50756751 ...
- eMMC基础技术8:操作模式1-boot mode
1.前言 eMMC总线操作包含: boot mode device identification mode interrupt mode data transfer mode 本文主要描述boot m ...
- 【sky第二期--PID算法】--【智能车论坛】
[sky第二期--PID算法] 想学PID的可以来[智能车论坛]这里有我发布的资料http://bbs.tekbots.eefocus.com/forum.php?mod=viewthread& ...
- 转载:为什么选择Nginx(1.2)《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19610.html 为什么选择Nginx?因为它具有以下特点: (1)更快 这表现在两个方面:一方面,在正常情况下,单次请求会得到更快 ...
- TCP/IP五层层次模型
TCP/IP层次模型共分为五层:应用层HTTP.传输层TCP.网络层IP.数据链路层Data-link.物理层physical.· 应用层-应用层是所有用户所面向的应用程序的统称.IC ...
- java.lang.Math
四舍五人 System.out.println(Math.round(1.8f));//输出位2 static(静态)方法random() //: object/Shifting.java packa ...
- 执行 crontab 的计划任务
新建了一个 Laravel 的计划任务,配置好 crontab * * * * * php /www/sunzhongwei/artisan schedule:run >> /dev/nu ...
- laravel 列表搜索查询(when,with用法以及关联图像id处理图像路径)
laravel中比较常规的列表查询: /** * 活动列表 * @param Request $request * @return \Illuminate\Http\JsonResponse */ p ...
- 步步为营-56-JQuery基础
jQuery本质还是封装好的js,只不过代码更简洁,而做的更好 使用JQuery选择器会返回一个jQuery对象,其本质是dom数组,jQuery对象可以调用JQuery方法. 1 选择器 1.1 基 ...
- swich使用
package demo; import java.util.Scanner; /** * swich(变量){//byte\shore\char\int'枚举(jdk1.5)/String(1.7) ...