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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Python 循環 > 原文: [https://thepythonguru.com/python-loops/](https://thepythonguru.com/python-loops/) * * * 于 2020 年 1 月 7 日更新 * * * Python 只有兩個循環: 1. `for`循環 2. `while`循環 ## `for`循環 * * * `for`循環語法: ```py for i in iterable_object: ? ?# do something ``` **注意**: `for`和`while`循環內的所有語句必須縮進相同的空格數。 否則,將拋出`SyntaxError`。 讓我們舉個例子 ```py my_list = [1,2,3,4] for i in my_list: ? ? print(i) ``` **預期輸出**: ```py 1 2 3 4 ``` 這是`for`循環的工作方式: 在第一次迭代中,為`i`分配了值`1`,然后執行了`print`語句。 在第二次迭代中,為`i`賦值`2`,然后再次執行`print`語句。 此過程將繼續進行,直到列表中沒有其他元素并且存在`for`循環為止。 ## `range(a, b)`函數 * * * `range(a, b)`函數從`a`,`a + 1`,`a+ 2` ....,`b - 2`和`b - 1`返回整數序列。 例如: ```py for i in range(1, 10): ? ? print(i) ``` **預期輸出**: ```py 1 2 3 4 5 6 7 8 9 ``` 您還可以通過僅提供一個參數來使用`range()`函數,如下所示: ```py >>> for i in range(10): ... ? ? ? ?print(i) 0 1 2 3 4 5 6 7 8 9 ``` 循環打印的范圍是 0 到 9。 `range(a, b)`函數具有可選的第三個參數,用于指定步長。 例如: ```py for i in range(1, 20, 2): ? ? print(i) ``` **預期輸出**: ```py 1 3 5 7 9 11 13 15 17 19 ``` ## `While`循環 * * * 句法: ```py while condition: ? ? # do something ``` `while`循環在其中繼續執行語句,直到條件變為假。 在每次迭代條件檢查之后,如果其條件為`True`,則會在`while`循環中再次執行語句。 讓我們舉個例子: ```py count = 0 while count < 10: ? ? print(count) ? ? count += 1 ``` **預期輸出**: ```py 0 1 2 3 4 5 6 7 8 9 ``` 在此處,將繼續打印,直到`count`小于`10`為止。 ## `break`語句 * * * `break`語句允許突破循環。 ```py count = 0 while count < 10: count += 1 ? ? if count == 5: ? ? ? ? ?break? ? ? ? print("inside loop", count) print("out of while loop") ``` 當`count`等于`5`時,如果條件求值為`True`,并且`break`關鍵字跳出循環。 **預期輸出**: ```py inside loop 1 inside loop 2 inside loop 3 inside loop 4 out of while loop ``` ## `continue`語句 * * * 當在循環中遇到`continue`語句時,它將結束當前迭代,并且程序控制將轉到循環主體的末尾。 ```py count = 0 while count < 10: ? ? count += 1 ? ? if count % 2 == 0: ? ? ? ? ? ?continue ? ? print(count) ``` **預期輸出**: ```py 1 3 5 7 9 ``` 如您所見,當`count % 2 == 0`時,將執行`continue`語句,該語句導致當前迭代結束,并且控件繼續進行下一個迭代。 在下一課中,我們將學習 [Python 數學函數](/python-mathematical-function/)。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看