- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between != and is not operator in Python
The != operator checks if the value of both the objects being compared have the same value or not. On the other hand “is not” operator checks if both the objects being compared are not pointing to the same reference. If the objects being compared are not pointing to the same reference then the “is not” operator returns true otherwise false. In this article, we will discuss how != and “is not” operators are used and what are the differences between them.
!= operator |
“Is not” operator |
---|---|
The != operator compares only the value of the objects being compared. |
The “is not” operator compares if the objects are pointing to the same memory location or not. |
It returns True if the value of both the objects are different and False otherwise. |
It returns true if the objects are not pointing to the same memory location otherwise it returns false. |
The syntax of != operator is object1 != object2 |
The syntax of “is not” operator is object1 is not object2 |
Example
In the below example, we compare two object values with different data types like integer, string, and list with the help of the!= operator and “is not” operator to see the difference between both the operators.
# python code to differentiate between != and “is not” operator. # comparing object with integer datatype a = 10 b = 10 print("comparison with != operator",a != b) print("comparison with is not operator ", a is not b) print(id(a), id(b)) # comparing objects with string data type c = "Python" d = "Python" print("comparison with != operator",c != d) print("comparison with is not operator", c is not d) print(id(c), id(d)) # comparing list e = [ 1, 2, 3, 4] f=[ 1, 2, 3, 4] print("comparison with != operator",e != f) print("comparison with is not operator", e is not f) print(id(e), id(f))
Output
comparison with != operator False comparison with is not operator False 139927053992464 139927053992464 comparison with != operator False comparison with is not operator False 139927052823408 139927052823408 comparison with != operator False comparison with is not operator True 139927054711552 139927052867136
Conclusion
In this article, we discussed the differences between the != operator and the “is not” operator and how the both of these comparison operators are used to compare two objects. The != operator compares only the value whereas the “is not” operator checks for the memory location of the object being compared. Both of these operators can be used in different scenarios while comparing two objects.