python循环

一、while

1、while

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

2、while else

count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
else:
   print count, " is not less than 5"

二、for

fruits = ['banana', 'apple',  'mango']
for index in range(len(fruits)):
   print '当前水果 :', fruits[index]
使用内置 enumerate 函数进行遍历
sequence = [12, 34, 34, 23, 45, 76, 89]
for i, j in enumerate(sequence):
  print i,j
0 12
1 34
2 34
3 23
4 45
5 76
6 89

三、pass语句

1、pass是空语句,是为了保持程序结构的完整性。

2、pass 不做任何事情,一般用做占位语句。

for letter in 'Python':
   if letter == 'h':
      pass
      print '这是 pass 块'
   print '当前字母 :', letter

print "Good bye!"

  

原文地址:https://www.cnblogs.com/detanx/p/pythonFool.html