break

终止整个循环:当循环或判断执行到break语句时,即使判断条件为True或者序列尚未完全被历遍,都会跳出循环或判断

  1. for i in xrange(10):
  2. print(i)
  3. if i == 8:
  4. break
  5. print('end')
  6. 执行:
  7. C:\Python27\python.exe D:/Python/type-of-data.py
  8. 0
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8
  17. end
  18. Process finished with exit code 0

continue

跳出当次循环

当循环或判断执行到continue语句时,continue后的语句将不再执行,会跳出当次循环,继续执行循环中的下一次循环

  1. for i in xrange(10):
  2. if i ==3:
  3. print('lalalalalalala')
  4. continue
  5. print(i)
  6. print('end')
  7. 执行:
  8. C:\Python27\python.exe D:/Python/type-of-data.py
  9. 0
  10. 1
  11. 2
  12. lalalalalalala
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. end
  20. Process finished with exit code 0

总结:

continue 语句跳出本次循环,只跳过本次循环continue后的语句

break 语句跳出整个循环体,循环体中未执行的循环将不会执行

  1. for i in xrange(10):
  2. if i ==3:
  3. print('lalalalalalala')
  4. continue
  5. print(i)
  6. if i == 8:
  7. break
  8. print('end')
  9. 执行:
  10. C:\Python27\python.exe D:/Python/type-of-data.py
  11. 0
  12. 1
  13. 2
  14. lalalalalalala
  15. 4
  16. 5
  17. 6
  18. 7
  19. 8
  20. end
  21. Process finished with exit code 0

python - break和continue的更多相关文章

  1. Python - break、continue 的使用

    前置知识 break.continue 会结合循环使用的,所以要先学会循环哦 python 提供了两种循环语句 for 循环:https://www.cnblogs.com/poloyy/p/1508 ...

  2. 记录今天学习python中for与while循环针对break和continue的用法

    python中有两个主要的循环for与while,其中针对这两个循环有两种不同的中断用法break与continue. 首先先看下面的循环代码: 1: for i in range(10):#变量i带 ...

  3. python 语句:条件、循环、break、continue...

    1. 条件语句 执行条件:判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围. [Python程序语言指定任何非0和非空(null)值为true,0 或 ...

  4. Python循环语句之break与continue的用法

    摘自原文章: http://www.jb51.net/article/73383.htm Python break 语句Python break语句,就像在C语言中,打破了最小封闭for或while循 ...

  5. Python基础(3)if_else、for、while、break与continue

    1.if ... else a=6 if a>=5: print("The a is bigger than 5") else: print("The a is s ...

  6. Python3基础(1)Python介绍、Python2 与Python3、变量、用户输入、if...else和for循环、while循环、break与continue

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ P ...

  7. python(3)-- 语句:条件、循环、break、continue...

    1. 条件语句 执行条件:判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围. [Python程序语言指定任何非0和非空(null)值为true,0 或 ...

  8. python中break、continue 、exit() 、pass终止循环的区别

    python中break.continue .exit() .pass区分 1.break:跳出循环,不再执行 Python break语句,就像在C语言中,打破了最小封闭for或while循环. b ...

  9. Python 循环语句(break和continue)

    Python 循环语句(break和continue) while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出 ...

随机推荐

  1. php下关于Cannot use a scalar value as an array的解决办法

    今天在测试php程序的时候,出现了一个错误提示:Cannot use a scalar value as an array,这个错误提示前几天也出过,当时好像稍微调了一下就好了,也没深究,今天却又出现 ...

  2. Python知识点进阶——生成器

    生成器 为什么要将列表转化为迭代器? 因为列表太大的话用内存太大,做成迭代器可以节省空间,用的时候再拿出部分. 生成器是不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,知 ...

  3. day23-python之日志 re模块

    1.logging import logging #-----------------------------------logging.basicConfig logging.basicConfig ...

  4. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. ArcGis API for JavaScript学习——离线部署API

    ArcGis API for JavaScript开发笔记——离线部署API 以3.18版API为例: 在加载图图前引用GIS服务是必须的.有两种方法,一是在线引用,而是离线部署引用. 在线引用: & ...

  6. Jin Ge Jin Qu hao UVA - 12563 01背包

    题目:题目链接 思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包 ...

  7. Go语言之并发编程(四)

    同步 Go 程序可以使用通道进行多个 goroutine 间的数据交换,但这仅仅是数据同步中的一种方法.通道内部的实现依然使用了各种锁,因此优雅代码的代价是性能.在某些轻量级的场合,原子访问(atom ...

  8. loj2035 「SDOI2016」征途

    学了斜率优化这题就能一气呵成地做出来啦qwqqwq #include <iostream> #include <cstdio> using namespace std; typ ...

  9. 【Median of Two Sorted Arrays】cpp

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  10. allocator class

    当分配一大块内存时,我们通常计划在这块内存上按需构造对象,这样的我们希望将内存分配和对象构造分离.但是通常的new关键字的分配的动态空间,有时候会造成一些浪费,更致命的是“如果一个类没有默认构造函数, ...