zoukankan      html  css  js  c++  java
  • 5.Python判断 条件 循环语句while/for/if

    1.Python条件语句  if....elif....else

    由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。

    1 if 判断条件1:
    2     执行语句1……
    3 elif 判断条件2:
    4     执行语句2……
    5 elif 判断条件3:
    6     执行语句3……
    7 else:
    8     执行语句4……

    2.循环语句while 

     while语句如下

    while 判断条件:
        执行语句……

    在 python 中,while … else 在循环条件为 false 时执行 else 语句块:

    1 #!/usr/bin/python
    2  
    3 count = 0
    4 while count < 5:
    5    print count, " is  less than 5"
    6    count = count + 1
    7 else:
    8    print count, " is not less than 5"

    输出结果如下

    1 0 is less than 5
    2 1 is less than 5
    3 2 is less than 5
    4 3 is less than 5
    5 4 is less than 5
    6 5 is not less than 5

    3.Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

    语法:

    for循环的语法格式如下:

    for iterating_var in sequence:
       statements(s)

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

    输出结果如下:

     1 当前字母 : P
     2 当前字母 : y
     3 当前字母 : t
     4 当前字母 : h
     5 当前字母 : o
     6 当前字母 : n
     7 当前水果 : banana
     8 当前水果 : apple
     9 当前水果 : mango
    10 Good bye!

    for通过序列索引迭代

    另外一种执行循环的遍历方式是通过索引,如下实例:

    1 #!/usr/bin/python
    2 # -*- coding: UTF-8 -*-
    3  
    4 fruits = ['banana', 'apple',  'mango']
    5 for index in range(len(fruits)):
    6    print '当前水果 :', fruits[index]
    7  
    8 print "Good bye!"
    for i in range(2,100):  这样迭代可以从2到99
    for i in range(100):  这样迭代可以从0到100
    上面示例如下:
    当前水果 : banana
    当前水果 : apple
    当前水果 : mango
    Good bye!
    ----------------------------
    在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
     1 #!/usr/bin/python
     2 # -*- coding: UTF-8 -*-
     3  
     4 for num in range(10,20):  # 迭代 10 到 20 之间的数字
     5    for i in range(2,num): # 根据因子迭代
     6       if num%i == 0:      # 确定第一个因子
     7          j=num/i          # 计算第二个因子
     8          print '%d 等于 %d * %d' % (num,i,j)
     9          break            # 跳出当前循环
    10    else:                  # 循环的 else 部分
    11       print num, '是一个质数'

    4.break 终止循环,continue跳出一次循环, pass不做任何事

    4.1break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。

    break语句用在while和for循环中。

    4.2

    ontinue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。

    continue语句用在while和for循环中。

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

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

    Python 语言 pass 语句语法格式如下:



  • 相关阅读:
    二分图的部分关系
    二分图的部分关系
    日常训练赛 Problem C – Complete Naebbirac’s sequence
    日常训练赛 Problem C – Complete Naebbirac’s sequence
    J
    J
    Python strip()方法
    Python startswith()方法
    Python splitlines()方法
    Python split()方法
  • 原文地址:https://www.cnblogs.com/zluckiy/p/11645255.html
Copyright © 2011-2022 走看看