> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Inequality

The `!=` (not equal) operator is a binary comparison operator that evaluates the value inequality of two operands. While it typically evaluates to a boolean (`True` or `False`), it does not implicitly coerce its result to a boolean. Instead, it delegates directly to the underlying rich comparison methods of the operands and returns the exact object yielded by those methods.

```python theme={"dark"}
operand_a != operand_b
```

## Underlying Object Model

In Python's data model, the `!=` operator maps to the `__ne__(self, other)` rich comparison dunder (magic) method.

```python theme={"dark"}
class CustomObject:
    def __init__(self, value):
        self.value = value

    def __ne__(self, other):
        if isinstance(other, CustomObject):
            return self.value != other.value
        return NotImplemented
```

Directly invoking `a.__ne__(b)` is not functionally identical to using the `!=` operator. A direct method call bypasses Python's internal comparison routing and may return the `NotImplemented` singleton if the left operand does not support comparison with the right operand's type. The `!=` operator orchestrates a strict delegation and fallback mechanism to resolve the comparison.

## Return Value Flexibility

Because Python does not coerce the output of rich comparison operators, `!=` will return whatever object the underlying `__ne__` method produces. This architectural decision allows the operator to yield non-boolean types. If a class's `__ne__` method is programmed to return a complex object—such as a vectorized array of booleans or a database query expression—the `!=` operator will return that exact object unmodified.

## Evaluation Fallback Chain

When evaluating `a != b`, the Python interpreter follows a precise sequence to resolve the comparison:

1. **Subclass Priority:** If the right operand is an instance of a strict subclass of the left operand's type, Python prioritizes the right operand and calls `b.__ne__(a)` first. This ensures that subclasses can override the comparison behavior of their parent classes.
2. **Left Operand `__ne__`:** If the subclass rule does not apply, Python calls `a.__ne__(b)`.
3. **Right Operand `__ne__`:** If the first attempted method returns the `NotImplemented` singleton, Python delegates to the other operand. It calls `b.__ne__(a)` (or `a.__ne__(b)` if the subclass rule triggered step 1).
4. **Fallback to Identity:** If both `__ne__` methods return `NotImplemented`, Python abandons value-based comparison and falls back to memory identity, effectively returning the boolean result of `a is not b`.

**Delegation to `__eq__`:**
If a class does not explicitly override `__ne__`, it inherits the default `object.__ne__` implementation. This default method handles the relationship between equality and inequality by delegating to the object's `__eq__` method and logically inverting the result, provided `__eq__` itself does not return `NotImplemented`.

## Cross-Type Comparison Mechanics

Because Python is strongly typed, the `!=` operator does not perform implicit type coercion. Comparing objects of incompatible types generally evaluates to `True`. Their underlying `__ne__` methods return `NotImplemented`, causing the operator to fall back to an identity comparison (`is not`), which evaluates to `True` for distinct objects.

```python theme={"dark"}

# Evaluates to True. No implicit coercion from string to integer.
1 != "1" 
```

**Numeric Exception:** Python's built-in numeric types (`int`, `float`, `complex`, `decimal`, `fractions`) are explicitly designed to interoperate. The `!=` operator evaluates the mathematical equivalence of these types rather than requiring strict type matching.

```python theme={"dark"}

# Evaluates to False. The mathematical values are equivalent despite differing types.
1 != 1.0 
```

## `!=` vs `is not`

It is critical to distinguish `!=` from the `is not` operator:

* `!=` evaluates **value equivalence** (orchestrating `__ne__` and potentially `__eq__` via the `object` base class).
* `is not` evaluates **memory identity** (whether two references point to different objects in memory).

```python theme={"dark"}
a = [1, 2, 3]
b = [1, 2, 3]

a != b      # False (Values are equivalent)
a is not b  # True (Distinct objects in memory)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
