Python流程控制

Python流程控制express

1)if條件測試數據結構

Python的比較操做
    全部的Python對象都支持比較操做
    測試操做符('=='操做符測試值的相等性; 'is'表達式測試對象的一致性)

Python中不一樣類型的比較方法
    數字:經過相對大小進行比較
    字符串:按照字典次序逐字符進行比較
    列表和元組:自左至右比較各部份內容
    字典:對排序以後的(鍵、值)列表進行比較

Python中的真假:
    一、任何非0數字和非空對象都爲真;
    二、數字0、空對象和特殊對象None均爲假;
    三、比較和相等測試會遞歸地應用於數據結構中;
    四、返回值爲True或False;

組合條件測試:
    X and Y: 與運算
    X or Y: 或運算
    not X: 非運算
    
注意:
    and或or運算會返回真或假的對象,而不是True或者False
    and和or是短路操做符

例:    >>>a = True
        >>>print a and 'a = T' or 'a = F'
        'a=T'           會返回‘a = T’,若不理解自行補充布爾運算
        

if測試的語法控制結構    
    if boolean_expression1:
        suite1
    elif boolean_expression2:
        suite2
    ...
    else:
        else_suite
        
    注意:
        elif、else語句均是可選的
        僅用於佔位,然後填充相關語句時,可使用pass
        
if/else三元表達式
    if  X :
        A = Y
    else :
        A = Z
    能夠改寫成      A = Y if X else Z
    語法格式:
        expression1 if boolean_expression else expression2
        (若是boolean_expression的值爲True,則條件表達式的結果爲expression1,不然爲expression2)

2)while循環app

用於編寫通用迭代結構
頂端測試爲真即會執行循環體,並重復屢次直到爲假後執行循環後的其餘語句

語法格式:
    while bool_expression: 
        while_suite
    else:
        else_suite
        
    注意:
        else分支爲可選部分
        只要boolean_expression的結果爲True,循環就會執行
        boolean_expression的結果爲False時終止循環,若是有else分支,則會執行
        
    break: 跳出最內層的循環;
    continue: 跳到所處的最近層循環的開始處;
    pass: 佔位語句
    else代碼塊:循環正常終止纔會執行;若是循環終止是由break跳出致使的,則else不會執行;  

while循環語法格式擴展
    while bool_expression1: 
        while_suite
        if boolean_expression2:
            break
        if boolean_expression3:
            continue
    else:
        else_suite

    例:  使用鍵盤輸入數據到列表內,直到輸入q或quit時中止
          >>>test = [ ]
          >>>while True:
                  x = raw_input('Enter an entry: ')
                  if x == 'q' or x == 'quit':
                        break
              else:
                  test.append(x)

3)for循環函數

用於遍歷任何有序的序列對象內的元素
可用於字符串、元組、列表和其它的內置可迭代對象,以及經過類所建立的新對象

語法格式:
    for expression1 in iterable:
        for_suite
    else:
        else_suite
        
注意:
    expression或是一個單獨的變量,或是一個變量序列,通常以元組的形式給出
    例:
    >>> T = [(1,2),(3,4),(5,6),(7,8)]
    >>> for (a,b) in T:
            print a,b
    1 2
    3 4
    5 6
    7 8
    
for循環形式擴展
    語法格式:
    for expression in iterable:
        for_suite
        if boolean_expression2:
            break
        if boolean_expression3:
            continue
    else:
        else_suite

for循環比while循環執行速度快
Python中提供了兩個內置函數,用於在for循環中定製特殊的循環(range或xrange  與zip)

4)range函數與zip測試

range:一次性地返回連續的整數列表
   xrange:一次產生一個數據元素,相較於range更節約空間
   zip:返回並行的元素元組的列表,經常使用於在for循環中遍歷數個序列
   
   range函數:
        非完備遍歷(用於每隔必定的個數元素挑選一個元素)
            >>> range(0,10,2)
            [0,2,4,6,8,10]
            >>> S = 'How are you ?'           
            >>> for i in range(0,len(S),2):
                    print S[i]
            H w a e y u ? 
        修改列表
            >>> L = [1,2,3,4,5]
            >>> for i in range(len(L)):
                    L[i]+=1
            >>> print L
            [2,3,4,5,6]
            
    zip
        實現並行迭代
        例1:
            >>> L1 = [ 1,2,3,4,5 ]
            >>> L2 = ['a','b','c','d','e']
            >>> zip(L1,L2)
            [(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e')]
        
        動態構造字典    
        例2:  已有 keys = [ 'x','y','z']   values = [1,2,3]  使用循環生成字典使其一一對應
         >>> dict1 = {}
         >>> keys = [ 'x','y','z']
         >>> values = [1,2,3]
         >>> for (k,v) in zip(keys,values):
                 dict1[k] = v
         >>> print dict1
         {'y': 2, 'x': 1, 'z': 3}
相關文章
相關標籤/搜索