!=
Not equal by value — everything == is, inverted.
Common call
status != "done"
Returns
bool
Replaces
mixed unrelated types → True (they are unequal)
Watch out
None checks: prefer `is not None` over != None
aa — Left operand.type: Any · required != bb — Right operand.type: Any · required
→ bool
Demo
Live evaluation
Try:
Inputs
afloatleft
bfloatright
Output
5 != 6
True
The exact inverse of ==, including the cross-type numeric rules: 1 != 1.0 is False because they compare equal.
Operands
| Name | Type | Required | Description |
|---|---|---|---|
| a | Any | yes | Left operand. |
| b | Any | yes | Right operand. |
Return value
bool — True when the operands are NOT equal by value. Unrelated types are unequal, so this is True for them.
Common patterns
Guard clauses
Early exits on unexpected values.
if response.status != 200: raise ApiError(response)
Change detection
Compare before/after snapshots.
if new_config != old_config: reload()
Examples
1. Different values
5 != 6
Returns
True2. Equal values
5 != 5
Returns
False3. Unrelated types
1 != "1"
Returns
TruePitfalls
1. != None instead of is not None
Works usually — but a class can override __ne__ and lie.
Fragile idiom
if x != None:
depends on __ne__
Fix
if x is not None:
identity — cannot be overridden
2. Float inequality inherits the float trap
Computed floats are almost always "not equal".
True, surprisingly
0.1 + 0.2 != 0.3
True
Fix
not math.isclose(0.1 + 0.2, 0.3)
False
When to use
Use it
- Guard clauses and validation
- Change detection between values
Reach for something else
- None checks → is not None
- Computed floats → math.isclose
Notes
Complexity
O(1) scalars; O(n) containers
Return
bool
CPython impl
Objects/object.c :: PyObject_RichCompare (Py_NE)
Memory
No allocation
Thread-safe
Yes for immutable operands
FAQ
No — Python 3 derives != from __eq__ automatically. Only override __ne__ for exotic semantics.
History
3.0
The legacy <> spelling removed; __ne__ auto-derived from __eq__.