---Advertisement---

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

By Bhavani

Published On:

---Advertisement---

πŸ‘‰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

---Advertisement---

Leave a Comment