Andromeda
Note

Boolean Logic (Python)

Definition

A system of logic based on two values, True and False, used to determine the execution path of a program.

Why It Matters

Boolean logic is the bedrock of all digital decision-making; mastering its operators is the only way to build predictable, deterministic programs that can handle the complexity of real-world branching conditions.

Core Concepts

  • Values: True and False (Must be capitalized in Python).
  • Boolean Operators:
    • and: Evaluates to True if both Boolean values are true.
    • or: Evaluates to True if either Boolean value is true.
    • not: Evaluates to the opposite Boolean value (unary operator).
  • Truthy/Falsey Values: In a Boolean context, certain non-Boolean values are treated as False: 0, 0.0, '' (empty string), and None. Almost everything else is True.
  • Precedence: not is evaluated first, then and, then or.
is_weekend = True
is_sunny = False

# Evaluates to True because is_weekend is True and is_sunny is False (not False is True)
if is_weekend and not is_sunny:
    print("Stay inside and code.")

Connected Concepts