Python_实用入门篇_04

1.if流程控制 

    1.语法结构                                   

      if 条件测试为 true:                                   

         执行语句              

                       elif 条件测试为 true:

         执行语句

        else true:

         执行语句

注意(一个if流程控制里,可以有多个elif 条件测试,可以省略else)

      2.条件测试(返回布尔值true或者false)

      常用的有(==, != ,>=, <= >=and<=  , >=or<= , in , not in)

复制代码

requested_toppings = ['mushrooms', 'extra cheese'] 
 
if 'mushrooms' in requested_toppings:     
    print("Adding mushrooms.") 
elif 'pepperoni' in requested_toppings:     
    print("Adding pepperoni.") 
elif 'extra cheese' in requested_toppings:     
    print("Adding extra cheese.")      

print("\nFinished making your pizza!") 

复制代码

2.while流程控制

    1.语法结构

      while 判断条件:

            执行语句……

注意:执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。当判断条件假false时,循环结束。

i = 1
while i < 10:   
    i += 1
    if i%2 > 0:
        print i

      2.while else

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

复制代码

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

复制代码

      3.无限循环

      如果条件判断语句永远为 true,循环将会无限的执行下去。

i = 1
while i == 1:  # 该条件永远为true,循环将无限执行下去
   num = input("Enter a number  :")
   print (“You entered:%d”%num)

                    4.countine与break

         while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立。

复制代码

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(直接跳出程序)

www.lnluqu.cn
www.025invest.cn
www.wx-detian.cn
www.xintravel.cn
www.fenghuicaifu.cn
www.chinaytbzw.cn
www.sydire.cn
www.jnymkj.cn
www.metrel-china.cn
www.hongheqiche.cn
www.zazhiku.cn
www.yz-hysl.cn
www.gutiangelun.cn
www.lnjjpm.cn
www.touraround.cn
www.sunmoon88.com.cn
www.13142js.cn
www.caixiantang.com.cn
www.poponet.cn
www.zglogistics.cn
www.zgxwzk.cn
www.bjltsc.cn
www.shshunfeng.cn
www.hzwssy.com.cn
www.daoyitang.cn
www.rossicount.cn
www.bjchange.cn

猜你喜欢

转载自blog.csdn.net/szgxingq/article/details/82015686