while循環圖轉載自:http://www.runoob.com/python/python-while-loop.html
注:python中的函數沒有{ },所以語句之間都是以 tab鍵進行縮進來確定層次結構的,另每個條件句末尾都是以冒號 : 結尾
1、條件判斷 if
基本格式:
if 判斷條件:
執行語句.....
elif 判斷條件:
執行語句.....
else:
執行語句.....
邏輯操作符: and、or、not、in、not in
例:判斷三角形
a,b,c = 3,4,5
if a + b > c and a + c > b and b + c > a:
if a == b and b == c:
print("等邊三角形")
elif a == b or a == c or b == c:
print("等腰三角形")
else:
print("普通三角形")
else:
print("不是三角形")
2、while 循環
3、for循環
格式 for 變量 in 迭代條件
執行內容
將list["a","b","c",1,2,3],生成給一個字典,將字符作爲字典的key,數字作爲字典的value。結果{'a': 1, 'b': 2, 'c': 3}
a=["a","b","c",1,2,3]
b={}
for i in range(0,3): #range 爲迭代方法,range(x,y),表示變量從x開始,一直到y-1,以1遞增
b[a[i]]=a[i+3]
b=[6,1,8,0,3,7,12,32,4]
for i in range(0,len(b)-1):
for j in range(0,len(b)-1-i):
if b[j] > b[j+1]:
m=b[j]
b[j]=b[j+1]
b[j+1]=m
print(b)