Open In App

Difference between != and is not operator in Python

Last Updated : 11 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see != (Not equal) operators. In Python != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Whereas is not operator checks whether id() of two objects is same or not. If same, it returns False and if not same, it returns True. And is not operator returns True if operands on either side are not equal to each other, and returns false if they are equal.

Let us understand the concepts one by one:

Example 1:

Python3




a = 10
b = 10
  
print(a is not b)
print(id(a), id(b))
  
c = "Python"
d = "Python"
print(c is not d)
print(id(c), id(d))
  
e = [1,2,3,4]
f = [1,2,3,4]
print(e is not f)
print(id(e), id(f))


Output:

False
140733278626480 140733278626480
False
2693154698864 2693154698864
True
2693232342792 2693232342600

Explanation:

  1. First with integer data the output was false because both the variables a, b are referring to same data 10.
  2. Second with string data the output was false because both the variables c, d are referring to same data “Python”.
  3. Third with list data the output was true because the variables e, f have different memory address.(Even though the both variable have the same data)

Example 2:

 != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. 

Python3




# Python3 code to 
# illustrate the 
# difference between
# != and is operator
  
a = 10
b = 10
print(a != b)
print(id(a), id(b))
  
c = "Python"
d = "Python"
print(c != d)
print(id(c), id(d))
  
e = [ 1, 2, 3, 4]
f=[ 1, 2, 3, 4]
print(e != f)
print(id(e), id(f))


Output:

False
140733278626480 140733278626480
False
2693154698864 2693154698864
False
2693232369224 2693232341064

Example 3:

The != operator compares the value or equality of two objects, whereas the Python is not operator checks whether two variables point to the same object in memory.

Python3




# Python3 code to 
# illustrate the 
# difference between
# != and is not operator
# [] is an empty list
list1 = []
list2 = []
list3 = list1
  
#First if
if (list1 != list2):
    print(" First if Condition True")
else:
    print("First else Condition False")
      
#Second if
if (list1 is not list2):
    print("Second if Condition True")
else:
    print("Second else Condition  False")
      
#Third if
if (list1 is not list3):
    print("Third if Condition True")
else
    print("Third else Condition False")
  
list3 = list3 + list2
  
#Fourth if
if (list1 is not list3):
    print("Fourth if Condition True")
else
    print("Fourth else Condition False")


Output:

First else Condition False
Second if Condition True
Third else Condition False
Fourth if Condition True

Explanation:

  1. The output of the first if the condition is “False” as both list1 and list2 are empty lists.
  2. Second if the condition shows “True” because two empty lists are at different memory locations. Hence list1 and list2 refer to different objects. We can check it with id() function in python which returns the “identity” of an object.
  3. The output of the third if the condition is “False” as both list1 and list3 are pointing to the same object.
  4. The output of the fourth if the condition is “True” because the concatenation of two lists always produces a new list.


Similar Reads

Benefits of Double Division Operator over Single Division Operator in Python
The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. C/C++ Code # A Python program to demonstrate use of # "//" for both integers and floating points print(5//2) print(-5//2) print(5.0//2) Output: 2 -3 2.0 The time complexity of the program is O(1) as it conta
2 min read
Difference between == and is operator in Python
When comparing objects in Python, the identity operator is frequently used in contexts where the equality operator == should be. In reality, it is almost never a good idea to use it when comparing data. What is == Operator?To compare objects based on their values, Python's equality operators (==) are employed. It calls the left object's __eq__() cl
3 min read
Difference between / vs. // operator in Python
In this article, we will see the difference between the / vs // operator in Python Python Division OperatorThe division operator '/ ' performs standard division, which can result in a floating-point number. However, if both the dividend and divisor are integers, Python will perform integer division only if the result is an integer. Otherwise, it wi
2 min read
Check if a value exists in a DataFrame using in & not in operator in Python-Pandas
In this article, Let’s discuss how to check if a given value exists in the dataframe or not.Method 1 : Use in operator to check if an element exists in dataframe. C/C++ Code # import pandas library import pandas as pd # dictionary with list object in values details = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'], 'Age
3 min read
Python IF with NOT Operator
We can use if with logical not operator in Python. The main use of the logical not operator is that it is used to inverse the value. With the help of not operator, we can convert true value to false and vice versa. When not applied to the value so it reverses it and then the final value is evaluated by the if condition. So according to its final va
4 min read
Python NOT EQUAL operator
In this article, we are going to see != (Not equal) operators. In Python, != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Python NOT EQUAL operators SyntaxThe Operator not equal in the Python description: != Not Equal operator, works in both Python
3 min read
not Operator in Python | Boolean Logic
Python not keyword is a logical operator that is usually used to figure out the negation or opposite boolean value of the operand. The Keyword 'not' is a unary type operator which means that it takes only one operand for the logical operation and returns the complementary of the boolean value of the operand. For example, if we give false as an oper
5 min read
Difference between concat() and + operator in Java
Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created. Concatenation is the process of joining end-
6 min read
Difference between Lodash _.clone() method and '=' operator to copy Objects
In this article, we will learn about the difference between using the _.clone() method in Lodash and using the '=' operator to copy objects. Both of these methods are used to create a copy of an object in JavaScript. However, both work in very different ways. Using _.clone() Method: The _.clone() method creates a new object and copies each value fr
3 min read
Difference between Relational operator(==) and std::string::compare() in C++
Relational operators vs std::string::compare() Return Value: Relational operators return boolean value, while compare() returns unsigned integer.Parameters : Relational operators need only two strings to perform comparison, one which is being compared and other one is for reference, while compare() can accept different arguments to perform certain
2 min read
Practice Tags :