Python入门教程-13 for-in
2015-07-22 20:25
615 查看
1. 循环
for-in是Python中表示循环的其中一种方法。2. 基本语法
示例:>>> values = ['first', 'second', 'third'] >>> for value in values: ... print value ... first second third >>>
for循环对于列表中的数据类型是不关注的,比如list存放的是tuple这种类型:
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> values = [('Tom', 23), ('John', 18), ('Ann', 20)] >>> for value in values: ... name, age = value ... info = "name=%s, age=%d" % (name, age) ... print(info) # in Python 3.4 ... name=Tom, age=23 name=John, age=18 name=Ann, age=20 >>>
3. break和continue
python的循环体中也可以使用break和continue语句,功能和语法和C/C++一样。>>> items = range(1, 20, 2) >>> items [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] >>> for item in items: ... if item % 3 == 0: pass ... print item ... if item >= 17: break ... 1 3 5 7 9 11 13 15 17 >>>
相关文章推荐
- windows下搞个python环境
- Differences between Python2 and Python3
- Python操作Word批量生成文章
- 【整理】详解Python中re.sub
- python基础教程总结10——文件
- python reshape()函数ValueError: total size of new array must be unchanged
- python学习笔记--随时更新
- Python文件操作读写文件
- Python 文件处理
- python中查找指定文件
- python获取当前时间
- Python学习笔记之语法(二)
- Python之dir()与__dict__的区别
- [Python]列举出目录中某类文件的路径
- 傅立叶外推算法的python实现和缺点
- Python文件操作之open()的mode
- python setDaemon
- python-minidom模块【解析xml】
- 【简易Python爬虫】 初试爬虫_简易Python图片爬虫实现
- Python csv模块学习