For循环

是迭代对象元素的常用方法
具有可迭代方法的任何对象都可以在for循环中使用。
python的一个独特功能是代码块不被{} 或begin,end包围。
相反,python使用缩进,块内的行必须通过制表符缩进,或相对于周围的命令缩进4个空格。

for number in [23, 41, 12, 16, 7]:print(number)
print('Hi')

结果:

1、枚举
枚举函数返回一个元组,其中包含每次迭代的计数(从默认为0的开始)和迭代序列获得的值

friends = ['steve', 'rachel', 'michael', 'adam', 'monica']
for index, friend in enumerate(friends):print(index,friend)

结果:

练习一个题:

#将这段话的标点符号去掉,并且把它分开到列表中
text='''On a dark desert highway, cool wind in my hair Warm smell of colitas,
rising up through the air Up ahead in the distance,
I saw a shimmering light My head grew heavy and my sight grew dim I had to stop
for the night There she stood in the doorway; I heard the mission bell And I was
thinking to myself, "This could be Heaven or this could be Hell" Then she lit up
a candle and she showed me the way'''for a in ['-',',',';','\"']:text=text.replace(a,' ')#用空格代替print(text)text=text.split(' ')[0:100]#用空格给分开
print(text)

结果:

2、len(’’)

#将这段话的标点符号去掉,并且把它分开到列表中
text='''On a dark desert highway, cool wind in my hair Warm smell of colitas,
rising up through the air Up ahead in the distance,
I saw a shimmering light My head grew heavy and my sight grew dim I had to stop
for the night There she stood in the doorway; I heard the mission bell And I was
thinking to myself, "This could be Heaven or this could be Hell" Then she lit up
a candle and she showed me the way'''for a in ['-',',',';','\"']:text=text.replace(a,' ')#用空格代替list=[]
for a in text.split(' '):if len(a)>0:#如果提取出来的长度大于0就放进列表中list.append(a)print(list[0:20])

结果:

3、Continue
continue语句将转到循环的下一次迭代
continue语句用于忽略某些值,但不会中断循环

for a in ['-',',',';','\"']:text=text.replace(a,' ')#用空格代替list=[]
for a in text.split(' '):if len(a)==0:#如果提取出来的=0就下一次循环continuelist.append(a)print(list[0:20])

结果:

4、Break
break语句将完全打断循环

#将这段话的标点符号去掉,并且把它分开到列表中
text='''On a dark desert highway, cool wind in my hair Warm smell of colitas,
rising up through the air Up ahead in the distance,
I saw a shimmering light My head grew heavy and my sight grew dim I had to stop
for the night There she stood in the doorway; I heard the mission bell And I was
thinking to myself, "This could be Heaven or this could be Hell" Then she lit up
a candle and she showed me the way'''for a in ['-',',',';','\"']:text=text.replace(a,' ')#用空格代替list=[]
for a in text.split(' '):if len(a)==0:breaklist.append(a)print(list[0:20])

结果:

练习:

编写一个Python程序,它迭代整数从1到50(使用for循环)。对于偶数的整数,将其附加到列表even_numbers。对于奇数的整数,将其附加到奇数奇数列表中。

提示:range(1,51)代表1-50取整数

even_number=[]#偶数列表
odd_number=[]#奇数列表for a in range(1,51):if a%2==0:even_number.append(a)else:odd_number.append(a)print(even_number)
print(odd_number)

结果:

[Python]学习笔记5——For循环相关推荐

  1. Python学习笔记(五)—LOOP 循环

    个人认为Python基础学习直到LOOP才算真正开始. 循环有While, do-while, 和for() 比如while 我们要输出100条"you are my sunshine &q ...

  2. Python学习笔记之While循环(二)

    使用while循环来处理列表和字典,通过将while循环同列表和字典结合起来使用,可收集.存储并组织大量输入,供以后查看和显示. 1.在列表之间移动元素,有时候,往往需要从另外一个列表移动到另外一个列 ...

  3. Python学习笔记之While循环(一)

    1.while循环简介:for循环用于针对集合中的每个元素都一个代码块,而while循环不断地运行,直到指定的条件不满足为止. 2.一个简单的while循环例子,很简单,小于10时循环,到number ...

  4. 潭州教育-Python学习笔记@条件与循环

    第一部分:条件语句 Python中条件语句由if,elif,else等控制,当if满足条件时执行相应代码块,不满足再判断是否满足elif条件,还不满足就执行else代码块 score = input( ...

  5. python里while的用法_Python学习笔记之While循环用法分析

    本文实例讲述了Python学习笔记之While循环用法.分享给大家供大家参考,具体如下: 前面一篇<Python学习笔记之For循环用法>详细介绍了Python for循环,这里再来讲述一 ...

  6. python中while的用法_Python学习笔记之While循环用法分析

    本文实例讲述了Python学习笔记之While循环用法.分享给大家供大家参考,具体如下: 前面一篇<Python学习笔记之For循环用法>详细介绍了Python for循环,这里再来讲述一 ...

  7. Python学习笔记 - 探索while无限迭代循环

    大家好,我是Mr数据杨,都知道Python的美在于它的简洁与实用性,就像<三国演义>里的诸葛亮七擒孟获一样.而今天,我将带领大家深入到这个美丽且富有魅力的Python世界. 想象一下Pyt ...

  8. while用法python_Python学习笔记之While循环用法分析

    本文实例讲述了Python学习笔记之While循环用法.分享给大家供大家参考,具体如下: 前面一篇<Python学习笔记之For循环用法>详细介绍了Python for循环,这里再来讲述一 ...

  9. python学习笔记(15)循环设计

    python学习笔记(15)循环设计 原链:http://www.cnblogs.com/vamei/archive/2012/07/09/2582435.html 注意:zip()在python2 ...

最新文章

  1. python sanic_sanic中文文档
  2. Kanzi常用操作1
  3. 用这种方式,我每次都是朋友圈里第一个预见AI科技趋势的人
  4. Appium下载安装及环境配置
  5. 计算机图形学与相关学科的关系,哈尔滨工业2015博士招生计算机图形学与人机交互设计大纲...
  6. SAP S/4HANA 的30天免费试用版
  7. 安卓dalvik和art区别
  8. web 服务器 内存 影响_C/C++服务器开发常用的7大开源库,让你在同行中脱颖而出...
  9. 【聚会】2014圣诞北京版主聚餐-淘虾记“光辉岁月”版
  10. html设置隐藏窗口,html – 在窗口大小调整时逐个隐藏菜单项
  11. Win10 PowerToys官方免费效率小工具集
  12. 涉密计算机清退登记表,涉密载体登记表.doc
  13. sprint 周期总结
  14. NetBeans的下载、安装
  15. 马步站桩-快速健身法 程序员朋友们 注意身体
  16. 一封来自 1985 年程序员的辞职信
  17. walking机器人仿真教程-查看仿真环境相关话题
  18. 量化股票查询代码是什么?
  19. Linux 与 Python编程2021 经典函数实例 educoder实训
  20. AD通过图表符链接多个原理图

热门文章

  1. IFE 斌斌学院(js)
  2. React之生命周期-forceUpdate
  3. 游戏本天梯_给新生的游戏本推荐指南
  4. 前端效果积累 |别具一格的3D酷炫轮播图效果收集
  5. Python、C/C++混编实现最短路径可视化—Dijkstra算法
  6. 区块链的跨链技术介绍完整版
  7. gtp6 linux 启动_gtp6
  8. Fabric创建通道流程解析
  9. 如何将西安80坐标转换为国家2000(或WGS84)坐标系?
  10. android 老年机系统,打造最实用的老年机:安卓篇