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

The Python `int` is a built-in numeric data type representing signed, arbitrary-precision mathematical integers. Unlike integers in languages like C or Java, Python integers are not bound by fixed hardware memory limits (e.g., 32-bit or 64-bit). Instead, they dynamically scale in size and are constrained only by the available system memory.

## Instantiation and Syntax

Integers can be instantiated via numeric literals in multiple bases or through the `int()` constructor.

```python theme={"dark"}

# Base-10 (Decimal)
dec_int = 42


# Base-2 (Binary)
bin_int = 0b101010


# Base-8 (Octal)
oct_int = 0o52


# Base-16 (Hexadecimal)
hex_int = 0x2A


# Constructor casting and base conversion
cast_int = int(42.9)          # Truncates to 42
str_int = int("42")           # Parses string to base-10
base_int = int("2A", base=16) # Parses string using specified base
```

## Internal Representation (CPython)

In the standard CPython implementation, an `int` is not a primitive scalar. It is a full C `struct` (specifically `PyLongObject`).

To achieve arbitrary precision, CPython represents the integer's absolute value as an array of "digits." On 64-bit systems, each digit in this array represents a base-$2^{30}$ value (stored in a 32-bit unsigned integer). As of Python 3.12, CPython utilizes a compact integer representation where the sign, the size (number of elements in the digit array), and memory allocation flags are stored in a dedicated tag field (`lv_tag`) within the integer struct, replacing the older approach of using the standard object header's signed size field (`ob_size`).

Because it is a full object, a Python `int` carries memory overhead. On a 64-bit system, a zero or single-digit integer typically consumes 24 to 28 bytes of memory, with the size growing by 4 bytes for every additional $2^{30}$ magnitude.

## Immutability and Interning

Python integers are strictly immutable. Any arithmetic or bitwise operation performed on an `int` allocates and returns a completely new `int` object rather than modifying the existing one in place.

To mitigate the performance overhead of object allocation for frequently used numbers, CPython implements **integer interning**. At startup, CPython pre-allocates an array of singleton integer objects for values ranging from `-5` to `256`. Any operation resulting in a value within this range returns a reference to the cached singleton rather than allocating a new object.

Because the CPython compiler optimizes code blocks by reusing identical integer literals (constant folding/co-allocation), demonstrating the boundaries of this interned range requires dynamically parsing the integers to bypass the compiler's static optimizations:

```python theme={"dark"}

# Within the interned range (-5 to 256)
a = 256
b = int("256")
print(a is b)  # True (Both reference the same pre-allocated singleton)


# Outside the interned range
x = 257
y = int("257")
print(x is y)  # False (Distinct objects are allocated in memory)
```

## Type Hierarchy

* `int` implements the `numbers.Integral` abstract base class.
* The Python `bool` type is a direct subclass of `int`. The boolean singletons `True` and `False` behave exactly as the integers `1` and `0` in all arithmetic contexts.

## Low-Level Methods

The `int` type exposes several methods for bitwise inspection and byte serialization, bypassing the need for external bit-manipulation libraries.

```python theme={"dark"}
n = 1024


# Returns the number of bits required to represent the integer in binary (excluding sign and leading zeros)
n.bit_length()  # 11


# Returns the number of ones in the binary representation of the absolute value (Python 3.10+)
n.bit_count()   # 1


# Serializes the integer to an array of bytes
byte_rep = (255).to_bytes(length=2, byteorder='big', signed=False) 

# Returns: b'\x00\xff'


# Deserializes an array of bytes back into an integer
int.from_bytes(b'\x00\xff', byteorder='big', signed=False) 

# Returns: 255
```

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