相关文章推荐
沉着的抽屉  ·  python for循环 ...·  1 月前    · 
博学的警车  ·  开始使用分析仪 Beta 版·  3 月前    · 
另类的砖头  ·  GroupBy 和 Ungroup 函式 ...·  8 月前    · 
乐观的枕头  ·  SQL SERVER ...·  1 年前    · 
精彩文章免费看

Python 循环语句

Python 循环语句

程序在一般情况下是按顺序执行的。
编程语言提供了各种控制结构,允许更复杂的执行路径。
循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式:

gif演示while执行过程
while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
# continue 和 break 用法
i = 1
while i < 10:   
    i += 1
    if i%2 > 0:     # 非双数时跳过输出
        continue
    print i         # 输出双数2、4、6、8、10
i = 1
while 1:            # 循环条件为1必定成立
    print i         # 输出1~10
    i += 1
    if i > 10:     # 当i大于10时跳出循环
        break
如果条件判断语句永远为 true,循环将会无限的执行下去,如下实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
var = 1
while var == 1 :  # 该条件永远为true,循环将无限执行下去
   num = raw_input("Enter a number  :")
   print "You entered: ", num
print "Good bye!"

以上实例输出结果:

Enter a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3
You entered: 3
Enter a number between :Traceback (most recent call last):
File "test.py", line 5, in <module>

num = raw_input("Enter a number :")

KeyboardInterrupt

注意:以上的无限循环你可以使用 CTRL+C 来中断循环。

循环使用else语句
在 python 中,while … else 在循环条件为 false 时执行 else 语句块:
#!/usr/bin/python
count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
else:
   print count, " is not less than 5"
简单语句组
类似 if 语句的语法,如果你的 while 循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:
#!/usr/bin/python
flag = 1
while (flag): print 'Given flag is really true!'
print "Good bye!"

注意:以上的无限循环你可以使用 CTRL+C 来中断循环。
实例 while循环 - 九九乘法表

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#九九乘法表
i = 1
while i :
    j = 1
    while j:
        print j ,"*", i ," = " , i * j , '  ',
        if i == j :
            break
        j += 1
        if j >= 10:
            break
    print "\n"
    i += 1
    if i >= 10:
        break
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # 第二个实例
   print '当前水果 :', fruit
print "Good bye!"
通过序列索引迭代
另外一种执行循环的遍历方式是通过索引,如下实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
fruits = ['banana', 'apple',  'mango']
for index in range(len(fruits)):
   print '当前水果 :', fruits[index]
print "Good bye!"

以上实例我们使用了内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。

循环 使用else语句
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
例子:打印质数
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for num in range(10, 20): 
    for i in range(2, num):
        if num%i == 0:
            j=num/i
            print '%d 等于 %d * %d' % (num,i,j)
            break;
    else:
        print num, '是一个质数'

Python break语句,就像在C语言中,打破了最小封闭for或while循环。
break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。
break语句用在while和for循环中。
如果您使用嵌套循环,break语句将停止执行最深层的循环,并开始执行下一行代码。
Python语言 break 语句语法:
break

Python continue 语句跳出本次循环,而break跳出整个循环。
continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。
continue语句用在while和for循环中。
Python 语言 continue 语句语法格式如下:
continue