Concepts / Boolean Values and Comparison Operators

Boolean Values and Comparison Operators

Boolean logic describes whether a condition is True or False. Logical operators let you express several requirements or alternatives in one conditional expression.

  • Programming

Decisions Need Conditions

When code makes a decision, it often needs to check more than one condition. A login decision might require both a username and a password. A number might be accepted when it satisfies one possible condition or another. Python provides the logical operators and, or, and not for combining or modifying Boolean conditions.

Key point

Boolean logic describes whether a condition is True or False. Logical operators let you express several requirements or alternatives in one conditional expression.

Tracing and

The and operator represents simultaneous requirements. Its result is True only when the condition on the left and the condition on the right are both True. If either condition is False, the combined result is False.

True and TrueTrueTrue and FalseFalseFalse and TrueFalseFalse and FalseFalse
Which combinations of two Boolean conditions make and return True?
python
Output
can_log_in is True because both conditions are True.
Worked example

Checking Two Requirements

A user may log in only when a username has been entered and a password has been entered.

  1. 1
    Check the first condition

    has_username represents whether the username requirement is satisfied.

  2. 2
    Check the second condition

    has_password represents whether the password requirement is satisfied.

  3. 3
    Combine the conditions

    and requires both condition results to be True.

Result

The login condition is True only when both has_username and has_password are True.

Tracing or

The or operator represents alternatives. Its result is True when at least one of the conditions is True. It is useful when several different scenarios should be accepted.

True or TrueTrueTrue or FalseTrueFalse or TrueTrueFalse or FalseFalse
How does the result change when at least one of two conditions is True?
python
Output
accepted is True because at least one condition is True.
Key point

Use and when every listed requirement must be satisfied. Use or when any one of several acceptable possibilities is enough.

Reversing a Result

The not operator negates a Boolean expression. It changes True to False and False to True. When not applies to a comparison or another compound expression, parentheses make the intended grouping clearer.

Truebefore notFalseafter notFalsebefore notTrueafter notnotnot
What happens to a Boolean value when not is applied to it?
python
Output
is_open is True because not changes False to True.

From Comparison to Boolean

A comparison such as x > 5 produces a Boolean result: True when the comparison condition is satisfied and False when it is not. That result can then be used by and, or, or not.

xinput valuex > 5comparisonTruecondition satisfiedFalsecondition not satisfiedevaluatecondition holdscondition does not hold
How does a comparison such as x > 5 turn an input value into True or False?
python
Output
meets_score is True, and eligible is True.

Reading Combined Expressions

Logical operators have a precedence order: not is evaluated first, then and, then or. Parentheses can make the intended grouping explicit and keep a conditional expression clear.

username checkBoolean resultpassword checkBoolean resultusername and passwordboth requiredalternative checkorfinal decisionTrue or Falseandandcombine alternativesevaluate
How do multiple comparison results combine with and, or, and not before a final decision?
Worked example

Making Grouping Explicit

Express a decision that requires both a username and a password, while also showing clearly which conditions are grouped together.

  1. 1
    Identify the simultaneous requirements

    The username and password conditions must both be satisfied, so they belong together with and.

  2. 2
    Use parentheses

    Parentheses show the intended grouping directly, even though Python has a defined precedence order.

  3. 3
    Read the result

    The grouped expression is True only when both requirements are True.

Result

(has_username and has_password) expresses the two simultaneous requirements clearly.

Common Logic Mistakes

  • Common mistake

    Using and when either condition should be acceptable

    What it looks like

    A number should be accepted when it is divisible by 2 or divisible by 3, but the expression requires both.

    Why it fails

    and returns True only when both conditions are True, so it rejects cases that satisfy just one acceptable option.

    Do this instead

    Use or when at least one condition is enough.

  • Common mistake

    Using or when every requirement must be satisfied

    What it looks like

    A login expression uses username or password even though both are required.

    Why it fails

    or returns True when only one condition is True, so one missing requirement could still produce a True result.

    Do this instead

    Use and for simultaneous requirements.

  • Common mistake

    Forgetting that not reverses the whole expression to which it applies

    What it looks like

    A negated comparison is written without parentheses when the intended grouping is unclear.

    Why it fails

    The reader may not know which comparison or compound expression is meant to be negated.

    Do this instead

    Use parentheses around comparisons or compound expressions when applying not.

  • Common mistake

    Ignoring logical precedence in a long expression

    What it looks like

    An expression combines not, and, and or without visible grouping.

    Why it fails

    The expression is evaluated in the order not, then and, then or, which may not match the reader's intended grouping.

    Do this instead

    Add parentheses to make the intended order explicit.

Practice the Operators

MEDIUM

For each expression, predict whether the result is True or False. Then explain which rule determines the result: both conditions for and, at least one condition for or, reversal for not, or precedence and grouping.

  1. True and False
  2. False or True
  3. not (True and False)
  4. (True or False) and False
  5. not (False or False)
Hints
  • For and, look for whether both inputs are True.
  • For or, check whether at least one input is True.
  • For not, evaluate the expression inside the parentheses first, then reverse its Boolean result.
  • Use the stated precedence order when parentheses do not already determine the grouping.
Worked example

Practice Check

Evaluate not (True and False).

  1. 1
    Evaluate the parenthesized expression

    True and False is False because and requires both conditions to be True.

  2. 2
    Apply not

    not reverses False to True.

Result

The complete expression evaluates to True.

Key Takeaways

  1. 1
    and returns True only when both conditions are True.
  2. 2
    or returns True when at least one condition is True.
  3. 3
    not reverses a Boolean result: True becomes False, and False becomes True.
  4. 4
    Comparison results can be combined with logical operators in conditional expressions.
  5. 5
    The precedence order is not, then and, then or; use parentheses when they improve clarity.

At a glance

  • Use and for requirements that must all be satisfied.
  • Use or for alternative conditions where at least one possibility is acceptable.
  • Use not to negate a Boolean expression.
  • Comparison results can be combined into larger conditional expressions.
  • Remember the precedence order not, and, or, and use parentheses to make intent clear.

More concepts starting with B