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:
TrueandFalse(Must be capitalized in Python). - Boolean Operators:
and: Evaluates toTrueif both Boolean values are true.or: Evaluates toTrueif 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), andNone. Almost everything else isTrue. - Precedence:
notis evaluated first, thenand, thenor.
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.")