python學習[第十三篇] 條件和循環
阿新 • • 發佈:2018-07-23
one res mds this 沒有 case語句 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_suite elif expression2: expr2_true_suite elif expression3: expr3_true_suite .... elif expressionx: exprx_true_suite else: 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學習[第十三篇] 條件和循環