Python中條件語句if 是經過一條或者多條的執行語句的結果,來判斷是否執行其包含的代碼塊。python
一般會配合else、elif一塊兒使用,達到根據條件進行多個代碼塊的執行操做。spa
簡單的if code
score = 90 if score >= 95: print("優秀") #沒有輸出 if 95 > score >= 80: print("良") #輸出: 良
和else 配合使用:blog
score = 90 if score >= 60: print("合格") else: print("不合格") #輸出: 合格
使用if -elif-else判斷結構:it
score = 80 if score < 60: print("不合格") elif 80 > score >= 60: print("中") else: print("良") #輸出: 良
在if語句中嵌套條件語句:class
score = 80 if score >= 60: print("經過") if 80 > score >= 60: print("中") if 90 > score >= 80: print("良") if 100 >= score >= 90: print("優") else: print("不合格") #輸出:經過 # 良
score = 80 if score < 60: print("不合格") elif score < 70: print("合格") elif score < 80: print("中") elif score < 90: print("良") else: print("優") #輸出:良
目前python3.0中沒有 switch case結構,因此咱們須要靈活的使用if-elif-else結構來進行條件判斷。di