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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Python 運算符 > 原文: [http://zetcode.com/lang/python/operators/](http://zetcode.com/lang/python/operators/) 在 Python 編程教程的這一部分中,我們介紹了 Python 運算符。 運算符是特殊符號,表示已執行某個過程。 編程語言的運算符來自數學。 應用處理數據。 運算符用于處理數據。 在 Python 中,我們有幾種類型的運算符: * 算術運算符 * 布爾運算符 * 關系運算符 * 按位運算符 一個運算符可以有一個或兩個操作數。 操作數是運算符的輸入(參數)之一。 僅使用一個操作數的那些運算符稱為一元運算符。 那些使用兩個操作數的對象稱為二進制運算符。 +和-可以是加減運算符,也可以是一元符號運算符。 這取決于實際情況。 ```py >>> 2 2 >>> +2 2 >>> ``` 加號可以用來表示我們有一個正數。 但是它通常不被使用。 減號更改值的符號。 ```py >>> a = 1 >>> -a -1 >>> -(-a) 1 ``` 乘法和加法運算符是二進制運算符的示例。 它們與兩個操作數一起使用。 ```py >>> 3 * 3 9 >>> 3 + 3 6 ``` ## Python 賦值運算符 賦值運算符`=`將值賦給變量。 在數學中,`=`運算符具有不同的含義。 在等式中,`=`運算符是一個相等運算符。 等式的左側等于右側。 ```py >>> x = 1 >>> x 1 ``` 在這里,我們為`x`變量分配一個數字。 ```py >>> x = x + 1 >>> x 2 ``` 先前的表達式在數學上沒有意義。 但這在編程中是合法的。 該表達式意味著我們向`x`變量加 1。 右邊等于 2,并且 2 分配給`x`。 ```py >>> a = b = c = 4 >>> print(a, b, c) 4 4 4 ``` 可以為多個變量分配一個值。 ```py >>> 3 = y File "<stdin>", line 1 SyntaxError: can't assign to literal ``` 此代碼示例導致語法錯誤。 我們無法為字面值分配值。 ## Python 算術運算符 下表是 Python 編程語言中的算術運算符表。 | 符號 | 名稱 | | --- | --- | | `+` | 加成 | | `-` | 減法 | | `*` | 乘法 | | `/` | 除法 | | `//` | 整數除法 | | `%` | 模數 | | `**` | 乘方 | 以下示例顯示了算術運算。 `arithmetic.py` ```py #!/usr/bin/env python # arithmetic.py a = 10 b = 11 c = 12 add = a + b + c sub = c - a mult = a * b div = c / 3 power = a ** 2 print(add, sub, mult, div) print(power) ``` 所有這些都是數學上已知的運算符。 ```py $ ./arithmetic.py 33 2 110 4.0 100 ``` 共有三位運算符負責部門劃分。 `division.py` ```py #!/usr/bin/env python # division.py print(9 / 3) print(9 / 4) print(9 // 4) print(9 % 4) ``` 該示例演示了除法運算符。 ```py print(9 / 4) ``` 結果為 2.25。 在 Python 2.x 中,`/`運算符是整數除法運算符。 這在 Python 3 中已更改。在 Python 3 中,`/`運算符返回一個十進制數。 ```py print(9 // 4) ``` `//`運算符是 Python 3 中的整數運算符。 ```py print(9 % 4) ``` `%`運算符稱為模運算符。 它找到一個數除以另一個的余數。 `9 % 4`,9 模 4 為 1,因為 4 兩次進入 9 且余數為 1。 ```py $ ./division.py 3.0 2.25 2 1 ``` ```py >>> 'return' + 'of' + 'the' + 'king' 'returnoftheking' ``` 加法運算符還可用于連接字符串。 ```py >>> 3 + ' apples' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' ``` 我們不能添加整數和字符串。 這導致`TypeError`。 ```py >>> str(3) + ' apples' '3 apples' ``` 為了使示例生效,必須使用`str()`函數將數字轉換為字符串。 另一方面,乘法運算符可以與字符串和數字一起使用。 ```py >>> 'dollar ' * 5 'dollar dollar dollar dollar dollar ' ``` ## Python 布爾運算符 在 Python 中,我們具有`and`,`or`和`not`布爾運算符。 使用布爾運算符,我們可以執行邏輯運算。 這些最常與`if`和`while`關鍵字一起使用。 `andop.py` ```py #!/usr/bin/env python # andop.py print(True and True) print(True and False) print(False and True) print(False and False) ``` 此示例顯示了邏輯和運算符。 僅當兩個操作數均為`True`時,邏輯和運算符才對`True`求值。 ```py $ ./andop.py True False False False ``` 如果兩個操作數中的任何一個為`True`,則邏輯或運算符求值為`True`。 `orop.py` ```py #!/usr/bin/env python # orop.py print(True or True) print(True or False) print(False or True) print(False or False) ``` 如果運算符的一方為`True`,則操作的結果為`True`。 ```py $ ./orop.py True True True False ``` 否定運算符`not`使`True` `False`和`False` `True`。 `negation.py` ```py #!/usr/bin/env python # negation.py print(not False) print(not True) print(not ( 4 < 3 )) ``` 該示例顯示了`not`運算符的作用。 ```py $ ./negation.py True False True ``` 并且,或者對短路進行了求值。 短路求值意味著僅當第一個參數不足以確定表達式的值時,才求值第二個參數:當和的第一個參數求值為`false`時,總值必須為`false`; 當或的第一個參數為`true`時,總值必須為`true`。 以下示例演示了簡短的短路求值。 `short_circuit.py` ```py #!/usr/bin/env python # short_circuit.py x = 10 y = 0 if (y != 0 and x/y < 100): print("a small value") ``` 表達式的第一部分計算為`False`。 表達式的第二部分不計算。 否則,我們將得到`ZeroDivisionError`。 ## Python 關系運算符 關系運算符用于比較值。 這些運算符總是產生布爾值。 | 符號 | 含義 | | --- | --- | | `<` | 小于 | | `<=` | 小于或等于 | | `>` | 大于 | | `>=` | 大于或等于 | | `==` | 等于 | | `!=` | 不等于 | | `is` | 對象身份 | | `is not` | 否定對象身份 | 上表顯示了 Python 關系運算符。 ```py >>> 3 < 4 True >>> 4 == 3 False >>> 4 >= 3 True ``` 如前所述,關系運算符返回布爾值:`True`或`False`。 注意,關系運算符不限于數字。 我們也可以將它們用于其他對象。 盡管它們可能并不總是有意義的。 ```py >>> "six" == "six" True >>> 'a' < 'b' True ``` 我們也可以比較字符串對象。 ```py >>> 'a' < 'b' ``` 這里到底發生了什么? 計算機不知道字符或字符串。 對于他們來說,一切都只是數字。 字符是存儲在特定表中的特殊數字,例如 ASCII。 ```py >>> 'a' > 6 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() > int() ``` 在不同的數據類型上不能使用關系運算符。 該代碼導致一個`TypeError`。 `compare.py` ```py #!/usr/bin/env python # compare.py print('a' < 'b') print("a is:", ord('a')) print("b is:", ord('b')) ``` 在內部,a 和 b 字符是數字。 因此,當我們比較兩個字符時,我們將比較它們的存儲數字。 內置的`ord()`函數返回單個字符的 ASCII 值。 ```py $ ./compare.py True a is: 97 b is: 98 ``` 實際上,我們比較兩個數字:97 和 98。 ```py >>> "ab" > "aa" True ``` 假設我們有一個包含更多字符的字符串。 如果前幾個字符相等,我們將比較下一個字符。 在我們的情況下,第二個位置的`b`字符的值比`a`字符大。 這就是為什么`"ab"`字符串大于`"aa"`字符串的原因。 當然,以這種方式比較字符串沒有多大意義。 但這在技術上是可能的。 ## Python 對象身份運算符 對象標識運算符`is`和`not is`檢查其操作符是否是同一對象。 `object_identity.py` ```py #!/usr/bin/env python # object_identity.py print(None == None) print(None is None) print(True is True) print([] == []) print([] is []) print("Python" is "Python") ``` `==`運算符測試是否相等,而`is`運算符測試對象身份。 我們是否在談論同一對象。 請注意,更多變量可能引用同一對象。 ```py $ ./object_identity.py True True True True False True ``` 輸出可能會讓您感到驚訝。 在 Python 語言中,只有一個`None`和一個`True`對象。 這就是`True`相等且與`True`相同的原因。 無論如何,那里只有一個真理。 空列表`[]`等于另一個空列表`[]`。 但是它們并不相同。 Python 已將它們放入兩個不同的內存位置。 它們是兩個不同的對象。 因此,`is`運算符返回`False`。 另一方面,`"Python"`是`"Python"`返回`True`。 這是由于優化:如果兩個字符串字面值相等,則將它們放置在相同的內存位置。 由于字符串是不可變的實體,因此不會造成任何傷害。 ## Python 成員運算符 成員運算符`in`和`not in`測試序列中的成員性,例如字符串,列表或元組。 `membership.py` ```py #!/usr/bin/env python # membership.py items = ("coin", "book", "pencil", "spoon", "paper") if "coin" in items: print("There is a coin in the tuple") else: print("There is no coin in the tuple") if "bowl" not in items: print("There is no bowl in the tuple") else: print("There is a bowl in the tuple") ``` 通過成員運算符,我們可以測試元組中是否存在某個項目。 ```py if "coin" in items: ``` 使用`in`運算符,我們檢查`items`元組中是否存在`"coin"`。 ```py if "bowl" not in items: ``` 使用`not in`運算符,我們檢查`items`元組中是否不存在`"bowl"`。 ```py $ ./membership.py There is a coin in the tuple There is no bowl in the tuple ``` 這是示例的輸出。 ## Python 三元運算符 三元運算符是一個簡單的條件分配語句。 ```py exp1 if condition else exp2 ``` 如果條件為`true`,則對`exp1`求值并返回結果。 如果條件為假,則求值`exp2`并返回其結果。 `ternary.py` ```py #!/usr/bin/env python # ternary.py age = 31 adult = True if age >= 18 else False print("Adult: {0}".format(adult)) ``` 在許多國家,成年取決于您的年齡。 如果您的年齡超過特定年齡,則您已經成年。 對于三元運算符,這是一種情況。 ```py adult = True if age >= 18 else False ``` 首先求值條件。 如果年齡大于或等于 18,則返回`True`。 如果不是,則返回`else`關鍵字后面的值。 然后,將返回的值分配給`adult`變量。 ```py $ ./ternary.py Adult: True ``` 31 歲的成年人是成年人。 ## Python 按位運算符 小數對人類是自然的。 二進制數是計算機固有的。 二進制,八進制,十進制或十六進制符號僅是相同數字的符號。 按位運算符使用二進制數的位。 我們有二進制邏輯運算符和移位運算符。 在 Python 等高級語言中很少使用按位運算符。 | 符號 | 含義 | | --- | --- | | `~` | 按位取反 | | `^` | 按位異或 | | `&` | 按位與 | | <code>&#124;</code> | 按位或 | | `<<` | 左移 | | `>>` | 右移 | 按位取反運算符分別將 1 更改為 0,將 0 更改為 1。 ```py >>> ~7 -8 >>> ~-8 7 ``` 運算符恢復數字 7 的所有位。這些位之一還確定數字是否為負。 如果我們再一次對所有位取反,我們將再次得到 7。 按位,運算符在兩個數字之間進行逐位比較。 僅當操作數中的兩個對應位均為 1 時,位位置的結果才為 1。 ```py 00110 & 00011 = 00010 ``` 第一個數字是二進制表示法 6,第二個數字是 3,最終結果是 2。 ```py >>> 6 & 3 2 >>> 3 & 6 2 ``` 按位或運算符在兩個數字之間進行逐位比較。 如果操作數中的任何對應位為 1,則位位置的結果為 1。 ```py 00110 | 00011 = 00111 ``` 結果為`00110`或十進制 7。 ```py >>> 6 | 3 7 ``` 按位互斥或運算符在兩個數字之間進行逐位比較。 如果操作數中對應位中的一個或另一個(但不是全部)為 1,則位位置的結果為 1。 ```py 00110 ^ 00011 = 00101 ``` 結果為`00101`或十進制 5。 ```py >>> 6 ^ 3 5 ``` 如前所述,Python 和其他高級語言很少使用按位運算符。 但是,在某些情況下會使用它們。 一個示例是掩碼。 掩碼是特定的位模式。 它確定是否設置了某些屬性。 讓我們舉一個 GUI 編程的例子。 `bitwise_or.py` ```py #!/usr/bin/env python # bitwise_or.py import wx app = wx.App() window = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX) window.Show(True) app.MainLoop() ``` 這是 wxPython 代碼的一個小示例。 `wx.MAXIMIZE_BOX`,`wx.RESIZE_BORDER`,`wx.SYSTEM_MENU`,`wx.CAPTION`和`wx.CLOSE_BOX`是常數。 按位或運算符將所有這些常數添加到掩碼中。 在我們的例子中,所有這些屬性都是使用按位或運算符設置的,并應用于`wx.Frame`小部件。 最后,我們還有按位移位運算符。 按位移位運算符向右或向左移位。 ```py number << n : multiply number 2 to the nth power number >> n : divide number by 2 to the nth power ``` 這些運算符也稱為算術移位。 ```py 00110 >> 00001 = 00011 ``` 我們將數字 6 的每個位向右移動。 等于將 6 除以 2。結果為`00011`或十進制 3。 ```py >>> 6 >> 1 3 ``` ```py 00110 << 00001 = 01100 ``` 我們將數字 6 的每個位向左移動。 等于將數字 6 乘以 2。結果為`01100`或十進制 12。 ```py >>> 6 << 1 12 ``` ## Python 復合賦值運算符 復合賦值運算符由兩個運算符組成。 他們是速記員。 ```py >>> i = 1 >>> i = i + 1 >>> i 2 >>> i += 1 >>> i 3 ``` `+=`復合運算符是這些速記運算符之一。 其他復合運算符是: ```py -= *= /= //= %= **= &= |= ^= >>= <<= ``` ## Python 運算符優先級 運算符優先級告訴我們首先求值哪個運算符。 優先級對于避免表達式中的歧義是必要的。 以下表達式 28 或 40 的結果是什么? ```py 3 + 5 * 5 ``` 像數學中一樣,乘法運算符的優先級高于加法運算符。 結果是 28。 ```py (3 + 5) * 5 ``` 要更改求值順序,可以使用方括號。 方括號內的表達式始終首先被求值。 以下列表顯示了 Python 中的運算符優先級。 ```py unary + - ~ ** * / % + - >> << & ^ | < <= == >= > != is not and or ``` 同一行上的運算符具有相同的優先級。 優先級從低到高。 `precedence.py` ```py #!/usr/bin/env python # precedence.py print(3 + 5 * 5) print((3 + 5) * 5) print(2 ** 3 * 5) print(not True or True) print(not (True or True)) ``` 在此代碼示例中,我們顯示一些常見的表達式。 每個表達式的結果取決于優先級。 ```py print(2 ** 3 * 5) ``` 冪運算符的優先級高于乘法運算符。 首先,對`2 ** 3`求值,返回 8。然后將結果乘以 5,結果為 40。 ```py print(not True or True) ``` 在這種情況下,`not`運算符具有更高的優先級。 首先,將第一個`True`值取反為`False`,然后`or`運算符組合`False`和`True`,最后得到`True`。 ```py $ ./precedence.py 28 40 40 True False ``` 關系運算符的優先級高于邏輯運算符。 `positive.py` ```py #!/usr/bin/env python # positive.py a = 1 b = 2 if (a > 0 and b > 0): print("a and b are positive integers") ``` `and`運算符等待兩個布爾值。 如果其中一個操作數不是布爾值,則會出現語法錯誤。 在 Python 中,關系運算符在邏輯與之前進行求值。 ```py $ ./positive.py a and b are positive integers ``` ## Python 關聯規則 有時,優先級不能令人滿意地確定表達式的結果。 還有另一個規則稱為關聯性。 運算符的關聯性確定優先級與相同的運算符的求值順序。 ```py 9 / 3 * 3 ``` 此表達式的結果是 9 還是 1? 乘法,刪除和模運算符從左到右關聯。 因此,該表達式的計算方式為:`(9 / 3) * 3`,結果為 9。 算術,布爾,關系和按位運算符都是從左到右關聯的。 另一方面,賦值運算符是正確關聯的。 ```py >>> a = b = c = d = 0 >>> a, b, c, d (0, 0, 0, 0) ``` 如果關聯從左到右,則以前的表達式將不可能。 復合賦值運算符從右到左關聯。 ```py >>> j = 0 >>> j *= 3 + 1 >>> j 0 ``` 您可能期望結果為 1。但是實際結果為 0。由于有關聯性。 首先求值右邊的表達式,然后應用復合賦值運算符。 在本章中,我們討論了 Python 中的運算符。
                  <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>

                              哎呀哎呀视频在线观看