迴圈(looping),計數迴圈(counting loop),條件迴圈(conditional loop)。
1 計數迴圈,for迴圈
每一次迴圈稱為一次迭代(iteration)。for looper in [1,2,3,4,5]:
print "hello"
再來試試看
這次和上次顯示的不同,是不是,如果迴圈出錯形成無限迴圈怎麼辦啊,中斷迴圈(ctrl+c)。for looper in [1,2,3,4,5]:
print looper
再來是乙個有意義點的程式,8的乘法表
for looper in [1,2,3,4,5]:
print looper, "times 8 =", looper * 8
執行結果如下
>>> ******************************== restart ******************************==
>>>
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
5 times 8 = 40
迴圈函式range(),
執行結果如下for looper in range (1,5):
print looper, "times 8 =", looper * 8
和上面顯示的結果有點不一樣,為什麼呢?>>> ******************************== restart ******************************==
>>>
1 times 8 = 8
2 times 8 = 16
3 times 8 = 24
4 times 8 = 32
range(),提供乙個數字列表,從給的第乙個數開始,在給的的最後乙個數之前結束。必須要注意到這一點。
再來乙個例子,8的乘法表(從1到10)
迴圈變數命名的通用慣例:習慣用i,j,k作為迴圈變數,不應當有其他用途。例如for looper in range (1,11):
print looper, "times 8 =", looper * 8
range()的簡寫for i in range (1,5):
print i, "times 8 =", i * 8
大多數的程式都從0開始迴圈而不是從1開始。range()預設從0開始,步長為1.如果想步長為2計數,可以這樣for i in range (5):
# 與寫作 for i in range (0, 5): 完全相同
print i, "times 8 =", i * 8
反向計數for i in range (1,10,2):
print i
再來個反向計數的例子,步長為-1後迴圈作反向計數for i in range (10, 1, -1):
print i
# 與孩子一起學程式設計08章
# 2023年8月27日14:03:52
# range()迴圈反向計數-2 8-10
import time
for i in range (10, 0, -1):
print i
time.sleep(1)
print "blast off!"
沒有數字的計數迴圈
執行結果for cool_guy in ["spongebob", "spoderman", "justin timberlake", "my dad"]:
print cool_guy, "is the coolest guy ever!"
>>>
spongebob is the coolest guy ever!
spoderman is the coolest guy ever!
justin timberlake is the coolest guy ever!
my dad is the coolest guy ever!
>>>
2 條件迴圈,while迴圈
如果能提前知道希望迴圈執行多少次,那麼for迴圈挺合適。不過,有時希望迴圈一直執行下去,直到發生某種情況時才結束,而且不知道發生這種情況之前會有多少次迭代。這就可以用while迴圈來實現。
執行結果print "type 3 to continue, anything else to quit."
someinput = raw_input()
while someinput == '3':
print "thank you for the 3. very kind of you."
print "type 3 to continue, anything else to quit."
someinput = raw_input()
print "that's not 3, so i'm quitting now."
>>>
type 3 to continue, anything else to quit.
3thank you for the 3. very kind of you.
type 3 to continue, anything else to quit.
3thank you for the 3. very kind of you.
type 3 to continue, anything else to quit.
3thank you for the 3. very kind of you.
type 3 to continue, anything else to quit.
5that's not 3, so i'm quitting now.
>>>
3 跳出迴圈——break和continue
用continue直接跳到迴圈的下一次迭代,或者用break完全終止迴圈。
先來看continue的用法
執行結果for i in range (1, 6):
print 'i =', i,
print 'hello, how',
if i == 3:
continue
print 'are you today?'
>>>
i = 1 hello, how are you today?
i = 2 hello, how are you today?
i = 3 hello, how
i = 4 hello, how are you today?
i = 5 hello, how are you today?
break的用法
執行結果for i in range (1, 6):
print 'i =', i,
print 'hello, how',
if i == 3:
break
print 'are you today?'
從上兩個程式能看出continue和break的用法和區別嗎?>>>
i = 1 hello, how are you today?
i = 2 hello, how are you today?
i = 3 hello, how
>>>
好了,python中的迴圈學習就到這兒,上一章的if,條件和測試,及這一章中計數迴圈和條件迴圈,差不多了吧,我都躍躍欲試了,呵呵。
與孩子一起學程式設計03章
基本數 算 四大基本運算,我就不用說了吧,加 減 乘 除 等號 也是乙個操作符,為賦值操作符。運算順序也是根據標準的運算法則,這些,大家都是了解的。另外,還有兩個操作符 指數 自乘為乙個冪 print 3 3 3 3 3 243 print 3 5 243 python用乙個雙星號 表示指數或者將乙...
與孩子一起學程式設計05章
python內建了乙個輸入函式 raw input 與孩子一起學程式設計05章 2013年8月21日16 03 04 學習raw input 輸入函式 print enter your name name raw input print hi name,how are you today.如果在pr...
與孩子一起學程式設計03章
1 dd input 吃飯人數 dd1 input 花費多少美元 dd2 input 小費費率 float 注意浮點型 float 每個都要定義!ss1 float dd1 float 1 float dd2 100 float dd print 每人應該出 ss1,美元 2 coding utf ...