<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python?While循環語句 Python 編程中 while 語句用于循環執行程序,即在某條件下,循環執行某段程序,以處理需要重復處理的相同任務。其基本形式為: ~~~ while 判斷條件: 執行語句…… ~~~ 執行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true。 當判斷條件假false時,循環結束。 執行流程圖如下: ![](https://box.kancloud.cn/2015-12-12_566c0ff28bd21.jpg) 實例: ~~~ #!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" ~~~ 以上代碼執行輸出結果: ~~~ The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye! ~~~ while 語句時還有另外兩個重要的命令 continue,break 來跳過循環,continue 用于跳過該次循環,break 則是用于退出循環,此外"判斷條件"還可以是個常值,表示循環必定成立,具體用法如下: ~~~ # continue 和 break 用法 i = 1 while i < 10: i += 1 if i%2 > 0: # 非雙數時跳過輸出 continue print i # 輸出雙數2、4、6、8、10 i = 1 while 1: # 循環條件為1必定成立 print i # 輸出1~10 i += 1 if i > 10: # 當i大于10時跳出循環 break ~~~ ## 無限循環 如果條件判斷語句永遠為 true,循環將會無限的執行下去,如下實例: ~~~ #!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # 該條件永遠為true,循環將無限執行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!" ~~~ 以上實例輸出結果: ~~~ Enter a number :20 You entered: 20 Enter a number :29 You entered: 29 Enter a number :3 You entered: 3 Enter a number between :Traceback (most recent call last): File "test.py", line 5, in <module> num = raw_input("Enter a number :") KeyboardInterrupt ~~~ 注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。 ## 循環使用 else 語句 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行,while … else 也是一樣。 ~~~ #!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" ~~~ 以上實例輸出結果為: ~~~ 0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5 ~~~ ## 簡單語句組 類似if語句的語法,如果你的while循環體中只有一條語句,你可以將該語句與while寫在同一行中, 如下所示: ~~~ #!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!" ~~~ 注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。 # Python?for 循環語句 Python for循環可以遍歷任何序列的項目,如一個列表或者一個字符串。 語法: for循環的語法格式如下: ~~~ for iterating_var in sequence: statements(s) ~~~ 流程圖: ![](https://box.kancloud.cn/2015-12-12_566c0ff2c7151.jpg) 實例: ~~~ #!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': # 第一個實例 print '當前字母 :', letter fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # 第二個實例 print '當前字母 :', fruit print "Good bye!" ~~~ [](http://www.runoob.com/try/showpy.php?filename=for_demo1&language=py)嘗試一下 ? 以上實例輸出結果: ~~~ 當前字母 : P 當前字母 : y 當前字母 : t 當前字母 : h 當前字母 : o 當前字母 : n 當前字母 : banana 當前字母 : apple 當前字母 : mango Good bye! ~~~ ## 通過序列索引迭代 另外一種執行循環的遍歷方式是通過索引,如下實例: ~~~ #!/usr/bin/python # -*- coding: UTF-8 -*- fruits = ['banana', 'apple', 'mango'] for index in range(len(fruits)): print '當前水果 :', fruits[index] print "Good bye!" ~~~ 以上實例輸出結果: ~~~ 當前水果 : banana 當前水果 : apple 當前水果 : mango Good bye! ~~~ 以上實例我們使用了內置函數 len() 和 range(),函數 len() 返回列表的長度,即元素的個數。 range返回一個序列的數。 ## 循環使用 else 語句 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行,while … else 也是一樣。 如下實例: ~~~ #!/usr/bin/python # -*- coding: UTF-8 -*- for num in range(10,20): # 迭代 10 到 20 之間的數字 for i in range(2,num): # 根據因子迭代 if num%i == 0: # 確定第一個因子 j=num/i # 計算第二個因子 print '%d 等于 %d * %d' % (num,i,j) break # 跳出當前循環 else: # 循環的 else 部分 print num, '是一個質數' ~~~ 以上實例輸出結果: ~~~ 10 等于 2 * 5 11 是一個質數 12 等于 2 * 6 13 是一個質數 14 等于 2 * 7 15 等于 3 * 5 16 等于 2 * 8 17 是一個質數 18 等于 2 * 9 19 是一個質數 ~~~ # Python?循環嵌套 Python 語言允許在一個循環體里面嵌入另一個循環。 Python for 循環嵌套語法: ~~~ for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s) ~~~ Python while 循環嵌套語法: ~~~ while expression: while expression: statement(s) statement(s) ~~~ 你可以在循環體內嵌入其他的循環體,如在while循環中可以嵌入for循環, 反之,你可以在for循環中嵌入while循環。 實例: 以下實例使用了嵌套循環輸出2~100之間的素數: ~~~ #!/usr/bin/python # -*- coding: UTF-8 -*- i = 2 while(i < 100): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print i, " 是素數" i = i + 1 print "Good bye!" ~~~ 以上實例輸出結果: ~~~ 2 是素數 3 是素數 5 是素數 7 是素數 11 是素數 13 是素數 17 是素數 19 是素數 23 是素數 29 是素數 31 是素數 37 是素數 41 是素數 43 是素數 47 是素數 53 是素數 59 是素數 61 是素數 67 是素數 71 是素數 73 是素數 79 是素數 83 是素數 89 是素數 97 是素數 Good bye! ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看