標籤:操作符 delete 操作 and span 字典 結果 沒有 Suite
python學習[第十三篇] 條件和迴圈if語句單一if 語句
if語句有三個部分構成,關鍵字if本身,判斷結果真假的條件運算式,以及運算式為真或非0是執行的代碼
if expression:
expr_true_suite
條件運算式可以是多重的 通過布爾操作符 and or not來實現
單一語句的if 代碼塊,如果if語句的執行代碼只有一行,可以放在一行來寫
if expresssion: expr_tru_suite
>>> if True: print True...True
else 語句
文法如下:
if expression: expr_true_suiteelse: expr_false_suite
elf 語句
可以有多個elif ,但只能由一個if 一個else , 文法如下
if expression1: expr1_true_suiteelif expression2: expr2_true_suiteelif expression3: expr3_true_suite....elif expressionx: exprx_true_suiteelse: none_of_above_suite
python 中沒有switch/case語句
我們可以通過字典來實現,注意字典後的值不要加引號,對應方法應在字典前定義好。
def insert_met(): print "this is insert_met"def delete_met(): print "this is delete_met"def update_met(): print "this is update_met"CMDs={"insert":insert_met,"delete":delete_met,"update":update_met}def choice(m): CMDs[m]()choice(‘insert‘)choice(‘update‘)choice(‘delete‘)
python中三元操作符的實現
python學習[第十三篇] 條件和迴圈