参考:http://www.runoob.com/python
Python While循环语句
Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:
while 判断条件: 执行语句……
简单语句组
类似if语句的语法,如果你的while循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:
flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!"
循环使用 else 语句
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5"
Python for 循环语句
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
语法:
for循环的语法格式如下:
for iterating_var in sequence: statements(s)
通过序列索引迭代
另外一种执行循环的遍历方式是通过索引,如下实例:
fruits = ['banana', 'apple', 'mango'] for index in range(len(fruits)): print '当前水果 :', fruits[index] print "Good bye!"
Python 循环嵌套
Python 语言允许在一个循环体里面嵌入另一个循环。
Python break,continue 语句
与php中break,continue没有太大区别。
Python pass 语句
Python pass是空语句,是为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句。
在php中没有出现过pass语句,查看手册给出的pass相当于break,continue之间的过度。即没有任何意义,不跳过(continue)也不终止(break)。