<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 運算符 > 原文: [https://www.programiz.com/python-programming/operators](https://www.programiz.com/python-programming/operators) #### 在本教程中,您將學習有關 Python 中不同類型的運算符,它們的語法以及如何在示例中使用它們的所有知識。 ## python 中的運算符是什么? 運算符是 Python 中執行算術或邏輯計算的特殊符號。 運算符所操作的值稱為操作數。 例如: ```py >>> 2+3 5 ``` 在此,`+`是執行加法的運算符。`2`和`3`是操作數,`5`是操作的輸出。 * * * ## 算術運算符 算術運算符用于執行數學運算,例如加法,減法,乘法等。 | 運算符 | 含義 | 示例 | | --- | --- | --- | | `+` | 相加兩個操作數或一元加 | `x + y + 2` | | `-` | 從左側減去右側操作數或一元減 | `x - y - 2` | | `*` | 將兩個操作數相乘 | `x * y` | | `/` | 將左操作數除以右操作數(結果總是為浮點) | `x / y` | | `%` | 模 - 左側除以右側操作數的余數 | `x % y`(`x / y`的余數) | | `//` | 整數除法 - 產生整數的除法,取整到數字的左側 | `x // y` | | `**` | 指數 - 左操作數的右操作數次冪 | `x ** y`(`x`的`y`次冪) | ### 示例 1:Python 中的算術運算符 ```py x = 15 y = 4 # Output: x + y = 19 print('x + y =',x+y) # Output: x - y = 11 print('x - y =',x-y) # Output: x * y = 60 print('x * y =',x*y) # Output: x / y = 3.75 print('x / y =',x/y) # Output: x // y = 3 print('x // y =',x//y) # Output: x ** y = 50625 print('x ** y =',x**y) ``` **輸出** ```py x + y = 19 x - y = 11 x * y = 60 x / y = 3.75 x // y = 3 x ** y = 50625 ``` * * * ## 比較運算符 比較運算符用于比較值。 根據條件返回`True`或`False`。 | 運算符 | 含義 | 示例 | | --- | --- | --- | | `>` | 大于 - 如果左操作數大于右操作數,則為`True` | `x > y` | | `<` | 小于 - 如果左操作數小于右操作數,則為`True` | `x < y` | | `==` | 等于 - 如果兩個操作數相等,則為`True` | `x == y` | | `!=` | 不等于 - 如果操作數不相等則為`True` | `x != y` | | `>=` | 大于或等于 - 如果左操作數大于或等于右操作數,則為`True` | `x >= y` | | `<=` | 小于或等于 - 如果左操作數小于或等于右操作數,則為`True` | `x <= y` | ### 示例 2:Python 中的比較運算符 ```py x = 10 y = 12 # Output: x > y is False print('x > y is',x>y) # Output: x < y is True print('x < y is',x<y) # Output: x == y is False print('x == y is',x==y) # Output: x != y is True print('x != y is',x!=y) # Output: x >= y is False print('x >= y is',x>=y) # Output: x <= y is True print('x <= y is',x<=y) ``` **輸出**: ```py x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True ``` * * * ## 邏輯運算符 邏輯運算符是`and`,`or`和`not`運算符。 | 運算符 | 含義 | 示例 | | --- | --- | --- | | `and` | 如果兩個操作數都為`True`,則為`True` | `x and y` | | `or` | 如果任何一個操作數為`True`,則為`True` | `x or y` | | `not` | 如果操作數為`false`,則為`True`(對操作數進行取反) | `not x` | ### 示例 3:Python 中的邏輯運算符 ```py x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x) ``` **輸出**: ```py x and y is False x or y is True not x is False ``` 這是這些運算符的[真值表](/python-programming/keyword-list#and_or_not)。 * * * ## 按位運算符 按位運算符作用于操作數,就好像它們是二進制數字的字符串一樣。 它們一點一點地運行,因此得名。 例如,2 是二進制形式的`10`,而 7 是`111`。 **在下表中**:令`x = 10`(二進制的`0000 1010`)和`y = 4`(二進制的`0000 0100`) | 運算符 | 含義 | 示例 | | --- | --- | --- | | `&` | 按位與 | `x & y = 0`(`0000 0000`) | | <code>&#124;</code> | 按位或 | <code>x &#124; y = 14</code>(`0000 1110`) | | `~` | 按位非 | `~x = -11`(`1111 0101`) | | `^` | 按位異或 | `x ^ y = 14`(`0000 1110`) | | `>>` | 按位右移 | `x >> 2 = 2`(`0000 0010`) | | `<<` | 按位左移 | `x << 2 = 40`(`0010 1000`) | * * * ## 賦值運算符 Python 中使用賦值運算符為變量賦值。 `a = 5`是一個簡單的賦值運算符,它將右邊的值 5 分配給左邊的變量。 Python 中有多種復合運算符,例如`a += 5`,它們會添加到變量中,然后再分配給它們。 等效于`a = a + 5`。 | 運算符 | 示例 | 相當于 | | --- | --- | --- | | `=` | `x = 5` | `x = 5` | | `+=` | `x += 5` | `x = x + 5` | | `-=` | `x -= 5` | `x = x - 5` | | `*=` | `x *= 5` | `x = x * 5` | | `/=` | `x /= 5` | `x = x / 5` | | `%=` | `x %= 5` | `x = x % 5` | | `//=` | `x //= 5` | `x = x // 5` | | `**=` | `x **= 5` | `x = x ** 5` | | `&=` | `x &= 5` | `x = x & 5` | | <code>&#124;=</code> |<code> x &#124; = 5</code> | <code>x = x &#124; 5</code> | | `^=` | `x ^= 5` | `x = x ^ 5` | | `>>=` | `x >>= 5` | `x = x >> 5` | | `<<=` | `x <<= 5` | `x = x << 5` | * * * ## 特殊運算符 Python 語言提供了一些特殊類型的運算符,例如身份運算符或成員資格運算符。 下面通過示例對其進行描述。 ### 身份運算符 `is`和`is not`是 Python 中的身份運算符。 它們用于檢查兩個值(或變量)是否位于內存的同一部分。 兩個相等的變量并不意味著它們是相同的。 | 運算符 | 含義 | 示例 | | --- | --- | --- | | `is` | 如果操作數相同,則為`True`(引用同一對象) | `x is True` | | `is not` | 如果操作數不相同,則為`True`(不引用同一對象) | `x is not True` | ### 示例 4:Python 中的身份運算符 ```py x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = [1,2,3] y3 = [1,2,3] # Output: False print(x1 is not y1) # Output: True print(x2 is y2) # Output: False print(x3 is y3) ``` **輸出**: ```py False True False ``` 在這里,我們看到`x1`和`y1`是相同值的整數,因此它們既相等又相同。`x2`和`y2`(字符串)也是如此。 但是列出了`x3`和`y3`。 它們是相等的但不相同。 這是因為盡管它們相等,但解釋器將它們分別定位在內存中。 * * * ### 成員運算符 `in`和`not in`是 Python 中的成員運算符。 它們用于測試在序列中是否找到值或變量([字符串](/python-programming/string),[列表](/python-programming/list),[元組](/python-programming/tuple),[集](/python-programming/set)和[字典](/python-programming/dictionary))。 在字典中,我們只能測試鍵的存在,而不是值。 | 運算符 | 含義 | 示例 | | --- | --- | --- | | `in` | 如果在序列中找到值/變量,則為`True` | `5 in x` | | `not in` | 如果在序列中未找到值/變量,則為`True` | `5 not in x` | ### 示例 5:Python 中的成員資格運算符 ```py x = 'Hello world' y = {1:'a',2:'b'} # Output: True print('H' in x) # Output: True print('hello' not in x) # Output: True print(1 in y) # Output: False print('a' in y) ``` **輸出**: ```py True True True False ``` 這里,`'H'`位于`x`中,但`'hello'`不存在于`x`中(請記住,Python 區分大小寫)。 同樣,`1`是鍵,`'a'`是字典`y`中的值。 因此,`'a' in y`返回`False`。
                  <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>

                              哎呀哎呀视频在线观看