Python 循环语句
while 循环 在给定的判断条件为 true 时执行循环体,否则退出循环体。
for 循环 重复执行语句
嵌套循环 你可以在while循环体中嵌套for循环
循环控制语句
循环控制语句可以更改语句执行的顺序。Python支持以下循环控制语句:
break 语句 在语句块执行过程中终止循环,并且跳出整个循环
continue 语句 在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
pass 语句 pass是空语句,是为了保持程序结构的完整性。
While 循环语句
while 判断条件(condition):执行语句(statements)……
例如:
#!/usr/bin/pythoncount = 0
while (count < 9):print 'The count is:', countcount = count + 1print "Good bye!"
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 和 break 用法i = 1
while i < 10: i += 1if i%2 > 0: # 非双数时跳过输出continueprint i # 输出双数2、4、6、8、10i = 1
while 1: # 循环条件为1必定成立print i # 输出1~10i += 1if 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: ", numprint "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
循环使用 else 语句
在 python 中,while … else 在循环条件为 false 时执行 else 语句块:
#!/usr/bin/pythoncount = 0
while count < 5:print count, " is less than 5"count = count + 1
else:print count, " is not less than 5"
打印:
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
简单语句组
类似 if 语句的语法,如果你的 while 循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:
#!/usr/bin/pythonflag = 1while (flag): print 'Given flag is really true!'print "Good bye!"
for 循环语句
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for iterating_var in sequence:statements(s)
例子:
#!/usr/bin/python
# -*- coding: UTF-8 -*-for letter in 'Python': # 第一个实例print("当前字母: %s" % letter)fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例print ('当前水果: %s'% fruit)print ("Good bye!")
通过序列索引迭代
#!/usr/bin/python
# -*- coding: UTF-8 -*-fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):print ('当前水果 : %s' % fruits[index])print ("Good bye!")
Python 循环嵌套
Python 语言允许在一个循环体里面嵌入另一个循环。
Python for 循环嵌套语法:
for iterating_var in sequence:for iterating_var in sequence:statements(s)statements(s)
Python while 循环嵌套语法:
while expression:while expression:statement(s)statement(s)
例如:
#!/usr/bin/python
# -*- coding: UTF-8 -*-i = 2
while(i < 100):j = 2while(j <= (i/j)):if not(i%j): breakj = j + 1if (j > i/j) : print i, " 是素数"i = i + 1print "Good bye!"
break 语句
#!/usr/bin/python
# -*- coding: UTF-8 -*-for letter in 'Python': # 第一个实例if letter == 'h':breakprint '当前字母 :', lettervar = 10 # 第二个实例
while var > 0: print '当前变量值 :', varvar = var -1if var == 5: # 当变量 var 等于 5 时退出循环breakprint "Good bye!"
continue 语句
#!/usr/bin/python
# -*- coding: UTF-8 -*-for letter in 'Python': # 第一个实例if letter == 'h':continueprint '当前字母 :', lettervar = 10 # 第二个实例
while var > 0: var = var -1if var == 5:continueprint '当前变量值 :', var
print "Good bye!"
pass 语句
Python pass 是空语句,是为了保持程序结构的完整性。
#!/usr/bin/python
# -*- coding: UTF-8 -*- # 输出 Python 的每个字母
for letter in 'Python':if letter == 'h':passprint '这是 pass 块'print '当前字母 :', letterprint "Good bye!"