<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 循環 本章節將為大家介紹Python循環語句的使用。 Python中的循環語句有 for 和 while。 Python循環語句的控制結構圖如下所示: ![](https://box.kancloud.cn/2015-12-12_566c100a7467f.png) ## while 循環 Python中while語句的一般形式: ``` while 判斷條件: statements ``` 同樣需要注意冒號和縮進。另外,在Python中沒有do..while循環。 以下實例使用了 while 來計算 1 到 100 的總和: ``` #!/usr/bin/env python3 n = 100 sum = 0 counter = 1 while counter <= n: sum = sum + counter counter += 1 print("Sum of 1 until %d: %d" % (n,sum)) ``` 執行結果如下: ``` Sum of 1 until 100: 5050 ``` ## for語句 Python for循環可以遍歷任何序列的項目,如一個列表或者一個字符串。 for循環的一般格式如下: ``` for <variable> in <sequence>: <statements> else: <statements> ``` Python loop循環實例: ``` >>> languages = ["C", "C++", "Perl", "Python"] >>> for x in languages: ... print x ... C C++ Perl Python >>> ``` 以下實例for實例中使用了 break語句,break語句用于跳出當前循環體: ``` #!/usr/bin/env python3 edibles = ["ham", "spam","eggs","nuts"] for food in edibles: if food == "spam": print("No more spam please!") break print("Great, delicious " + food) else: print("I am so glad: No spam!") print("Finally, I finished stuffing myself") ``` 執行腳本后,在循環到 "spam"時會跳出循環體: ``` Great, delicious ham No more spam please! Finally, I finished stuffing myself ``` ## range()函數 如果你需要遍歷數字序列,可以使用內置range()函數。它會生成數列,例如: ``` >>> for i in range(5): ... print(i) ... 0 1 2 3 4 ``` 你也可以使用range指定區間的值: ``` >>> for i in range(5,9) : print(i) 5 6 7 8 >>> ``` 也可以使range以指定數字開始并指定不同的增量(甚至可以是負數;有時這也叫做'步長'): ``` >>> for i in range(0, 10, 3) : print(i) 0 3 6 9 >>> ``` 負數: ``` >>> for i in range(-10, -100, -30) : print(i) -10 -40 -70 >>> ``` 您可以結合range()和len()函數以遍歷一個序列的索引,如下所示: ``` >>> a = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i in range(len(a)): ... print(i, a[i]) ... 0 Mary 1 had 2 a 3 little 4 lamb ``` 還可以使用range()函數來創建一個列表: ``` >>> list(range(5)) [0, 1, 2, 3, 4] >>> ``` ## break和continue語句及循環中的else子句 break語句可以跳出for和while的循環體。如果你從for或while循環中終止,任何對應的循環else塊將不執行。 continue語句被用來告訴Python跳過當前循環塊中的剩余語句,然后繼續進行下一輪循環。 循環語句可以有else子句;它在窮盡列表(以for循環)或條件變為假(以while循環)循環終止時被執行,但循環被break終止時不執行.如下查尋質數的循環例子: ``` >>> for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print(n, 'equals', x, '*', n//x) ... break ... else: ... # 循環中沒有找到元素 ... print(n, 'is a prime number') ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 ``` ## pass語句 pass語句什么都不做。它只在語法上需要一條語句但程序不需要任何操作時使用.例如: ``` >>> while True: ... pass # 等待鍵盤中斷 (Ctrl+C) ``` 最小的類: ``` >>> class MyEmptyClass: ... pass ```
                  <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>

                              哎呀哎呀视频在线观看