zoukankan      html  css  js  c++  java
  • python 循环语句

    while循环

    #!/usr/bin/python
    
    count = 0
    while (count < 9):
       print 'The count is:', count
       count = count + 1
    
    print "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!

    continue 和break的使用:

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

    结果:

    2
    4
    6
    8
    10
    ----------------------------------
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    无限循环:

    var = 1
    while var == 1 :  # 该条件永远为true,循环将无限执行下去
       num = raw_input("Enter a number  :")
       print "You entered: ", num
    
    print "Good bye!"

    结果 因为var始终=1,因此会不换要求你填写内容,然后输出你写的内容:

    Enter a number  :soyoungboy
    You entered:  soyoungboy
    Enter a number  :haha
    You entered:  haha
    Enter a number  :

    循环while+else

    当满足count不小于5的条件走else语句

    #!/usr/bin/python
    
    count = 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

    死循环:

    #!/usr/bin/python
    
    flag = 1
    
    while (flag): print 'Given flag is really true!'
    
    print "Good bye!"

     for循环

    # !/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    for letter in 'Python':  # 第一个实例
        print '当前字母 :', letter
    
    fruits = ['banana', 'apple', 'mango']
    for fruit in fruits:  # 第二个实例
        print '当前水果 :', fruit
    
    print "Good bye!"

    结果:

    当前字母 : P
    当前字母 : y
    当前字母 : t
    当前字母 : h
    当前字母 : o
    当前字母 : n
    当前水果 : banana
    当前水果 : apple
    当前水果 : mango
    Good bye!

    序列索引迭代

    fruits = ['banana', 'apple', 'mango']
    i = len(fruits)
    l = range(i)
    # i相当于size,l相当于position
    print i
    print l
    for index in l:
        print '当前水果 :', fruits[index]
    
    print "Good bye!"

    结果:

    3
    [0, 1, 2]
    当前水果 : banana
    当前水果 : apple
    当前水果 : mango
    Good bye!

    循环使用else语句

    for num in range(10, 20):  # 迭代 10 到 20 之间的数字
        for i in range(2, num):  # 根据因子迭代
            if num % i == 0:  # 确定第一个因子
                j = num / i  # 计算第二个因子
                print '%d 等于 %d * %d' % (num, i, j)
                break  # 跳出当前循环
        else:  # 循环的 else 部分
            print num, '是一个质数'

    结果:

    10 等于 2 * 5
    11 是一个质数
    12 等于 2 * 6
    13 是一个质数
    14 等于 2 * 7
    15 等于 3 * 5
    16 等于 2 * 8
    17 是一个质数
    18 等于 2 * 9
    19 是一个质数

    循环嵌套:

    # !/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    i = 2
    while (i < 100):
        j = 2
        while (j <= (i / j)):
            if not (i % j): break
            j = j + 1
        if (j > i / j): print i, " 是素数"
        i = i + 1
    
    print "Good bye!"

    结果:

    2  是素数
    3  是素数
    5  是素数
    7  是素数
    11  是素数
    13  是素数
    17  是素数
    19  是素数
    23  是素数
    29  是素数
    31  是素数
    37  是素数
    41  是素数
    43  是素数
    47  是素数
    53  是素数
    59  是素数
    61  是素数
    67  是素数
    71  是素数
    73  是素数
    79  是素数
    83  是素数
    89  是素数
    97  是素数
    Good bye!

    break语句:

    # !/usr/bin/python
    # -*- coding: UTF-8 -*-
    for letter in 'hello world':  # First Example
        if letter == 'o':
            break
        print 'Current Letter :', letter

    结果:

    Current Letter : h
    Current Letter : e
    Current Letter : l
    Current Letter : l

    continue语句

    # !/usr/bin/python
    # -*- coding: UTF-8 -*-
    for letter in 'hello world':  # First Example
        if letter == 'o':
            continue
        print 'Current Letter :', letter
    Current Letter : h
    Current Letter : e
    Current Letter : l
    Current Letter : l
    Current Letter :  
    Current Letter : w
    Current Letter : r
    Current Letter : l
    Current Letter : d

    pass语句 占位语句:

    for letter in 'hello world':  # First Example
        if letter == 'o':
            pass
            print "-----------我是分割线-----------"
        print 'Current Letter :', letter

    结果:

    Current Letter : h
    Current Letter : e
    Current Letter : l
    Current Letter : l
    -----------我是分割线-----------
    Current Letter : o
    Current Letter :  
    Current Letter : w
    -----------我是分割线-----------
    Current Letter : o
    Current Letter : r
    Current Letter : l
    Current Letter : d
  • 相关阅读:
    GIT配置及用法
    Web前端深思
    SPA解释:单页应用程序
    对 Sea.js 进行配置(一) seajs.config
    前端开发知识体系技能点【根据自我学习顺序】
    App性能提升方法
    浅谈Bootstrap自适应功能在Web开发中的应用
    《写给大家看的设计书》 读书笔记(三)
    《写给大家看的设计书》读书笔记(一)
    《写给大家看的设计书》读书笔记(二)
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/6676608.html
Copyright © 2011-2022 走看看