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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python `for`循環 > 原文: [https://www.programiz.com/python-programming/for-loop](https://www.programiz.com/python-programming/for-loop) #### 在本文中,您將學習使用`for`循環的不同變體對元素序列進行迭代。 ## 什么是 Python 中的`for`循環? Python 中的`for`循環用于迭代序列([列表](https://www.programiz.com/python-programming/list),[元組](https://www.programiz.com/python-programming/tuple),[字符串](https://www.programiz.com/python-programming/string))或其他可迭代對象。 在序列上進行迭代稱為遍歷。 ### `for`循環的語法 ```py for val in sequence: Body of for ``` 在這里,`val`是在每次迭代中獲取序列內項目值的變量。 循環繼續,直到我們到達序列中的最后一項。 `for`循環的主體使用縮進與其余代碼分開。 ### `for`循環流程圖 ![Flowchart of for Loop in Python programming](https://img.kancloud.cn/af/d3/afd3443cc2fbab478b3e94aa7116ae34_219x384.png "for Loop Flowchart") Python 中`for`循環的流程圖 ### 示例:Python `for`循環 ```py # Program to find the sum of all numbers stored in a list # List of numbers numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11] # variable to store the sum sum = 0 # iterate over the list for val in numbers: sum = sum+val print("The sum is", sum) ``` 運行該程序時,輸出為: ```py The sum is 48 ``` * * * ## `range()`函數 我們可以使用`range()`函數生成數字序列。`range(10)`將生成 0 到 9 之間的數字(10 個數字)。 我們還可以將開始,停止和步長定義為`range(start, stop,step_size)`。 如果未提供,則`step_size`默認為 1。 從某種意義上講,`range`對象是“惰性”的,因為在創建它時,它不會生成它“包含”的每個數字。 但是,它不是迭代器,因為它支持`in`,`len`和`__getitem__`操作。 此函數不會將所有值存儲在內存中; 這將是低效的。 因此它會記住開始,停止,步長并在旅途中生成下一個數字。 要強制此函數輸出所有項目,我們可以使用函數`list()`。 以下示例將闡明這一點。 ```py print(range(10)) print(list(range(10))) print(list(range(2, 8))) print(list(range(2, 20, 3))) ``` **輸出** ```py range(0, 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 3, 4, 5, 6, 7] [2, 5, 8, 11, 14, 17] ``` 我們可以在`for`循環中使用`range()`函數來迭代數字序列。 它可以與`len()`函數結合使用索引來遍歷序列。 這是一個例子。 ```py # Program to iterate through a list using indexing genre = ['pop', 'rock', 'jazz'] # iterate over the list using index for i in range(len(genre)): print("I like", genre[i]) ``` **輸出**: ```py I like pop I like rock ?I like jazz ``` * * * ## 循環與`else` `for`循環也可以具有可選的`else`塊。 如果`for`循環的序列中的項目用盡,則會執行`else`部分。 `break`關鍵字可用于停止`for`循環。 在這種情況下,其他部分將被忽略。 因此,如果沒有中斷發生,則`for`循環的`else`部分將運行。 這是一個例子來說明這一點。 ```py digits = [0, 1, 5] for i in digits: print(i) else: print("No items left.") ``` 當你運行程序時,輸出將是: ```py 0 1 5 No items left. ``` 在這里,`for`循環打印列表中的項目,直到循環用盡。 當`for`循環用完時,它將執行`else`中的代碼塊并打印。 僅當未執行`break`關鍵字時,此`for...else`語句才能與`break`關鍵字一起使用以運行`else`塊。 讓我們舉個例子: ```py # program to display student's marks from record student_name = 'Soyuj' marks = {'James': 90, 'Jules': 55, 'Arthur': 77} for student in marks: if student == student_name: print(marks[student]) break else: print('No entry with that name found.') ``` **輸出**: ```py No entry with that name found. ```
                  <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>

                              哎呀哎呀视频在线观看