# Python Not Equal Operator

** Python Not Equal Operator **: In programming,

**operators**are essential to compare the values of the variables with each other. This is one of the most fundamental concepts. In this article, we will focus on the python operator “not equal”.

### Python Not Equal Operator Syntax

In Python, **not equal **is a comparison operator that compares whether two variables are not equal.

This operator returns the value **True **if the two variables compared are of the same type and have different values, if the values of the two variables are identical, it returns **False**.

**NOTE **: Python is a dynamic and highly typed language, i.e. for example the integer 5 is not equal to the string “5”.

There are two operators that allow you to do this operator in python :

Operator | Description |

!= | Available in python 2 and 3 |

<> | Available in python 3, but deprecated in python 3.x |

I rather recommend using the “!=” operator but it is important to know that the other operator exists in case you find this syntax in a code.

## Using Python *“!=”* not equal operator

In the following example, we will test different cases to illustrate how the not equal operator works:

```
a = 10
b = 10
c = 20
d = "10"
#Case 1
if a != b:
print("a is not equal to b")
else:
print("a is equal to b")
#Case 2
if a != c:
print("a is not equal to c")
else:
print("a is equal to c")
#Case 3
if a != d:
print("a is not equal to d")
else:
print("a is equal to d")
```

*Output : *

a is equal to b

a is not equal to c

a is not equal to d

In our first case, variable A has a value of 10 and variable B also has a value of 10. Since the two values are identical, the not equal operator returns ** False**.

In our second case, variable A has the value 10 and variable C has the value 20. Since the two values are different, the not equal operator returns * True*.

The third one is slightly different. Here we try to compare the values of two different types. The not equal operator considers that their values are not identical, so it returns ** True**. To overcome this kind of problem, it is possible to caste your variable to Integer so that you can perform the comparison.

## Using Python *“<>”* not equal operator

This operator works on the same principle as the operator just explained. Here is an example:

```
a = 10
b = 10
c = 20
d = "10"
if a <> b:
print("a is not equal to b")
else:
print("a is equal to b")
if a <> c:
print("a is not equal to c")
else:
print("a is equal to c")
if a <> d:
print("a is not equal to d")
else:
print("a is equal to d")
```

**Output**:

a is equal to b

a is not equal to c

a is not equal to d

This only works on python 2.x versions, on python 3.x it raises a SyntaxError exception.

## Not Equal – Tuples and Lists

It is quite possible to use this operator on iterators or custom objects. This is one of the powers of the operators available under python.

```
list1 = [10, 20, 30]
list2 = [10, 20, 30]
list3 = [20, 40, 60]
tuple1 = (10, 20, 30)
tuple2 = (10, 20, 30)
tuple3 = (20, 40, 60)
#list comparaison
if list1 != list2:
print("list1 is not equal to list2")
else:
print("list1 is equal to list2")
if list1 != list3:
print("list1 is not equal to list3")
else:
print("list1 is equal to list3")
#Tuples comparaison
if tuple1 != tuple2:
print("tuple1 is not equal to tuple2")
else:
print("tuple1 is equal to tuple2")
if tuple1 != tuple3:
print("tuple1 is not equal to tuple3")
else:
print("tuple1 is equal to tuple3")
```

**Output**:

list1 is equal to list2

list1 is not equal to list3

tuple1 is equal to tuple2

tuple1 is not equal to tuple3

## Summary

- In python 3.x, use the
**“!=”**not equal operator,**“<>”**has been deprecated from this verse - You can any type of objects (
)*Tuples, list, dictionnary, string, custom objects…* - To return an opposite boolean value, use the
**equal operator ==**.

Comments

Leave a comment