Python Not Equal Operator: The Complete Guide

Python is a dynamic and strongly typed language, so if the two variables have the same values but are of a different type, then not equal operator will return True.

Python Not Equal Operator

Python not equal is a built-in operator that returns True if two variables are of the same type and have different values; if the values are identical, then it returns False. The not equal operator is a comparison operator in Python. For comparing object identities, you can use the keyword is, and its negation is not

Python has several basic operators that include some comparison operators, too. A comparison operator compares the values on both sides of the operator to classify the relation between them as either True or False.

print(1 == 1)
print(1 != 1)
print([] is [])

See the output.

➜  pyt python3 app.py
True
False
False
➜  pyt

See the following table.

Operator Description
!= Not Equal operator works in both Python 2 and Python 3.
<> Not equal operator in Python 2, deprecated in Python 3.

 

There’s the != (not equal) operator that returns True when two values differ, though be careful with the types because “1 != 1”.

This will always return True, and “1” == 1 will always return False since the types differ.

Python is dynamically but strongly typed, and other statically typed languages would complain about comparing different types.

We can use Python, not equal operator with f-strings if you use Python 3.6 or higher version.

# app.py

x = 11
y = 21
z = 19

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 = '11'
print(f'x is not equal to s = {x!=s}')

See the output.

➜  pyt python3 app.py
x is not equal to y = True
x is not equal to z = True
x is not equal to s = True
➜  pyt

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 a Data class with fields – id and record. When we are using the not equal operator, we want to compare it for record value. We can achieve this by implementing our __ne__() function.

See the following code.

# app.py

class App:
    id = 0
    netflix = ''

    def __init__(self, i, s):
        self.id = i
        self.netflix = s

    def __ne__(self, other):
        # return true if different types
        if type(other) != type(self):
            return True
        if self.netflix != other.netflix:
            return True
        else:
            return False


d1 = App(1, 'Stranger Things')
d2 = App(2, 'Money Heist')
d3 = App(3, 'Sacred Games')

print(d1 != d2)
print(d2 != d3)

See the output.

➜  pyt python3 app.py
True
True
➜  pyt

#Comparison operators in Python

Comparisons are used to compare values. It either returns True or False according to the condition.

 
Operator Meaning Example
> Greater than – True if the left operand is greater than the right x > y
< Less than – True if the left operand is less than the right x < y
== Equal to – True if both operands are equal x == y
!= Not equal to – True if operands are not equal x != y
>= Greater than or equal to – True if the left operand is greater than or equal to the right x >= y
<= Less than or equal to – True if the left operand is less than or equal to the right x <= y

 

Finally, Python Not Equal Operator Example is over.

Recommended Posts

Null Object In Python

Python operators

Python property()

Python pow()

Python open()

Python iter()

1 thought on “Python Not Equal Operator: The Complete Guide”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.