> ## 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 Right Shift Assignment

The `>>=` operator is the augmented assignment operator for bitwise right shift in Python. It shifts the binary representation of the left operand to the right by the number of bits specified by the right operand, and binds the resulting value back to the left operand's reference.

## Syntax

```python theme={"dark"}
x >>= y
```

This operation evaluates the right shift and assigns the result back to `x`. It is generally functionally equivalent to:

```python theme={"dark"}
x = x >> y
```

## Object Model and Dunder Methods

When the Python interpreter encounters `x >>= y`, it attempts to perform an in-place modification by invoking the `__irshift__` (in-place right shift) magic method on the left operand:

```python theme={"dark"}
x = x.__irshift__(y)
```

* **Return Value Binding**: Python's augmented assignment always binds the *return value* of the operation back to the left operand (`x`).
* **Mutable Types**: If the object implements `__irshift__`, the method typically modifies the object's internal state and returns `self` (the mutated object), though returning a newly allocated object is syntactically permitted.
* **Fallback Mechanism**: If the left operand does not implement `__irshift__` (such as Python's built-in, immutable `int` type), or if its `__irshift__` method returns `NotImplemented`, Python falls back to standard right shift evaluation:

  1. Python evaluates `x.__rshift__(y)`.
  2. If `__rshift__` is missing or returns `NotImplemented`, Python attempts to invoke the right operand's reflected method: `y.__rrshift__(x)`.
  3. If `__rrshift__` is also missing or returns `NotImplemented`, Python raises a `TypeError`.

  If either step 1 or 2 succeeds, Python creates a new object with the result and binds the reference `x` to this new object.

## Operand Constraints

The constraints applied to the operands of `>>=` are dictated by the underlying types and their implemented magic methods, rather than being universal rules of the operator itself:

* **Built-in Integers**: When operating on Python's standard `int` type, the right operand (`y`) must be an integer or an object implementing the `__index__` magic method. Passing a type without `__index__` (e.g., `x >>= 2.0`) raises a `TypeError`. Furthermore, the right operand must be non-negative; attempting a negative shift (e.g., `x >>= -1`) raises a `ValueError: negative shift count`.
* **Custom and Third-Party Types**: Custom classes or external library objects (like NumPy arrays or pandas Series) that implement `__irshift__`, `__rshift__`, or `__rrshift__` are not bound by the built-in integer constraints. They can accept entirely different types (such as other arrays) as the right operand and can define custom behaviors for negative values.

## Built-in Integer Mechanics

When applied to Python's built-in `int` type, the operator behaves as follows:

1. **Bit Manipulation**: The operator evaluates the binary representation of the left operand (`x`) and shifts its bits to the right by `y` positions. The rightmost `y` bits are discarded.
2. **Sign Extension**: Because Python integers have arbitrary precision, they do not have a fixed bit-width. Python simulates an infinite two's complement representation.
   * For **positive integers**, vacated bits on the left are filled with `0`s.
   * For **negative integers**, vacated bits on the left are filled with `1`s to preserve the sign bit.
3. **Mathematical Equivalence**: Provided `y >= 0`, a bitwise right shift by `y` positions is mathematically identical to floor division by $2^y$. Under this condition, `x >>= y` yields the exact same result as `x = x // (2**y)`.

## Execution Examples

**Positive Integer Shift:**

```python theme={"dark"}
x = 20        # Binary: 10100
x >>= 2       # Shift right by 2 bits. Discard the '00' on the right.
print(x)      # Output: 5 (Binary: 101)
```

**Negative Integer Shift:**

```python theme={"dark"}
y = -37       # Binary: ...11011011 (Infinite two's complement)
y >>= 3       # Shift right by 3 bits. Sign bit (1) is extended.
print(y)      # Output: -5 (Binary: ...11111011)
```

**Type Fallback Demonstration:**

```python theme={"dark"}
a = 10
id_before = id(a)
a >>= 1
id_after = id(a)


# Because 'int' is immutable and lacks __irshift__, 

# Python falls back to __rshift__ and creates a new object in memory.
print(id_before == id_after)  # Output: False
```

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