Python--4了不起的分支和循環
阿新 • • 發佈:2017-08-18
執行 as3 縮進 計數 字符串 情況 pan 異常 spa
if條件分支:
if 條件 :
條件為True執行操作
elif 條件 :
條件為True執行操作
else:
其他情況執行操作
python避免懸掛else
C語言:else就近
if(hi > 2) if(hi > 7) printf("好棒! 好棒 !"); esle printf("qie")
python通過強制使用縮進
條件表達式(三元操作符)
x, y = 4, 5 if x < y: small = x else: small = y
可改進為 語法:x if 條件 else y
small = x if x < y else y
斷言 assert
assert這個關鍵字稱之為斷言,但這個關鍵字後邊的條件為假的時候,程序自動崩潰並拋出AssertionError的異常
>>> assert 3 > 4
while循環
語法:while 條件:
循環體
for 循環 計數器循環
表達式可以是一個列表或是元組或字符串
語法: for 目標 in 表達式:
循環體
favourite = ‘FishC‘ for i in davourute: print(i)
F i s h c
Python--4了不起的分支和循環