<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 控制語句 > 原文: [https://thepythonguru.com/python-control-statements/](https://thepythonguru.com/python-control-statements/) * * * 于 2020 年 1 月 7 日更新 * * * 程序根據某些條件執行語句是很常見的。 在本節中,我們將了解 Python 中的`if else`語句。 但是在我們需要了解關系運算符之前。 關系運算符使我們可以比較兩個對象。 | 符號 | 描述 | | --- | --- | | `<=` | 小于或等于 | | `<` | 小于 | | `>` | 大于 | | `>=` | 大于或等于 | | `==` | 等于 | | `!=` | 不等于 | 比較的結果將始終為布爾值,即`True`或`False`。 請記住,`True`和`False`是用于表示布爾值的 python 關鍵字。 讓我們舉一些例子: ```py >>> 3 == 4 False >>> 12 > 3 True >>> 12 == 12 True >>> 44 != 12 True ``` 現在您可以處理`if`語句了。 `if`語句的語法如下所示: ```py if boolean-expression: #statements else: #statements ``` **注意**: `if`塊中的每個語句都必須使用相同數量的空格縮進,否則將導致語法錯誤。 這與 Java,C,C# 等使用花括號(`{}`)的語言完全不同。 現在來看一個例子 ```py i = 10 if i % 2 == 0: ? ?print("Number is even") else: ? ?print("Number is odd") ``` 在這里您可以看到,如果數字為偶數,則將打印`"Number is even"`。 否則打印`"Number is odd"`。 **注意**: `else`子句是可選的,您可以根據需要僅使用`if`子句,如下所示: ```py if today == "party": ? ? print("thumbs up!") ``` 在此,如果`today`的值為`"party"`,則將打印`thumbs up!`,否則將不打印任何內容。 如果您的程序需要檢查一長串條件,那么您需要使用`if-elif-else`語句。 ```py if boolean-expression: ? ?#statements elif boolean-expression: ? ?#statements elif boolean-expression: ? ?#statements elif boolean-expression: ? ?#statements else: ? ?#statements ``` 您可以根據程序要求添加`elif`條件。 這是一個說明`if-elif-else`語句的示例。 ```py today = "monday" if today == "monday": ? ?print("this is monday") elif today == "tuesday": ? ?print("this is tuesday") elif today == "wednesday": ? ?print("this is wednesday") elif today == "thursday": ? ?print("this is thursday") elif today == "friday": ? ?print("this is friday") elif today == "saturday": ? ?print("this is saturday") elif today == "sunday": ? ?print("this is sunday") else: ? ?print("something else") ``` ## 嵌套`if`語句 * * * 您可以將`if`語句嵌套在另一個`if`語句中,如下所示: ```py today = "holiday" bank_balance = 25000 if today == "holiday": ? ?if bank_balance > 20000: ? ? ? print("Go for shopping") ? ?else: ? ? print("Watch TV") else: print("normal working day") ``` 在下一篇文章中,我們將學習 [Python 函數](/python-functions/)。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看