本章节学习Python的循环语句,程序在一般情况下是按顺序执行的。
编程语言提供了各种控制结构,允许更复杂的执行路径。
循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式:
Python提供了for循环和while循环(在Python中没有do..while循环):
循环类型 | 描述 |
---|---|
while 循环 | 在给定的判断条件为 true 时执行循环体,否则退出循环体 |
for 循环 | 重复执行语句 |
嵌套循环 | 你可以在while循环体中嵌套for循环 |
循环控制语句
循环控制语句可以更改语句执行的顺序。Python支持以下循环控制语句:
控制语句 | 描述 |
---|---|
break 语句 | 在语句块执行过程中终止循环,并且跳出整个循环 |
continue 语句 | 在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环 |
pass 语句 | pass是空语句,是为了保持程序结构的完整性 |
While循环语句
Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:
while 判断条件:
执行语句……
执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假false时,循环结束。
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
count = 0
while (count < 9):
print "The count is:",count
count = count + 1
print "Good Bye!"
执行结果:
[root@ruizhong scripts]# python while_1.py
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good Bye!
while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
continue的用法
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
i = 1
while i < 10:
i += 1
if i%2 > 0: # 非双数时跳过输出
continue
print i # 输出双数2、4、6、8、10
执行结果:
[root@ruizhong scripts]# python while_2.py
2
4
6
8
10
break的用法
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
i = 1
while 1: # 循环条件为1必定成立
print i # 输出1~5
i += 1
if i > 5: # 当i大于5时跳出循环
break
执行结果:
[root@ruizhong scripts]# python while_3.py
1
2
3
4
5
循环使用 else 语句
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
count = 0
while count < 5:
count += 1
print count,"is less than 5"
else:
print count,"is not less than 5"
执行结果:
[root@ruizhong scripts]# python while_4.py
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is less than 5
5 is not less than 5
for 循环语句
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
语法:
for循环的语法格式如下:
for iterating_var in sequence:
statements(s)
流程图:
[root@ruizhong scripts]# vim for_1.py
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
for letter in 'Python':
print "当前字母:", letter
fruites = ['banana','apple','mango']
for fruit in fruites:
print "当前字母:", fruit
print "Good Bye!"
执行结果:
[root@ruizhong scripts]# python for_1.py
当前字母: P
当前字母: y
当前字母: t
当前字母: h
当前字母: o
当前字母: n
当前字母: banana
当前字母: apple
当前字母: mango
Good Bye!
通过序列索引迭代
另外一种执行循环的遍历方式是通过索引,如下实例:
[root@ruizhong scripts]# vim for_2.py
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
fruits = ['banana','apple','mango']
for index in range(len(fruits)):
print "当前水果:", fruits[index]
print "GOOD Bye"
执行结果:
[root@ruizhong scripts]# python for_2.py
当前水果: banana
当前水果: apple
当前水果: mango
GOOD Bye
以上实例我们使用了内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。
循环使用 else 语句
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
如下实例:
[root@ruizhong scripts]# vim for_3.py
#!/usr/bin/env python
#-*- coding:UTF-8 -*-
for num in range(10,20): #迭代10到20之间的数字
for i in range(2,num): #根据因子迭代
if num%i == 0: #确定第一个因子
j=num/i #计算第二个因子
print '%d 等于 %d * %d' % (num,i,j)
break #跳出当前循环
else: #循环的else部分
print num,'是一个质数'
执行结果:
[root@ruizhong scripts]# python for_3.py
10 等于 2 * 5
11 是一个质数
12 等于 2 * 6
13 是一个质数
14 等于 2 * 7
15 等于 3 * 5
16 等于 2 * 8
17 是一个质数
18 等于 2 * 9
19 是一个质数