Table of contents
1.
Introduction
2.
Python NOT EQUAL Operator Syntax
2.1.
Python
3.
Example 1: NOT EQUAL Operator with Same Data Type
3.1.
Python
4.
Example 2: NOT EQUAL Operator with Different Data Types
4.1.
Python
5.
Compare Lists in Python using the Not Equal Operator:
5.1.
Python
6.
Use of if Statement with the Not Equal Operator in Python:
6.1.
Python
7.
Python NOT EQUAL Operator with Custom Objects
7.1.
Python
7.2.
Python
8.
Frequently Asked Questions
8.1.
What is the difference between "!=" and "is not" operators in Python? 
8.2.
Can I use the "!=" operator to compare strings in Python? 
8.3.
What happens if I use the "!=" operator to compare a number and a string in Python? 
9.
Conclusion
Last Updated: Aug 21, 2025
Easy

Not Equal to in Python

Author Ravi Khorwal
0 upvote

Introduction

Python is a powerful programming language that offers various operators to compare values. One of these operators is the "not equal to" operator, represented by "!=". This operator checks if two values are not equal & returns a boolean result (True or False). 

not equal to in python

In this article, we will discuss the usage of the "not equal to" operator in Python in detail, like its syntax, examples with different data types, & applications. 

Python NOT EQUAL Operator Syntax

The "not equal to" operator in Python is denoted by the "!=" symbol. It is used to compare two values & determine if they are not equal. The syntax for using the "not equal to" operator is as follows:

value1 != value2


Here, "value1" & "value2" are the values or expressions that you want to compare. The operator returns "True" if the values are not equal & "False" if they are equal.

Let's look at a simple example to show the usage of the "!=" operator:

  • Python

Python

x = 5

y = 10

if x != y:

   print("x is not equal to y")

else:

   print("x is equal to y")
You can also try this code with Online Python Compiler
Run Code

 

Output

x is not equal to y


In this example, we compare the values of variables "x" & "y" using the "!=" operator. Since "x" is not equal to "y", the condition "x != y" evaluates to "True", & the code inside the "if" block is executed, printing "x is not equal to y".

Examples of NOT EQUAL Operator in Python:

Let's look at some more examples to better understand how the "not equal to" operator works in Python.

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

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

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

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

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

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

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:

 

Live masterclass