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

The `bool` type in Python is a built-in data type representing one of two possible truth values: `True` or `False`. It is a direct subclass of the integer class (`int`), meaning boolean values behave as integers in numeric contexts, where `True` evaluates to `1` and `False` evaluates to `0`.

Because `True` and `False` are implemented as singletons in Python, they have fixed memory addresses throughout the lifecycle of a program.

```python theme={"dark"}

# Literal assignment
is_active = True
is_deleted = False


# Type inspection
type(True)             # <class 'bool'>
issubclass(bool, int)  # True
isinstance(True, int)  # True


# Numeric context behavior
True + True            # 2
False * 50             # 0
True == 1              # True
```

## Truth Value Testing

Any object in Python can be evaluated in a boolean context (such as an `if` or `while` condition) or converted explicitly using the `bool()` constructor.

Under the hood, the `bool()` constructor calls the object's `__bool__()` dunder method. If `__bool__()` is not defined, Python falls back to the `__len__()` method. If neither is defined, the object evaluates to `True` by default.

An object evaluates to `False` (often referred to as "falsy") if it is:

1. A constant defined to be false: `None` or `False`.
2. A zero of any numeric type: `0`, `0.0`, `0j`, `Decimal(0)`, `Fraction(0, 1)`.
3. An empty sequence or collection: `''` (empty string), `()`, `[]`, `{}`, `set()`, `range(0)`.

```python theme={"dark"}

# Explicit conversion using the bool() constructor
bool(0)          # False
bool("Python")   # True
bool([])         # False
bool([1, 2, 3])  # True


# Custom class boolean evaluation
class CustomObject:
    def __len__(self):
        return 0

bool(CustomObject())  # False
```

## Logical Operators

Python provides three logical operators to perform boolean arithmetic: `not`, `and`, and `or`.

Unlike the `not` operator, which strictly returns a `bool` object, the `and` and `or` operators utilize **short-circuit evaluation** and return the actual object evaluated, which may not be of type `bool`.

* **`not x`**: Returns `True` if `x` is falsy; returns `False` if `x` is truthy.
* **`x and y`**: Evaluates `x`. If `x` is falsy, it returns `x` (short-circuiting). Otherwise, it evaluates and returns `y`.
* **`x or y`**: Evaluates `x`. If `x` is truthy, it returns `x` (short-circuiting). Otherwise, it evaluates and returns `y`.

```python theme={"dark"}

# 'not' always returns a boolean
not "Hello"    # False
not ""         # True


# 'and' returns the first falsy value, or the last truthy value
"A" and "B"    # "B" (Evaluates both, returns last)
"" and "B"     # ""  (Short-circuits at first falsy value)


# 'or' returns the first truthy value, or the last falsy value
"A" or "B"     # "A" (Short-circuits at first truthy value)
"" or "B"      # "B" (Evaluates both, returns last)
```

## Bitwise Operators on Booleans

Because `bool` is a subclass of `int`, bitwise operators (`&`, `|`, `^`, `~`) can be applied to boolean values. The bitwise AND (`&`), OR (`|`), and XOR (`^`) operators return a `bool` type when both operands are booleans, evaluating both operands and bypassing the short-circuiting behavior of `and` and `or`.

However, the bitwise NOT operator (`~`) returns an `int` because it operates directly on the underlying integer representation of the boolean (`1` for `True`, `0` for `False`).

```python theme={"dark"}
True & False  # False (Bitwise AND, returns bool)
True | False  # True  (Bitwise OR, returns bool)
True ^ True   # False (Bitwise XOR, returns bool)
~True         # -2    (Bitwise NOT, returns int, equivalent to ~1)
~False        # -1    (Bitwise NOT, returns int, equivalent to ~0)
```

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