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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                --: 作者:不知百度知 2022年9月 [TOC] ## 1. if三元表達式(三元運算符) ### 1.1 說明 使用一行代碼快速判斷,更換復雜的多行if語句,使代碼能夠簡單地維護。 ### 1.2 其它語言的三元表達式 > 判段的條件 ? 條件為真時的結果:條件為假時的結果 ```java public class java { public static void main(String[] args){ int x = 100; int y = 101; int MAX = (x > y)? x: y; System.out.println("MAX:" + MAX); } } ``` ### 1.2 python的三元表達式 > 條件為真時的結果 if 判段的條件 else 條件為假時的結果 ```python x = 4 y = 99 if x > 3 else 999 ``` ## 2. 變量的真假 在 Python 看來,只有變量的值為0或空、長度為0時,才會被看作假(注意引號、括號里邊啥都沒有,連空格都不要有!): ` False None 0 "" '' () [] {} ` 其他一切都被解釋為真! 測試示例: ``` if 0: print('True1') else: print('False1') if "": print('True2') else: print('False2') if []: print('True3') else: print('False3') if {}: print('True4') else: print('False4') if (): print('True5') else: print('False5') ``` 結果為 ``` False1 False2 False3 False4 False5 ``` 所以我們經常會見到別人的代碼這樣判斷 ``` if 變量: xxx ``` 比如: ``` a = 10 if a: # a大于0,條件為真 a -= 1 else: a = 10 ``` ## 3. 短路邏輯(short-circuit logic) 邏輯操作符有個有趣的特性:在不需要求值的時候不進行操作。 <div style='height:20px'></div> 這么說可能比較“高深”,舉個例子,表達式 x and y ,需要 x 和 y 兩個變量同時為真 (True) 的時候,結果才為真。 <div style='height:20px'></div> 因此,如果當 x 變量得知是假 (False) 的時候,表達式就會立刻返回 False ,而不用去管 y 變量的值。 這種行為被稱為短路邏輯( short-circuit logic )或者惰性求值( lazy evaluation )。 <div style='height:20px'></div> 這種行為同樣也應用于 or 操作符,Python 的做法是如果 x 為假,表達式會返回 x 的值 (0) ,否則它就會返回 y 的值。 ``` a = 3 and 4 # 結果為4 b = 3 or 4 # 結果為3 ``` ## 4. 對象的類型判斷 - type(對象) == 類型 - isinstance(對象, 類型) python中type可以獲得一個對象的數據類型,isinstance可以判斷一個對象的數據類型,他們的區別有兩點: ### 4.1. isinstance更加靈活 type只是返回一個對象的數據類型,而isinstance可以判斷這個對象的數據類型是否為某幾個數據類型中的一個。 假設我們要判斷一個對象的數據類型是否為int或者float,兩個函數的寫法示例如下 ~~~python a = 4 # 使用type if type(a) == int or type(a) == float: print('yes') # 使用isinstance if isinstance(a, (int, float)): print('yes') ~~~ 顯然,在這種場景下,isinstance更有優勢 ### 4.2. 判斷存在繼承關系的情況 ~~~python class A: pass class B(A): pass a = A() b = B() print(type(b) == A) # False print(isinstance(b, A)) # True ~~~ B是A的子類, type(b)返回的是類B, 不等于A, 但B是A的子類,因此,我們可以認為b也是A的對象,面對這種存在繼承關系的情況,應當使用isinstance。 官方推薦用第二種方法判斷變量的類型,現在先有個初步印象,學到后面再作詳細解釋。 ## 5. 運算符優先級 以下表格列出了從最高到最低優先級的所有運算符: | 運算符 | 描述 | | --- | --- | | \*\* | 指數 (最高優先級) | | ~ + - | 按位翻轉, 一元加號和減號 (最后兩個的方法名為 +@ 和 -@) | | \* / % // | 乘,除,取模和取整除 | | \+ - | 加法減法 | | \>> << | 右移,左移運算符 | | & | 位 'AND' | | ^ | | 位運算符 | | >= | 比較運算符 | | <> == != | 等于運算符 | | \= %= /= //= -= += \*= \*\*= | 賦值運算符 | | is is not | 身份運算符 | | in not in | 成員運算符 | | not and or | 邏輯運算符 | 常見幾種類型運算符優先級示意圖 ![](https://img.kancloud.cn/8c/b2/8cb2f457186af753c267f3a3dea2d696_845x490.png) > 練習 - 請用最快速度說出答案: not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 <div style='height:100px'></div> not or and 的優先級是不同的: not > and > or,我們按照優先級給它們加上括號: ``` (not 1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9) == 0 or 0 or 4 or 6 or 9 == 4 ``` 【Tip】 溫馨提示:為了更好的表達你的程序,有些括號還是不能省下的,畢竟不是所有程序員都跟你一樣都將優先級爛透于心的。
                  <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>

                              哎呀哎呀视频在线观看