zoukankan      html  css  js  c++  java
  • 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!"
    

      

  • 相关阅读:
    解决Struts中文乱码问题总结
    也谈创业企业CEO该拿多少工资
    Codeforces Round #242 (Div. 2) A. Squats
    设计模式之模板方法模式
    hdu2768Cat vs. Dog (反建法,最大独立集)
    Python
    kvm
    linux bin文件制作
    python
    pytho
  • 原文地址:https://www.cnblogs.com/detanx/p/pythonFool.html
Copyright © 2011-2022 走看看