Operators

!= — Value inequality

Tests inequality. The default __ne__ delegates to __eq__ and negates the result unless it is NotImplemented, but a custom class can define its own inequality independently. When both implementations decline, inequality falls back to nonidentity. NaN compares unequal even to itself. Rich comparisons share precedence and support chaining: the middle operand is evaluated once, and later operands are skipped when a preceding comparison is falsy. Custom __ne__ results need not be bool. Custom dispatch uses __ne__ and the counterpart __ne__ on the other operand; a right-hand subclass may have priority.

Formal Syntax & Structure

python syntaxpython
left != right

Parameters & Attributes

NameTypeDescription
leftobjectFirst operand; its type participates in operation selection.
rightobjectSecond operand; supported values depend on the operand types.

Return Value

bool or custom result

Boolean for ordinary built-in comparisons; custom rich comparison methods can return other values.

Working Example

Example Snippetpython
print(3 != 4)print([1,2] != [1,2])nan=float("nan")print(nan != nan)
Python Reference