# How to use equal and not equal operators in Python

Learn about One of the most important Python operators: Python `==` and Python `!=`.

Basics of Programming with Math Ope...

Python leverages operators to do various tasks. Four basic operators that are heavily used are arithmetic operators, comparison operators, assignment operators and logical operators.

In this article I’ll share some of my experience with comparison operators.

We have seven comparison operators but we will only talk about equal (`==`) and not equal (`!=`) operators.

These comparison operators can be used with integers, strings, lists, dictionaries, and anything you can think of.

These operators can compare the integer value with float value; for example `1 == 1.0` , this statement returns True because technically these two values are same.

On the other hand, if we compare two lists in a different order, it returns False; example `[1,2,3] == [3,2,1]`, this statement returns False. This is because, although the number of items in a list is the same, but the order is different and that makes the second list altogether different list than the first list.

But if you do the same comparison with two dictionaries it will return True, For example `{'a':1,'b':2} == {'b':2, 'a':1}` returns True. This is because dictionaries are not sorted in Python.

Even though you created a dictionary in sorted order, every time you print the dictionary, you will see different results.

## python does not equal (!=) Operator

Python uses `!=` operator to compare the inequality of two variables. It checks left-side value is not equal to the right-side value. `<>` works same as `!=` but is discontinued in Python 3.

The comparison returns either True or False. See the example code below to understand `!=` operator.

```# Same number compared using not equal operator
>>> 1 != 1
False

# Different numbers compared
>>> 1 != 0
True

# In Python 1 is considered True and 0 is False, so the statement is False
>>> 1 != True
False

# In Python 1 is considered True and 0 is False, so the statement is True
>>> 0 != True
True

# In Python 1 is considered True and 0 is False, so the statement is True
>>> 1 != False
True

# In Python 1 is considered True and 0 is False, so the Statement is False
>>> 0 != False
False

# Integer is compared to String
>>> 1 != "1"
True

# Integer is compared to float equivalent
>>> 1 != 1.0
False

# comparing same dictionary with different element order using equal operator
>>> {'a':1,'b':2} !={'b':2, 'a':1}
False

# Comparing same list using not equal operator
>>> [1,2,3] != [1,2,3]
False

# Comparing list with same elements in different order using not equal operator
>>> [1,2,3] != [3,2,1]
True```

## Python equal (==) Operator

Python uses `==` operator to compare the equality of two variables. `==` operator produces just opposite results to `!=` operator. If left side is equal to the right side, the statement returns True else False.

See the example code below to understand `==` operator.

```# Same number compared
>>> 1 == 1
True

# Different numbers compared
>>> 1 == 0
False

# In Python 1 is considered True and 0 is False, so the statement is True
>>> 1 == True
True

# In Python 1 is considered True and 0 is False, so the statement is False
>>> 0 == True
False

# In Python 1 is considered True and 0 is False, so the statement is False
>>> 1 == False
False

# In Python 1 is considered True and 0 is False, so the statement is opposite of False
>>> 0 == False
True

# Integer is compared to String
>>> 1 == "1"
False

# Integer is compared to float equivalent
>>> 1 == 1.0
True

# comparing same dictionary with different element order
>>> {'a':1,'b':2} =={'b':2, 'a':1}
True

# Comparing same list using equal operator
>>> [1,2,3] == [1,2,3]
True

# Comparing list with same elements in different order using equal operator
>>> [1,2,3] == [3,2,1]
False```

## is vs == in Python

In Python `==` operator compares only the value but doesn’t check for the object. It means, if you create two different objects residing in different memory with same values, `==` operator returns true but `is` operator returns false.

Let’s compare `is` and `==` operator with an example code:

```_list = [1,2,3]
new_list = [1,2,3]

_list == new_list
## returns True

_list is new_list
## returns False
```

The first statement returns True because `==` compares only the values which are [1,2,3]. Both lists have the same values and the statement returned True.

The second statement returns False because, although the values in these two lists are same but the location where these two lists are saved is different.

List is mutable object in Python, which means, list can be changed once created. Hence when you create two lists with the same elements, they point to two different locations and gets created in two different memory.

Let’s see with more examples, how `is` and `==` operators are different.

```# Integer is immutable object and will point to same address
>>> a = 1
>>> b = 1
>>> a == b
True
>>> a is b
True

>>> a = 1
>>> b = a
>>> a == b
True
>>> a is b
True

# String is immutable object and will point to same address
>>> a = "some string"
>>> b = "some string"
>>> a == b
True
>>> a is b
True

>>> a = "some string"
>>> b = a
>>> a == b
True
>>> a is b
True

# Same for Boolean
>>> a = True
>>> b = True
>>> a == b
True
>>> a is b
True

# List shows different results as we talked earlier why.
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a == b
True
>>> a is b
False

>>> a = [1,2,3]
>>> b = a
>>> a == b
True
>>> a is b
True

# Same results for set because set is also mutable in Python
>>> a = (1,2,3)
>>> b = (1,2,3)
>>> a == b
True
>>> a is b
False

>>> a = (1,2,3)
>>> b = a
>>> a == b
True
>>> a is b
True

# dict is mutable and hence it will produce same results as list and set
>>> a = {'a':1,'b':2}
>>> b = {'a':1,'b':2}
>>> a == b
True
>>> a is b
False

>>> a = {'a':1,'b':2)
>>> b = a
>>> a == b
True
>>> a is b
True ```

Now that you know the difference between `==` and `is` operator, you can apply the same rules on `!=` and `is not`.