Use != to test value inequality (a != b, returning True when a and b have different values), and use is not to test object identity inequality (a is not b, such as x is not None)—noting that Python 2’s legacy <> operator was completely removed in Python 3 and raises a SyntaxError.

Basic Example

# 1. Value inequality (!=): Compares whether two objects have different values
print(5 != 10)          # Output: True
print("abc" != "ABC")   # Output: True (case-sensitive)
print([1, 2] != [1, 2]) # Output: False (equal list contents)

# 2. Identity inequality (is not): Checks whether two variables point to different objects in memory
a = [1, 2]
b = [1, 2]
print(a != b)           # Output: False (values are equal)
print(a is not b)       # Output: True  (two distinct list instances in memory)

# 3. Standard idiom for checking against None
result = "ok"
if result is not None:
    print("Result is present:", result)

Key Details & Edge Cases

  • <> is a SyntaxError in Python 3: While Python 2 supported a <> b as an alias for a != b (unless from __future__ import barry_as_FLUFL was active), Python 3 removed <> permanently. Always use !=.
  • != vs. is not (Value vs. Identity):
    • != invokes a.__ne__(b) to compare values (e.g., 1000 != 1000 is False, 1 != 1.0 is False).
    • is not compares memory addresses / object identities (id(a) != id(b)). Never use is not to compare strings, integers, floats, or lists (x is not 1000 can evaluate to True even when x == 1000 because Python does not intern large integers). Use is not exclusively for singletons like None, True, False, or sentinel objects (if val is not None:).
  • Floating-point precision & NaN behavior with !=:
    • Due to IEEE 754 binary floating-point representation, 0.1 + 0.2 != 0.3 evaluates to True (0.30000000000000004 != 0.3). Compare floats using not math.isclose(a, b) instead of a != b.
    • Under IEEE 754 rules, NaN is never equal to anything—including itself—so float("nan") != float("nan") evaluates to True. Check for NaN with math.isnan(x) or pd.isna(x).
  • Chained inequality gotcha (a != b != c): Python chains comparison operators as (a != b) and (b != c), which does not check whether all three values are pairwise distinct (1 != 2 != 1 evaluates to True even though a == c). To verify all items are unique, compare lengths with a set: len({a, b, c}) == 3.
  • Custom classes (__eq__ and __ne__): In Python 3, defining __eq__(self, other) on a custom class automatically delegates != (__ne__) to not (self == other) unless you explicitly override __ne__.

Comparison of Inequality & Negation Operators in Python

Operator / Pattern Syntax What It Checks Best Used When
!= (Not Equal) a != b Value inequality (a.__ne__(b)) Comparing numbers, strings, collections, or custom objects by value.
is not (Identity Not Equal) x is not None Object identity inequality (id(x) != id(y)) Checking against None or custom sentinel objects (PEP 8 standard).
not in (Negated Membership) item not in seq Absence of an element in a collection/string Checking whether a key, element, or substring is missing.
not math.isclose() not math.isclose(a, b) Approximate floating-point inequality Comparing float results where binary rounding error would break !=.
<> (Removed) a <> b Raises SyntaxError in Python 3 Never—removed in Python 3; always replace with !=.