python break和continue

时间:05-04 来源:老王python, 标签:循环

break语句,就像在C语言中,打破了最小封闭for或while循环。 而continue语句,终止这次循环,继续执行下一次迭代的循环。 for和while循环可以设置一个else子句;如果你的循环不是通过break终止的话,最后会执行else语句的内容。比如搜索素数的例子:


>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print n, 'equals', x, '*', n/x
...             break
...     else:
...         # loop fell through without finding a factor
...         print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

相关文章

老王python提供python基础教程,爬虫,seo工具,excel相关开发教程。

Copyright © 2020 www.cnpythoner.com All rights reserved. 赣ICP备19013357号-1基于python+django开发