> ## 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 Is Not

The `is not` operator is a negated identity operator in Python that evaluates whether two operands reference different objects in memory. It returns `True` if the variables point to distinct memory addresses, regardless of whether their underlying values are equivalent.

```python theme={"dark"}
operand_a is not operand_b
```

Under the hood, the `is not` operator compares the unique integer identities of the objects, which are exposed via Python's built-in `id()` function. The expression `a is not b` is functionally equivalent to evaluating `id(a) != id(b)`.

## Identity vs. Equality

It is critical to distinguish between the `is not` operator and the `!=` (not equal) operator:

* **`is not`** evaluates **object identity**. It strictly checks memory allocation.
* **`!=`** evaluates **object value**. It delegates to the object's `__ne__()` dunder method to determine if the contents of the objects differ.

Two variables can have identical values but exist as separate objects in memory. In such scenarios, `!=` evaluates to `False`, while `is not` evaluates to `True`.

```python theme={"dark"}

# Two distinct list objects with identical values
list_a = [1, 2, 3]
list_b = [1, 2, 3]


# Value comparison
print(list_a != list_b)      # False: The contents are the same


# Identity comparison
print(list_a is not list_b)  # True: They occupy different memory addresses


# Reference assignment
list_c = list_a
print(list_a is not list_c)  # False: Both reference the exact same object
```

## CPython Interning and Constant Folding

The behavior of `is not` is heavily influenced by CPython's memory optimization techniques, specifically object interning and constant folding. CPython globally caches and reuses specific immutable objects, such as small integers (typically `-5` to `256`) and certain string literals.

When two variables are assigned the same interned value, the interpreter points them to the same cached object. In these specific cases, `is not` will evaluate to `False` even if the variables were declared independently.

For immutable literals outside the intern range, CPython's AST optimizer performs constant folding. If identical literals are defined within the same code block (such as a standard Python script), the compiler co-allocates them to the same memory address. To reliably demonstrate distinct memory allocation for identical values outside the intern range, the values must be computed at runtime.

```python theme={"dark"}

# Small integers are globally interned
x = 256
y = 256
print(x is not y)  # False: CPython reuses the memory address for 256


# Constant folding in the same code block reuses memory
a = 257
b = 257
print(a is not b)  # False: The compiler optimizes identical literals in the same block


# Runtime computation forces separate allocation outside the intern range
c = 257
d = int('257')
print(c is not d)  # True: Distinct memory addresses are allocated at runtime
```

<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>
