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.
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.
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.
- 1Check the first condition
has_usernamerepresents whether the username requirement is satisfied. - 2Check the second condition
has_passwordrepresents whether the password requirement is satisfied. - 3Combine 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.
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.
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.
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.
Making Grouping Explicit
Express a decision that requires both a username and a password, while also showing clearly which conditions are grouped together.
- 1Identify the simultaneous requirements
The username and password conditions must both be satisfied, so they belong together with and.
- 2Use parentheses
Parentheses show the intended grouping directly, even though Python has a defined precedence order.
- 3Read 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
Common mistake Using and when either condition should be acceptable
What it looks likeA number should be accepted when it is divisible by 2 or divisible by 3, but the expression requires both.
Why it failsand returns True only when both conditions are True, so it rejects cases that satisfy just one acceptable option.
Do this insteadUse or when at least one condition is enough.
Common mistake Using or when every requirement must be satisfied
What it looks likeA login expression uses username or password even though both are required.
Why it failsor returns True when only one condition is True, so one missing requirement could still produce a True result.
Do this insteadUse and for simultaneous requirements.
Common mistake Forgetting that not reverses the whole expression to which it applies
What it looks likeA negated comparison is written without parentheses when the intended grouping is unclear.
Why it failsThe reader may not know which comparison or compound expression is meant to be negated.
Do this insteadUse parentheses around comparisons or compound expressions when applying not.
Common mistake Ignoring logical precedence in a long expression
What it looks likeAn expression combines not, and, and or without visible grouping.
Why it failsThe expression is evaluated in the order not, then and, then or, which may not match the reader's intended grouping.
Do this insteadAdd parentheses to make the intended order explicit.
Practice the Operators
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.
- True and False
- False or True
- not (True and False)
- (True or False) and False
- 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).
- 1Evaluate the parenthesized expression
True and False is False because and requires both conditions to be True.
- 2Apply not
not reverses False to True.
The complete expression evaluates to True.
Key Takeaways
- 1and returns True only when both conditions are True.
- 2or returns True when at least one condition is True.
- 3not reverses a Boolean result: True becomes False, and False becomes True.
- 4Comparison results can be combined with logical operators in conditional expressions.
- 5The 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.