每天一個python 小案例——while

   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


打印輸出結果

wKioL1Myf57yiLO0AAEvFyl0eEo936.jpg

用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)

輸出結果

wKioL1Myf_CCuGk_AAEkX9SY9Fw156.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章