python流程控制語法
if語句
var1 = 100
if var1:
print "1 - got a true expression value"
print var1
var2 = 0
if var2:
print "2 - got a true expression value"
print var2
print "good bye!"
#if的條件可以是數字或字串或者布林值true和false(布林表示式)
#如果是數字,則只要不等於0,就為true
#如果是字串,則只要不是空串,就為true
if else
var = 100
if var == 200:
print "1 - got a true expression value"
print var
elif var == 150:
print "2 - got a true expression value"
print var
elif var == 100:
print "3 - got a true expression value"
print var
else:
print "4 - got a false expression value"
print var
print "good bye!"
巢狀if else
var = 100
if var < 200:
print "expression value is less than 200"
if var == 150:
print "which is 150"
elif var == 100:
print "which is 100"
elif var == 50:
print "which is 50"
elif var < 50:
print "expression value is less than 50"
else:
print "could not find true expression"
print "good bye!"
while迴圈
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
x=2i=0
while x<34:
i+=1
if i==12:
print "break....................................."
break
; print x
x=35
i=0while x<34:
i+=1
if i==12:
print "break....................................."
break
; print x
else:
print "沒有執行迴圈......."
for迴圈
for i in range(0,5):
print i;
for i in range(0,5,1):
print i;
for i in range(5,0,-1):
print i;
#遍歷集合
r=range(10,20)
r=#set型別r=[
"aaa",3,"c"
]print(r)
for num in r:
print(num)
r=print(r)
for num in r.values(
): print(num)
python流程控制 python之流程控制
電腦程式在解決某個具體問題時,包括三種情形,即順序執行所有的語句 選擇執行部分的語句和迴圈執行部分語句,這正好對應著程式設計中的三種程式執行結構流程 順序結構 選擇結構和迴圈結構。事實證明,任何乙個能用計算機解決的問題,只要應用這三種基本結構來寫出的程式都能解決。python語言當然也具有這三種基本...
python之流程控制
python中流程控制之if while for if 用來判斷條件是否符合,而後執行相應的操作 也可單獨使用if用來當滿足條件後執行某個操作 常見 if if else if elif else 典型的三目運算就是利用簡化if語句來獲取所需的值 用來結束迴圈操作 只結束距離自己最近的迴圈體 for...
python之流程控制
1.1.if語句語法 if 條件1 elif 條件2 else 條件3 1.2.if語句應用 登陸功能 name input please enter your name strip password input please enter your passwd strip if name weix...