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 aSyntaxErrorin Python 3: While Python 2 supporteda <> bas an alias fora != b(unlessfrom __future__ import barry_as_FLUFLwas active), Python 3 removed<>permanently. Always use!=.!=vs.is not(Value vs. Identity):!=invokesa.__ne__(b)to compare values (e.g.,1000 != 1000isFalse,1 != 1.0isFalse).is notcompares memory addresses / object identities (id(a) != id(b)). Never useis notto compare strings, integers, floats, or lists (x is not 1000can evaluate toTrueeven whenx == 1000because Python does not intern large integers). Useis notexclusively for singletons likeNone,True,False, or sentinel objects (if val is not None:).
- Floating-point precision &
NaNbehavior with!=:- Due to IEEE 754 binary floating-point representation,
0.1 + 0.2 != 0.3evaluates toTrue(0.30000000000000004 != 0.3). Compare floats usingnot math.isclose(a, b)instead ofa != b. - Under IEEE 754 rules,
NaNis never equal to anything—including itself—sofloat("nan") != float("nan")evaluates toTrue. Check forNaNwithmath.isnan(x)orpd.isna(x).
- Due to IEEE 754 binary floating-point representation,
- 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 != 1evaluates toTrueeven thougha == 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__) tonot (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 !=. |
You must log in or register to comment.
