python學習[第十三篇] 條件和迴圈

來源:互聯網
上載者:User

標籤:操作符   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學習[第十三篇] 條件和迴圈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.