while 循環,它所作的和 if 語句類似,也是去檢查一個布爾表達式的真假,不一
樣的是它下面的代碼片段不是隻被執行一次,而是執行完後再調回到 while 所在的位置,
如此重複進行,直到 while 表達式爲 False 爲止
#!/bin/usr/python
i=0
numbers=[]
while i<5:
print "At the top i is %d" % i
numbers.append(i)
i = i+1
print "Number now:",numbers
print "At hte bottom i is %d" % i
print "The numbers:"
for num in numbers:
print num
打印輸出結果
用for循環打印出同樣的結果
#!/bin/usr/python
numbers=[]
for i in range(0,5):
numbers.append(i)
print "At the top i is %d" %(i)
print "Numbers now",numbers
print "At the botton i is %d" %(i+1)
輸出結果