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 会一直执行它下面的代码片段,直到它对应的布尔表 ...
随机推荐
- DES加密模式详解
DES加密模式详解 http://www.cnblogs.com/Lawson/archive/2012/05/20/2510781.html http://www.blogjava.net/wayn ...
- select 不要 用*
背景 说实在的,这有什么好记录的呢.记录这个有啥用,真是技术人员的吹毛求疵.说起来,就是给人装有用吧.既然记录了,也想个相关的段子吧.曾经有个同事写了个sql,效率极差,来了个女同事,竟然解决了,问题 ...
- PHP框架CodeIgniter--URL去除index.php
今天学习CodeIgniter简称CI的第一天,记录下学习心得. CI中国https://codeigniter.org.cn/user_guide/general/urls.html?highlig ...
- Windows不能在本地计算机启动MongoDB,错误代码 100
今天在计算机上面启动MongoDB时,直接给我报错,Windows不能在本地计算机启动MongoDB,服务错误代码 100. 这种问题解决方法是: 找到data文件夹db下面的mongod.lock文 ...
- 【python】中文提取,判断,分词
参考: http://www.cnblogs.com/kaituorensheng/p/3595879.html https://github.com/fxsjy/jieba 判断是否包含中文 def ...
- window 连linux
https://blog.csdn.net/ruanjianruanjianruan/article/details/46954681 https://blog.csdn.net/u013754317 ...
- ubuntu 电源管理
https://www.cnblogs.com/sky-heaven/p/4561374.html?tdsourcetag=s_pcqq_aiomsg 挂起命令 echo mem > /sys ...
- laravel 事件广播
Laravel 5.1 之中新加入了事件广播的功能,作用是把服务器中触发的事件通过websocket服务通知客户端,也就是浏览器,客户端js根据接受到的事件,做出相应动作.本文会用简单的代码展示一个事 ...
- hdu5646数学构造+二分
/* 满足n>=(k+1)*k/2的整数n必定满足 a+(a+1)+...+(a+k-1)<=n<=(a+1)+(a+2)+...+(a+k) 只要在[a,a+k]中减掉一个数字ai ...
- hdu4578线段树区间更新
/* 只有在区间中的数字不相同时才pushdown:往子区间传递数字再到子区间更新,同时该区间的flag置0 更新完左右子区间后进行pushup,如果左右子区间数字相同,那么把子区间合并,子区间数字置 ...