> ## 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 Bitwise AND Assignment

The `&=` operator is the in-place bitwise AND assignment operator. It evaluates the bitwise AND operation between the left and right operands, and subsequently assigns the resulting value back to the left operand's reference.

## Syntax

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

Logically, this is evaluated as:

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

## Type-Specific Behavior

The behavior of the `&=` operator depends on the data types of the operands:

### 1. Integers (Bitwise AND)

When applied to integers, `&=` performs a bit-by-bit comparison of the binary representations of the operands. A bit in the resulting integer is set to `1` if and only if the corresponding bits in both operands are `1`. Otherwise, the bit is set to `0`.

```python theme={"dark"}
x = 12      # Binary: 1100
x &= 10     # Binary: 1010
print(x)    # Output: 8 (Binary: 1000)
```

### 2. Sets (Intersection Update)

When applied to sets, `&=` performs an in-place intersection. It mutates the left operand, retaining only the elements that are present in both the left and right sets.

```python theme={"dark"}
s = {1, 2, 3, 4}
s &= {3, 4, 5, 6}
print(s)    # Output: {3, 4}
```

## Underlying Data Model (Dunder Methods)

When the Python interpreter encounters `x &= y`, it attempts to invoke the augmented assignment magic method `__iand__` on the left operand:

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

* **Mutable Types (e.g., Sets):** If `__iand__` is implemented, the operation modifies the object in memory and returns `self`. The variable `x` remains bound to the exact same memory address.
* **Immutable Types (e.g., Integers):** Integers do not implement `__iand__` because they cannot be mutated. Python automatically falls back to the standard bitwise AND method `__and__`. It evaluates `x.__and__(y)`, creates a completely new integer object in memory with the result, and rebinds the variable `x` to this new object.

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