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 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 not equal operators
Operator | Description |
---|---|
!= | Not Equal operator, works in both Python 2 and Python 3. |
<> | Not equal operator in Python 2, deprecated in Python 3. |
Python 2 Example
Let’s see some examples of not-equal operator in Python 2.7.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ 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 >>> <img class="alignnone wp-image-22480 size-full" src="http://all-learning.com/wp-content/uploads/2019/02/Python-not-equal-operator-1.png" alt="Python not equal operator" width="1324" height="628" /> |
Python 3 Example
Here is some examples with Python 3 console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$ 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 >>> <strong><img class="alignnone wp-image-22481 size-full" src="http://all-learning.com/wp-content/uploads/2019/02/Python-not-equal-operator2.png" alt="Python not equal operator" width="1278" height="628" /> </strong> |
We can use Python not equal operator with f-strings too if you are using Python 3.6 or higher version.
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 3 4 5 |
x is not equal to y = False x is not equal to z = True x is not equal to s = True |
Python not equal with custom object
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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:
1 2 3 4 |
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:
1 2 3 4 |
True True |