Example 1: NOT EQUAL Operator with Same Data Type
When comparing values of the same data type, the "!=" operator behaves as expected. Here's an example:
Python
a = 10
b = 20
c = 10
print(a != b)
print(a != c)

You can also try this code with Online Python Compiler
Run Code
Output
True
False
In this example, we compare the values of variables "a", "b", & "c". The first comparison "a != b" returns "True" because "a" (10) is not equal to "b" (20). The second comparison "a != c" returns "False" because "a" & "c" have the same value (10).
Example 2: NOT EQUAL Operator with Different Data Types
Python allows comparisons between different data types using the "!=" operator. Here's an example:
Python
x = 5
y = "5"
print(x != y)

You can also try this code with Online Python Compiler
Run Code
Output
True
In this case, "x" is an integer & "y" is a string. Even though they both represent the value "5", they are of different data types. When using the "!=" operator to compare them, it returns "True" because an integer is not equal to a string.
Compare Lists in Python using the Not Equal Operator:
The "not equal to" operator can also be used to compare lists in Python. When comparing lists, the operator checks if the lists have the same elements in the same order. Here's an example:
Python
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = [3, 2, 1]
print(list1 != list2)
print(list1 != list3)

You can also try this code with Online Python Compiler
Run Code
Output
False
True
In this example, we have three lists: "list1", "list2", & "list3". The first comparison "list1 != list2" returns "False" because both lists have the same elements in the same order. The second comparison "list1 != list3" returns "True" because the lists have different elements or the same elements in a different order.
It's important to note that when comparing lists using the "!=" operator, the order of elements matters. If the lists have the same elements but in a different order, the operator will consider them as not equal.
Use of if Statement with the Not Equal Operator in Python:
The "not equal to" operator is commonly used in conditional statements, such as the "if" statement, to make decisions based on comparisons. Here's an example that demonstrates the usage of the "!=" operator with an "if" statement:
Python
age = 18
if age != 21:
print("You are not 21 years old.")
else:
print("You are 21 years old.")

You can also try this code with Online Python Compiler
Run Code
Output
You are not 21 years old.
In this example, we have a variable "age" that represents a person's age. The "if" statement checks if the value of "age" is not equal to 21 using the "!=" operator. If the condition is true (i.e., the person's age is not 21), the code inside the "if" block is executed, printing "You are not 21 years old." If the condition is false (i.e., the person's age is 21), the code inside the "else" block is executed, printing "You are 21 years old."
Using the "!=" operator in conditional statements allows you to make decisions & execute different code blocks based on whether a condition is not met.
Python NOT EQUAL Operator with Custom Objects
In Python, you can also use the "!=" operator to compare custom objects. When comparing custom objects, the behavior of the "!=" operator depends on how the object's equality is defined.
By default, if you have not defined the "__eq__()" method for your custom class, the "!=" operator will compare the object identities rather than their values. Here's an example:
Python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("Rahul", 25)
person2 = Person("Rahul", 25)
person3 = person1
print(person1 != person2)
print(person1 != person3)

You can also try this code with Online Python Compiler
Run Code
Output
True
False
In this example, we define a custom class called "Person" with attributes "name" & "age". We create three instances of the "Person" class: "person1", "person2", & "person3". The first comparison "person1 != person2" returns "True" because "person1" & "person2" are different objects, even though they have the same attribute values. The second comparison "person1 != person3" returns "False" because "person1" & "person3" refer to the same object.
To customize the behavior of the "!=" operator for your custom objects, you need to define the "__eq__()" method in your class. The "__eq__()" method determines how equality is checked between objects of your class. Here's an example:
Python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __eq__(self, other):
if isinstance(other, Person):
return self.name == other.name & self.age == other.age
return False
person1 = Person("Rinki", 30)
person2 = Person("Rinki", 30)
person3 = Person("Harsh", 35)
print(person1 != person2)
print(person1 != person3)

You can also try this code with Online Python Compiler
Run Code
Output
False
True
In this modified example, we define the "__eq__()" method inside the "Person" class. The method checks if the other object is also an instance of the "Person" class & compares their "name" & "age" attributes. Now, when we compare "person1" & "person2" using the "!=" operator, it returns "False" because they have the same attribute values. Comparing "person1" & "person3" returns "True" because they have different attribute values.
Frequently Asked Questions
What is the difference between "!=" and "is not" operators in Python?
The "!=" operator compares the values of two objects, while the "is not" operator compares the identities of two objects. The "!=" operator checks if the values are not equal, whereas the "is not" operator checks if the objects are not the same instance.
Can I use the "!=" operator to compare strings in Python?
Yes, you can use the "!=" operator to compare strings in Python. It will return "True" if the strings are not equal and "False" if they are equal. For example: string1 = "hello" string2 = "world" print(string1 != string2) # Output: True
What happens if I use the "!=" operator to compare a number and a string in Python?
When you use the "!=" operator to compare a number and a string, it will always return "True" because they are of different data types. Python does not implicitly convert the string to a number or vice versa.
Conclusion
In this article, we explored the "not equal to" operator ("!=") in Python. We learned its syntax, how to use it with different data types, and how to customize its behavior with custom objects. The "!=" operator is a crucial comparison operator that allows you to check if two values are not equal, making it useful in conditional statements and loops.
Recommended Readings: