python循环,,一、while1、w


一、while

1、while

i = 1while i < 10:       i += 1    if i%2 > 0:     # 非双数时跳过输出        continue    print i         # 输出双数2、4、6、8、10 i = 1while 1:            # 循环条件为1必定成立    print i         # 输出1~10    i += 1    if i > 10:     # 当i大于10时跳出循环        break

2、while else

count = 0while count < 5:   print count, " is  less than 5"   count = count + 1else:   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,j0 121 342 343 234 455 766 89

三、pass语句

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

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

for letter in ‘Python‘:   if letter == ‘h‘:      pass      print ‘这是 pass 块‘   print ‘当前字母 :‘, letterprint "Good bye!"

  

python循环

评论关闭