In Python, the equal to (==) and not equal to (!=) operators are usually use for testing the equality of two objects. Actually, these are part of the many comparison operators also, that we get to use in Python. Thus, Operators like, equal to (==), not equal to (!=), greater than (>), less than (<) and many more. Thus, in this post, we will get to see the usage of Python does not equal operator with ample number of examples and use cases.

Python does not equal

Python supports a number of comparison operators as given below:

  • == the Equal to
  • !=  the Not equal to
  • >  the Greater than
  • >= the Greater than or equal to
  • <  the Less than
  • <=  the Less than or equal to

For example:

Not equal to

int x != int y

As values of both objects are not equal so condition became True.

Equal to:

a == 7

str = ‘Python Equal’

Note: The <> (not equal to or equivalent of !=) was also supportive in version 2 of Python. However, it has been deprecated in version 3 and produces “Invalid Syntax” Error.

Not Equal operator in Python

You can use “!=” and “is not” for not equal operation in Python.

Python != Operator

The python != ( not equal operator ) return True, if the values of the two Python operands given on each side of the operator is not equal, otherwise false .

Python is dynamically, but strongly type , and other statically typed languages would complain about comparing different types . So if the two variables have the same values but they are of different type, then not equal operator will return True.

Python — is not operator

The is operator is the object identity operator use to check if two objects in fact are the same and its negation is not : x is y is true if and only if x and y are the same object.

The above example will print “not equal” as x = 2 as assigned earlier.

Python Comparison Operators

comparison operator , also called python relational operator, compares the values on both sides of the operator to classify the relation between them as either true or false .

Python Not Equal

Python not equal comparison is done with !=, the not equal operator. The result of the operation is a Boolean.

>>> 7 != 9
True
>>> 42 != 42
False
>>> 'Shubham' != 'Manish'
False

The most common use of the not equal operator is to decide the flow of the application:

x, y = 33, 53
if x != y:
   print('x and y are not equal')
else:
   print('x and y are equal')

Comparing Objects with !=

Python not equal operator compares the value of objects, that’s in contrast to the Python is not operator that compares if they are actually different objects. For example:

>>> x = [2, 3]
>>> z = [2, 3]
>>> x != z
False
>>> x is not z
True

Comparing Lists in Python

You can use the not equal operator to compare lists:

>>> [5, 7] != [2, 3]
True
>>> [2, 6] != [2, 6]
False

Comparing Tuples in Python

You can use the not equal operator to compare tuples:

>>> (22, 43) != (22, 33)
True
>>> (22, 33) != (22, 33)
False

Comparing Sets in Python

You can use the not equal operator to compare sets:

>>> set([7, 3]) != set([2, 3])
True
>>> set([3, 5]) != set([3, 5])
False
>>> set([3, 2]) != set([2, 3])
False

As you can see the order of the initial list doesn’t make a difference in the comparison, because the Set’s order doesn’t matter.

Comparing Dictionaries in Python

You can use the equal operator to compare dictionaries:

>>> {2: 3, 5: 'b'} != {2: 3, 5: 'b'}
False
>>> {5: 'b', 2: 3} != {2: 3, 5: 'b'}
False
>>> {3: 3, 5: 'b'} != {2: 3, 5: 'b'}
True

As you can see the order doesn’t make a difference in the comparison, because the Dictionary’s order doesn’t matter.

WRAPPING UP — Python Does Not Equal

We have thus with the help of ample number of examples seen here the explanation of the not equal to operator (!= or <>). We have also seen the various use cases of the same. Examples which were use to bring out its usage with dictionaries, tuples, sets, lists, strings and other variables also. We also saw what are the various comparison operators. I have tried my best to make this topic clear to you. But, in case, you still have some doubts lingering. Then, please do write to me in the comments section and I am as always, ever-ready to help you. And, also solve your queries and problems.

Categorized in: