<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 `if...else`語句 > 原文: [https://www.programiz.com/python-programming/if-elif-else](https://www.programiz.com/python-programming/if-elif-else) #### 在本文中,您將學習使用不同形式的`if..else`語句在 Python 程序中創建決策。 ## Python 中的`if...else`語句是什么? 僅當滿足特定條件時,我們才想執行代碼時才需要決策。 `if…elif…else`語句在 Python 中用于決策。 ### Python `if`語句語法 ```py if test expression: statement(s) ``` 在此,程序將求值`test expression`并僅在測試表達式為`True`時才執行語句。 如果測試表達式為`False`,則不執行該語句。 在 Python 中,`if`語句的主體由縮進指示。 主體以縮進開始,第一條未縮進的線標記結束。 Python 將非零值解釋為`True`。`None`和`0`解釋為`False`。 ### Python `if`語句流程圖 ![Flowchart of if statement in Python programming](https://img.kancloud.cn/f6/22/f622d7ddab80f9499201aa80b24e40e7_227x250.png "If Statement Flowchart") Python 編程中`if`語句的流程圖 ### 示例:Python `if`語句 ```py # If the number is positive, we print an appropriate message num = 3 if num > 0: print(num, "is a positive number.") print("This is always printed.") num = -1 if num > 0: print(num, "is a positive number.") print("This is also always printed.") ``` 運行該程序時,輸出為: ```py 3 is a positive number This is always printed This is also always printed. ``` 在上面的示例中,`num > 0`是測試表達式。 僅當`if`的主體的值為`True`時,才執行它。 當變量`num`等于 3 時,測試表達式為`true`,并且執行`if`主體內的語句。 如果變量`num`等于 -1,則測試表達式為`false`,并且跳過`if`主體內部的語句。 `print()`語句位于`if`塊之外(未縮進)。 因此,無論測試表達式如何,都將執行它。 * * * ## Python `if...else`語句 ### `if...else`的語法 ```py if test expression: Body of if else: Body of else ``` `if..else`語句求值`test expression`并僅在測試條件為`True`時才執行`if`的主體。 如果條件為`False`,則執行`else`的主體。 縮進用于分隔塊。 ### Python `if..else`流程圖 ![Flowchart of if...else statement in Python Programming](https://img.kancloud.cn/cb/02/cb023095206e9938b0a3b18b1895469e_279x272.png "if...else statement flowchart") Python 中`if...else`語句的流程圖 ### `if...else`的示例 ```py # Program checks if the number is positive or negative # And displays an appropriate message num = 3 # Try these two variations as well. # num = -5 # num = 0 if num >= 0: print("Positive or Zero") else: print("Negative number") ``` **輸出** ```py Positive or Zero ``` 在上面的示例中,當`num`等于 3 時,測試表達式為`true`,并且執行`if`的主體,其他的`body`被跳過。 如果`num`等于 -5,則測試表達式為`false`,并且執行`else`的主體,并且跳過`if`的主體。 如果`num`等于 0,則測試表達式為`true`,并且執行`if`的主體,而跳過其他的`body`。 * * * ## Python `if...elif...else`語句 ### `if...elif...else`的語法 ```py if test expression: Body of if elif test expression: Body of elif else: Body of else ``` `elif`是`if`的縮寫。 它允許我們檢查多個表達式。 如果`if`的條件為`False`,它將檢查下一個`elif`塊的條件,依此類推。 如果所有條件均為`False`,則執行`else`的主體。 根據條件,僅執行幾個`if...elif...else`塊中的一個塊。 `if`塊只能有一個`else`塊。 但是它可以具有多個`elif`塊。 ### `if...elif...else`的流程圖 ![Flowchart of if...elif....else in Python programming](https://img.kancloud.cn/b5/21/b52129173f52d01b55a2e08562aae7af_412x407.png "if...elif...else statement flowchart") Python 中`if...elif....else`語句的流程圖 ### `if...elif...else`的示例 ```py '''In this program, we check if the number is positive or negative or zero and display an appropriate message''' num = 3.4 # Try these two variations as well: # num = 0 # num = -4.5 if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number") ``` 當變量`num`為正時,將打印正數。 如果`num`等于 0,則打印零。 如果`number`為負,則打印負數。 * * * ## Python 嵌套`if`語句 我們可以在另一個`if...elif...else`語句中包含一個`if...elif...else`語句。 這在計算機編程中稱為嵌套。 這些語句中的任何數目都可以彼此嵌套。 縮進是弄清楚嵌套級別的唯一方法。 它們可能會造成混淆,因此除非有必要,否則必須避免使用它們。 ### Python 嵌套`if`示例 ```py '''In this program, we input a number check if the number is positive or negative or zero and display an appropriate message This time we use nested if statement''' num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number") ``` **輸出 1** ```py Enter a number: 5 Positive number ``` **輸出 2** ```py Enter a number: -1 Negative number ``` **輸出 3** ```py Enter a number: 0 Zero ```
                  <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>

                              哎呀哎呀视频在线观看