您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“python不等于運算符怎么用”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“python不等于運算符怎么用”這篇文章吧。
Python not equal operator returns True if two variables are of same type and have different values, if the values are same then it returns False.
如果兩個變量具有相同的類型并且具有不同的值 ,則Python不等于運算符將返回True ;如果值相同,則它將返回False 。
Python is dynamic and strongly typed language, so if the two variables have the same values but they are of different type, then not equal operator will return True.
Python是動態的強類型語言,因此,如果兩個變量具有相同的值,但它們的類型不同,則不相等的運算符將返回True 。
Operator | Description |
---|---|
!= | Not Equal operator, works in both Python 2 and Python 3. |
<> | Not equal operator in Python 2, deprecated in Python 3. |
操作員 | 描述 |
---|---|
!= | 不是Equal運算符,可在Python 2和Python 3中使用。 |
<> | 在Python 2中不等于運算符,在Python 3中已棄用。 |
Let's see some examples of not-equal operator in Python 2.7.
我們來看一些Python 2.7中不等于運算符的示例。
$ python2.7 Python 2.7.10 (default, Aug 17 2018, 19:45:58) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 10 <> 20 True >>> 10 <> 10 False >>> 10 != 20 True >>> 10 != 10 False >>> '10' != 10 True >>>
Here is some examples with Python 3 console.
這是Python 3控制臺的一些示例。
$ python3.7 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 10 <> 20 File "<stdin>", line 1 10 <> 20 ^ SyntaxError: invalid syntax >>> 10 != 20 True >>> 10 != 10 False >>> '10' != 10 True >>>
We can use Python not equal operator withf-strings too if you are using Python 3.6 or higher version.
如果您使用的是Python 3.6或更高版本,我們也可以將Python不等于運算符與f字符串一起使用。
x = 10 y = 10 z = 20 print(f'x is not equal to y = {x!=y}') flag = x != z print(f'x is not equal to z = {flag}') # python is strongly typed language s = '10' print(f'x is not equal to s = {x!=s}')
Output:
輸出:
x is not equal to y = False
x is not equal to z = True
x is not equal to s = True
When we use not equal operator, it calls __ne__(self, other) function. So we can define our custom implementation for an object and alter the natural output.
當我們使用不等于運算符時,它將調用__ne__(self, other)函數。 因此,我們可以為對象定義自定義實現并更改自然輸出。
Let's say we have Data class with fields – id and record. When we are using the not-equal operator, we just want to compare it for record value. We can achieve this by implementing our own __ne__() function.
假設我們有帶字段的Data類-id和record。 當我們使用不等于運算符時,我們只想比較它的記錄值。 我們可以通過實現自己的__ne __()函數來實現這一點。
class Data: id = 0 record = '' def __init__(self, i, s): self.id = i self.record = s def __ne__(self, other): # return true if different types if type(other) != type(self): return True if self.record != other.record: return True else: return False d1 = Data(1, 'Java') d2 = Data(2, 'Java') d3 = Data(3, 'Python') print(d1 != d2) print(d2 != d3)
Output:
輸出:
False
True
Notice that d1 and d2 record values are same but “id” is different. If we remove __ne__() function, then the output will be like this:
請注意,d1和d2記錄值相同,但“ id”不同。 如果刪除__ne __()函數,則輸出將如下所示:
True
True
以上是“python不等于運算符怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。