Python While循環(huán)語句
Python 編程中 while 語句用于循環(huán)執(zhí)行程序,即在某條件下,循環(huán)執(zhí)行某段程序,以處理需要重復(fù)處理的相同任務(wù),
Python 循環(huán)語句
。其基本形式為:while 判斷條件: 執(zhí)行語句……
執(zhí)行語句可以是單個語句或語句塊。判斷條件可以是任何表達(dá)式,任何非零、或非空(null)的值均為true。
當(dāng)判斷條件假false時,循環(huán)結(jié)束。
執(zhí)行流程圖如下:
實例:
#!/usr/bin/pythoncount = 0while (count < 9): print 'The count is:', count count = count + 1print "Good bye!"
以上代碼執(zhí)行輸出結(jié)果:
The count is: 0The count is: 1The count is: 2The count is: 3The count is: 4The count is: 5The count is: 6The count is: 7The count is: 8Good bye!
while 語句時還有另外兩個重要的命令 continue,break 來跳過循環(huán),continue 用于跳過該次循環(huán),break 則是用于退出循環(huán),此外"判斷條件"還可以是個常值,表示循環(huán)必定成立,具體用法如下:
# continue 和 break 用法i = 1while i < 10: i += 1 if i%2 > 0: # 非雙數(shù)時跳過輸出 continue print i # 輸出雙數(shù)2、4、6、8、10i = 1while 1: # 循環(huán)條件為1必定成立 print i # 輸出1~10 i += 1 if i > 10: # 當(dāng)i大于10時跳出循環(huán) break
無限循環(huán)
如果條件判斷語句永遠(yuǎn)為 true,循環(huán)將會無限的執(zhí)行下去,如下實例:
#!/usr/bin/python# -*- coding: UTF-8 -*-var = 1while var == 1 : # 該條件永遠(yuǎn)為true,循環(huán)將無限執(zhí)行下去 num = raw_input("Enter a number :") print "You entered: ", numprint "Good bye!"以上實例輸出結(jié)果:
Enter a number :20You entered: 20Enter a number :29You entered: 29Enter a number :3You entered: 3Enter a number between :Traceback (most recent call last): File "test.py", line 5, in<module>num = raw_input("Enter a number :")KeyboardInterrupt</module>注意:以上的無限循環(huán)你可以使用 CTRL+C 來中斷循環(huán)。
循環(huán)使用 else 語句
在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區(qū)別,else 中的語句會在循環(huán)正常執(zhí)行完(即 for 不是通過 break 跳出而中斷的)的情況下執(zhí)行,while … else 也是一樣。
#!/usr/bin/pythoncount = 0while count < 5: print count, " is less than 5" count = count + 1else: print count, " is not less than 5"
以上實例輸出結(jié)果為:
0 is less than 51 is less than 52 is less than 53 is less than 54 is less than 55 is not less than 5
簡單語句組
類似if語句的語法,如果你的while循環(huán)體中只有一條語句,你可以將該語句與while寫在同一行中, 如下所示:
#!/usr/bin/pythonflag = 1while (flag): print 'Given flag is really true!'print "Good bye!"
注意:以上的無限循環(huán)你可以使用 CTRL+C 來中斷循環(huán)。
Python for 循環(huán)語句
Python for循環(huán)可以遍歷任何序列的項目,如一個列表或者一個字符串。
語法:
for循環(huán)的語法格式如下:
for iterating_var in sequence: statements(s)
流程圖:
vcl9sb29w" class="alignnone size-full wp-image-4022" height="351" src="http://www.2cto.com/uploadfile/Collfiles/20151207/2015120708282374.jpg" width="388" />實例:
#!/usr/bin/python# -*- coding: UTF-8 -*-for letter in 'Python': # 第一個實例 print '當(dāng)前字母 :', letterfruits = ['banana', 'apple', 'mango']for fruit in fruits: # 第二個實例 print '當(dāng)前字母 :', fruitprint "Good bye!"
嘗試一下 ?
以上實例輸出結(jié)果:
當(dāng)前字母 : P當(dāng)前字母 : y當(dāng)前字母 : t當(dāng)前字母 : h當(dāng)前字母 : o當(dāng)前字母 : n當(dāng)前字母 : banana當(dāng)前字母 : apple當(dāng)前字母 : mangoGood bye!
通過序列索引迭代
另外一種執(zhí)行循環(huán)的遍歷方式是通過索引,如下實例:
#!/usr/bin/python# -*- coding: UTF-8 -*-fruits = ['banana', 'apple', 'mango']for index in range(len(fruits)): print '當(dāng)前水果 :', fruits[index]print "Good bye!"
以上實例輸出結(jié)果:
當(dāng)前水果 : banana當(dāng)前水果 : apple當(dāng)前水果 : mangoGood bye!
以上實例我們使用了內(nèi)置函數(shù) len() 和 range(),函數(shù) len() 返回列表的長度,即元素的個數(shù),
電腦資料
《Python 循環(huán)語句》(http://m.maimaiyin.com)。 range返回一個序列的數(shù)。
循環(huán)使用 else 語句
在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區(qū)別,else 中的語句會在循環(huán)正常執(zhí)行完(即 for 不是通過 break 跳出而中斷的)的情況下執(zhí)行,while … else 也是一樣。
如下實例:
#!/usr/bin/python# -*- coding: UTF-8 -*-for num in range(10,20): # 迭代 10 到 20 之間的數(shù)字 for i in range(2,num): # 根據(jù)因子迭代 if num%i == 0: # 確定第一個因子 j=num/i # 計算第二個因子 print '%d 等于 %d * %d' % (num,i,j) break # 跳出當(dāng)前循環(huán) else: # 循環(huán)的 else 部分 print num, '是一個質(zhì)數(shù)'
以上實例輸出結(jié)果:
10 等于 2 * 511 是一個質(zhì)數(shù)12 等于 2 * 613 是一個質(zhì)數(shù)14 等于 2 * 715 等于 3 * 516 等于 2 * 817 是一個質(zhì)數(shù)18 等于 2 * 919 是一個質(zhì)數(shù)
Python 循環(huán)嵌套
Python 語言允許在一個循環(huán)體里面嵌入另一個循環(huán)。
Python for 循環(huán)嵌套語法:
for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s)
Python while 循環(huán)嵌套語法:
while expression: while expression: statement(s) statement(s)
你可以在循環(huán)體內(nèi)嵌入其他的循環(huán)體,如在while循環(huán)中可以嵌入for循環(huán), 反之,你可以在for循環(huán)中嵌入while循環(huán)。
實例:
以下實例使用了嵌套循環(huán)輸出2~100之間的素數(shù):
#!/usr/bin/python# -*- coding: UTF-8 -*-i = 2while(i < 100): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print i, " 是素數(shù)" i = i + 1print "Good bye!"
以上實例輸出結(jié)果:
2 是素數(shù)3 是素數(shù)5 是素數(shù)7 是素數(shù)11 是素數(shù)13 是素數(shù)17 是素數(shù)19 是素數(shù)23 是素數(shù)29 是素數(shù)31 是素數(shù)37 是素數(shù)41 是素數(shù)43 是素數(shù)47 是素數(shù)53 是素數(shù)59 是素數(shù)61 是素數(shù)67 是素數(shù)71 是素數(shù)73 是素數(shù)79 是素數(shù)83 是素數(shù)89 是素數(shù)97 是素數(shù)Good bye!