> ## 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 Type-Annotated Variable

A type-annotated variable in Python is a variable explicitly bound to a type hint using a colon (`:`) syntax, as defined in PEP 526. These annotations specify the expected data type of the variable for static type checkers, IDEs, and linters, without altering Python's inherent dynamic runtime typing behavior.

## Syntax

The syntax consists of the variable identifier, a colon, the type expression, and an optional assignment.

**Initialized Variable:**

```python theme={"dark"}
variable_name: expected_type = initial_value
```

**Uninitialized Variable:**
Variables can be annotated without immediate assignment. This declares the type for static analysis but does not bind the variable to a value in the local namespace.

```python theme={"dark"}
variable_name: expected_type
```

## Runtime Mechanics

Python does not enforce type annotations at runtime. The interpreter evaluates the type expression but does not prevent type-mismatched assignments.

```python theme={"dark"}

# Static checkers flag this, but Python executes it without raising an error.
count: int = "five" 
```

**The `__annotations__` Dictionary:**
When variables are annotated at the module level or class level, Python evaluates the type expression at runtime and stores the mapping in the `__annotations__` dunder attribute.

```python theme={"dark"}
class ServerConfig:
    host: str = "127.0.0.1"
    port: int

print(ServerConfig.__annotations__)

# Output: {'host': <class 'str'>, 'port': <class 'int'>}
```

*Note:* To optimize memory and performance, Python does **not** store annotations for local variables defined inside functions or methods. They are strictly consumed by static analysis tools.

## Type Expressions

Type annotations can range from built-in primitives to complex generic types.

**Primitives:**

```python theme={"dark"}
is_active: bool = True
threshold: float = 3.14
```

**Standard Collections (PEP 585):**
Modern Python allows standard collections to be parameterized directly using square brackets.

```python theme={"dark"}
user_ids: list[int] = [1, 2, 3]
environment_vars: dict[str, str] = {"ENV": "production"}
coordinates: tuple[float, float] = (45.0, -90.0)
```

**Union Types (PEP 604):**
The bitwise OR operator (`|`) is used to indicate that a variable can accept one of multiple types.

```python theme={"dark"}

# Can be an integer or a string
identifier: int | str = "ID-492"


# Equivalent to typing.Optional[int]
timeout: int | None = None 
```

**Advanced Typing Constructs:**
For more complex structural typing, the `typing` module provides specialized constructs.

```python theme={"dark"}
from typing import Any, Callable, Literal


# Accepts absolutely any type (disables strict type checking for this variable)
payload: Any = {"data": 123}


# A variable holding a function that takes an int and returns a str
formatter: Callable[[int], str] = lambda x: f"Value: {x}"


# Restricts the variable to specific literal values
status: Literal["pending", "running", "failed"] = "running"
```

## Type Aliases (PEP 613 / PEP 695)

Type expressions can be assigned to variables to create reusable type aliases. In Python 3.12+, the `type` keyword is used to explicitly declare an alias. For Python 3.10 and 3.11, `typing.TypeAlias` is used to explicitly distinguish aliases from standard variable assignments.

```python theme={"dark"}

# Python 3.12+ syntax
type Vector = list[float]
velocity: Vector = [9.8, 0.0, -1.2]


# Legacy syntax (Python 3.10+)
from typing import TypeAlias
Matrix: TypeAlias = list[list[int]]
grid: Matrix = [[1, 0], [0, 1]]
```

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