> ## 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 Literal Pattern

A literal pattern is a structural pattern matching construct in Python that evaluates whether a subject expression exactly matches a specified constant literal. Operating within a `match...case` block, it acts as the simplest form of pattern matching, routing execution flow based on direct value equivalence.

## Evaluation Mechanics

The underlying comparison mechanism depends on the type of literal provided in the `case` clause:

1. **Equality (`==`):** For numbers (integers, floats, complex), strings, and bytes, Python evaluates the pattern using the equality operator. The match succeeds if `subject == literal`.
2. **Identity (`is`):** For singletons (`None`, `True`, and `False`), Python strictly evaluates the pattern using the identity operator. The match succeeds only if `subject is literal`.

## Syntax

```python theme={"dark"}
match subject_expression:
    case "exact_string":
        # Executes if subject_expression == "exact_string"
    case 42:
        # Executes if subject_expression == 42
    case 3.14:
        # Executes if subject_expression == 3.14
    case True:
        # Executes if subject_expression is True
    case None:
        # Executes if subject_expression is None
```

## Supported Literal Types

Literal patterns strictly accept the following built-in constant types:

* **Strings:** `"text"`, `'text'`, `r"raw"`
* **Bytes:** `b"bytes"`
* **Integers:** `10`, `-5`, `0xFF`
* **Floats:** `3.14`, `1e10`
* **Complex Numbers:** `3+4j`
* **Booleans:** `True`, `False`
* **Null Object:** `None`

## Technical Constraints and Nuances

**1. F-Strings are Invalid**
Formatted string literals (f-strings) cannot be used as literal patterns. Because f-strings are evaluated dynamically at runtime, they violate the requirement that patterns must be static constants.

```python theme={"dark"}

# SyntaxError: patterns may not contain f-strings
case f"prefix_{value}": 
```

**2. Bare Names Create Capture Patterns**
A common developer pitfall is attempting to use a local variable as a literal pattern. A bare, unquoted identifier in a `case` statement is interpreted by the parser as a **Capture Pattern**, which unconditionally binds the subject to that variable name rather than comparing their values.

```python theme={"dark"}
MY_CONST = 100

match subject:
    case MY_CONST: 
        # This does NOT check if subject == 100.
        # It captures the subject's value into a new local variable named 'MY_CONST',
        # shadowing the outer variable.
```

**3. Dotted Names (Value Patterns)**
To match against a predefined constant without triggering a capture pattern, you must use a dotted name (an attribute lookup). In the Python grammar, this is technically classified as a **Value Pattern**, but it serves the exact same mechanical purpose as a literal pattern.

```python theme={"dark"}
import enum

class Status(enum.Enum):
    SUCCESS = 1

match subject:
    case Status.SUCCESS:
        # Evaluates using equality: subject == Status.SUCCESS
```

**4. Float Precision**
Because floating-point literal patterns rely on `==`, they are subject to standard IEEE 754 precision limitations. A subject resulting from a mathematical operation (e.g., `0.1 + 0.2`) will fail to match a literal pattern of `case 0.3:` due to floating-point representation errors.

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