---Advertisement---

Python Not Equal Operator (!=) Explained with Examples and Usage Simple And Best Guide 2025

By Bhavani

Updated On:

---Advertisement---
Python Not Equal Operator

👉Python Not Equal Operator (!=)

Python Not Equal Operator : The Python Not Equal Operator (!=) is used to compare two values and returns True if the values are different; otherwise, it returns False. Python’s flexibility and dynamic nature make it a powerful language for handling various data types and conditions using this operator.

👉Types of Not Equal Operators in Python

Python Not Equal Operator

Python supports two types of Not Equal operators:

  1. != — Used in Python 2 and Python 3.
  2. <> — Used only in Python 2 (deprecated in Python 3).

👉Syntax

  • X != Y — Recommended syntax for Python 3.
  • X <> Y — Outdated syntax for Python 2.

👉Examples of Python Not Equal Operator

python

A = 44

B = 284

C = 284

print(B != A)  # Output: True

print(B != C)  # Output: False

python

C = 12222

X = 12222.0

Y = “12222”

print(C != X)  # Output: False

print(X != Y)  # Output: True

print(C != Y)  # Output: True


👉Using Not Equal Operator with if Statement

The if statement can check conditions using the != operator.

👉Example:

python

X = 5

Y = 5

if X != Y:

    print(“X is not equal to Y”)

else:

    print(“X is equal to Y”)

Output: X is equal to Y


👉Using == Operator with while Loop

The == operator can be combined with a while loop to iterate as long as a condition is true.

👉Example:

python

m = 300

while m <= 305:

    m += 1

    if m % 2 == 0:

        continue

    print(m)

👉Output:

301

303

305


👉Using Not Equal Operator with while Loop

The != operator can also be combined with a while loop to filter certain conditions.

👉Example:

python

m = 300

while m <= 305:

    m += 1

    if m % 2 != 0:

        continue

    print(m)

👉Output:

302

304

306


👉Using Not Equal Operator with Custom Objects

Python allows developers to define custom conditions using the __ne__ method.

👉Example:

python

class G9Example:

    s_n = ”

    def __init__(self, name):

        self.s_n = name

    def __ne__(self, x):

        if type(x) != type(self):

            return True

        if self.s_n != x.s_n:

            return True

        else:

            return False

G1 = G9Example(“Software Moji Moji”)

G2 = G9Example(“HipHop Moji”)

G3 = G9Example(“Software Moji Moji”)

print(G1 != G2)  # Output: True

print(G2 != G3)  # Output: True

print(G1 != G3)  # Output: False


👉Comparison Operators in Python

OperatorDescriptionExample
!=Not equal toA != B
==Equal toA == B
>=Greater than or equal toA >= B
<=Less than or equal toA <= B
>Greater thanA > B
<Less thanA < B

👉Useful Tips for Using Not Equal Operator

✅ Use != for better compatibility in Python 3.
✅ Avoid <> as it is deprecated in Python 3.
✅ In formatted strings, ensure you use != and not ≠, which may appear in certain fonts or editors.
✅ The != operator is powerful for filtering data conditions, especially in dynamic data types.

 Python Operators Explained

Download Python

Leave a Comment

Index