zoukankan      html  css  js  c++  java
  • Python学习笔记四:过程控制

    条件语句:

    #以缩进来区分表示同一范围。

     1 # coding=utf8
     2 # 例1:if 基本用法
     3 
     4 flag = False
     5 name = 'luren'
     6 if name == 'python':         # 判断变量否为'python'
     7     flag = True          # 条件成立时设置标志为真
     8     print 'welcome boss'    # 并输出欢迎信息
     9 else:
    10     print name              # 条件不成立时输出变量名称

    多重判断:

     1 # coding=utf8
     2 # 例2:elif用法
     3 
     4 num = 5     
     5 if num == 3:            # 判断num的值
     6     print 'boss'        
     7 elif num == 2:
     8     print 'user'
     9 elif num == 1:
    10     print 'worker'
    11 elif num < 0:           # 值小于零时输出
    12     print 'error'
    13 else:
    14     print 'roadman'     # 条件均不成立时输出

    python 并不支持 switch 语句

     1 # coding=utf8
     2 # 例3:if语句多个条件
     3 
     4 num = 9
     5 if num >= 0 and num <= 10:    # 判断值是否在0~10之间
     6     print 'hello'
     7 >>> hello        # 输出结果
     8 
     9 num = 10
    10 if num < 0 or num > 10:    # 判断值是否在小于0或大于10
    11     print 'hello'
    12 else:
    13     print 'undefine'
    14 >>> undefine        # 输出结果
    15 
    16 num = 8
    17 # 判断值是否在0~5或者10~15之间
    18 if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):    
    19     print 'hello'
    20 else:
    21     print 'undefine'
    22 >>> undefine        # 输出结果

    同一行,与IDL类似

    1 #!/usr/bin/python 
    2  
    3 var = 100 
    4  
    5 if ( var  == 100 ) : print "Value of expression is 100" 
    6  
    7 print "Good bye!" 

    循环语句:

    while

    1 #!/usr/bin/python
    2 
    3 count = 0
    4 while (count < 9):
    5    print 'The count is:', count
    6    count = count + 1
    7 
    8 print "Good bye!"

    while... else

    1 #!/usr/bin/python
    2 
    3 count = 0
    4 while (count < 9):
    5    print 'The count is:', count
    6    count = count + 1
    7 
    8 print "Good bye!"
    1 #!/usr/bin/python
    2 
    3 flag = 1
    4 
    5 while (flag): print 'Given flag is really true!'
    6 
    7 print "Good bye!"

    while循环嵌套

     1 #coding=utf-8
     2 #!/usr/bin/python
     3 
     4 i = 2
     5 while(i < 100):
     6    j = 2
     7    while(j <= (i/j)):
     8       if not(i%j): break
     9       j = j + 1
    10    if (j > i/j) : print i, " 是素数"
    11    i = i + 1
    12 
    13 print "Good bye!"

    for

    遍历任何序列的项目,列表或字符串

    for i in sequence

         statements(s)

     1 #!/usr/bin/python
     2 
     3 for letter in 'Python':     # First Example
     4    print 'Current Letter :', letter
     5 
     6 fruits = ['banana', 'apple',  'mango']
     7 for fruit in fruits:        # Second Example
     8    print 'Current fruit :', fruit
     9 
    10 print "Good bye!"

    >
    Current Letter : P
    Current Letter : y
    Current Letter : t
    Current Letter : h
    Current Letter : o
    Current Letter : n
    Current fruit : banana
    Current fruit : apple
    Current fruit : mango
    Good bye!
    
    

    索引

    1 #!/usr/bin/python
    2 
    3 fruits = ['banana', 'apple',  'mango']
    4 for index in range(len(fruits)):
    5    print 'Current fruit :', fruits[index]
    6 
    7 print "Good bye!"

    内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。

    for.... else...(for循环嵌套)

     1 #!/usr/bin/python
     2 
     3 for num in range(10,20):  #to iterate between 10 to 20
     4    for i in range(2,num): #to iterate on the factors of the number
     5       if num%i == 0:      #to determine the first factor
     6          j=num/i          #to calculate the second factor
     7          print '%d equals %d * %d' % (num,i,j)
     8          break #to move to the next number, the #first FOR
     9    else:                  # else part of the loop
    10       print num, 'is a prime number'

    break跳出整个循环  continue跳出本次循环  pass空语句,是为了保持程序结构的完整性。

    1 #!/usr/bin/python
    2 
    3 for letter in 'Python': 
    4    if letter == 'h':
    5       pass
    6       print 'This is pass block'
    7    print 'Current Letter :', letter
    8 
    9 print "Good bye!"
  • 相关阅读:
    夫妻合伙创业,度过艰苦的时期,酒楼生意蒸蒸日上
    搬运工做起了石头生意,他的石雕产品深受喜爱
    都市白领开毛衣店,仅一年时间收入20万元
    高中生下海创业,几十年来他始终将家乡的利益放在首位
    白手起家的他,创立公司,身价达到数千万
    从搬砖工到亿万富豪,这些年他经历了什么?
    货车司机创业做物流,他只为赢得老百姓的口碑
    患难夫妻做服装,他们的公司在全国做出了名堂
    struts2注解的作用
    struts2注解的作用
  • 原文地址:https://www.cnblogs.com/haizhupan/p/4198454.html
Copyright © 2011-2022 走看看