Concepts / Boolean Values and Comparison Operators

Boolean Values and Comparison Operators

The and operator returns True only when both conditions are true; use it when you need multiple requirements to be satisfied simultaneously.

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

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.

Checking Two Requirements

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

Check the first condition: has_username represents whether the username requirement is satisfied.

Check the second condition: has_password represents whether the password requirement is satisfied.

Combine the conditions: and requires both condition results to be True.

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.

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.

notnotTruebefore notFalseafter notFalsebefore notTrueafter not
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.

evaluatecondition holdscondition does not holdxinput valuex > 5comparisonTruecondition satisfiedFalsecondition not satisfied
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.

andandcombine alternativesevaluateusername checkBoolean resultpassword checkBoolean resultusername and passwordboth requiredalternative checkorfinal decisionTrue or False
How do multiple comparison results combine with and, or, and not before a final decision?

Making Grouping Explicit

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

Identify the simultaneous requirements: The username and password conditions must both be satisfied, so they belong together with and.

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

Read the result: The grouped expression is True only when both requirements are True.

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

Common Logic Mistakes

  • Using and when either condition should be acceptable

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

    Fix: Use or when at least one condition is enough.

  • Using or when every requirement must be satisfied

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

    Fix: Use and for simultaneous requirements.

  • Forgetting that not reverses the whole expression to which it applies

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

    Fix: Use parentheses around comparisons or compound expressions when applying not.

  • Ignoring logical precedence in a long expression

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

    Fix: 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.

Practice Check

Evaluate not (True and False).

Evaluate the parenthesized expression: True and False is False because and requires both conditions to be True.

Apply not: not reverses False to True.

The complete expression evaluates to True.

Key Takeaways

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

Key Takeaways

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