Logical Operators and Truth Tables
A boolean expression is any expression that evaluates to True or False; a condition is a boolean expression inside a conditional statement; a conditional statement controls program flow based on that condition.
From Truth to Control Flow
Programs often need to choose between alternative execution paths. The starting point is a boolean expression: an expression that evaluates to True or False. When that boolean expression is used inside a conditional statement, it becomes a condition. The conditional statement then uses the condition to control program flow.
These three ideas describe different levels of the same process. A boolean expression produces a Boolean value. A condition is that boolean expression in its control-flow role. A conditional statement is the larger structure that selects an execution path based on the condition.
Combining Comparisons
Comparison operators and logical operators are the main tools for building conditions. The comparison operators listed in the source are ==, !=, >, <, >=, and <=. They compare values and contribute boolean expressions. The logical operators are and, or, and not. They combine or modify boolean expressions so that a condition can represent more than one requirement.
Combining Two Requirements
Suppose a program needs one condition that represents both of these requirements: a score is at least 50, and a submission is marked complete.
Build the comparisons: Each requirement can be represented by a comparison, and each comparison produces a boolean value.
Join the comparisons: The logical operator and combines the two boolean expressions into one compound condition.
Use the compound condition: The resulting condition can be placed inside a conditional statement to control which branch runs.
A compound condition is made from boolean expressions joined by a logical operator.
A compound condition is not a new kind of value. It is a larger boolean expression built from smaller boolean expressions with logical operators.
Reading the Truth Table
A truth table makes the behavior of logical operators explicit. It lists possible truth values for input conditions and shows the resulting truth value after an operator is applied. For two input conditions, the possible input combinations are True and True, True and False, False and True, and False and False. The operator and requires both inputs to be True. The operator or produces True when at least one input is True. The operator not reverses one Boolean value.
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
Truth values produced by and and or for two input conditions.
| A | not A |
|---|---|
| True | False |
| False | True |
The not operator reverses the Boolean value of one input.
Scope and Indentation
A conditional statement is a compound statement. Its structure has a header ending with a colon and an indented body. The body contains the statements controlled by that header. In Python, indentation is syntactically significant: it defines the scope of the body rather than serving only as visual formatting.
When reading a conditional, first identify the header and its condition. Then identify every statement at the corresponding indentation level. Those indented statements belong to the conditional body. A statement that is not part of that indentation level is outside that body and is not controlled by the same condition.
Three Branching Patterns
Branches are alternative execution paths. A chained conditional places multiple alternative tests in one sequence by using multiple elif clauses. A nested conditional places one conditional inside another, so a later decision occurs within an earlier branch. A guarded conditional uses a condition as a guard before the program proceeds with an action or another part of the flow.
| Pattern | Structure | Control-flow idea |
|---|---|---|
| Chained | Multiple elif clauses | Tests alternative conditions in one sequence |
| Nested | One conditional inside another | Tests a later condition inside an earlier branch |
| Guarded | A condition used as a guard | Allows the guarded path only when its condition is satisfied |
Short-Circuit Evaluation
Short-circuit evaluation affects program execution by allowing the result of a compound condition to be determined before every expression has been evaluated. With and, a False result from an earlier operand is enough to determine that the combined result is False. With or, a True result from an earlier operand is enough to determine that the combined result is True. When the result is already determined, later expressions in that logical combination are skipped.
Tracing the Skipped Operand
Consider a compound condition with A and B. Determine whether B must be evaluated when A is False and the operator is and, and when A is True and the operator is or.
False and B: The first operand is False, so the result of the and combination is already False. B is skipped.
True or B: The first operand is True, so the result of the or combination is already True. B is skipped.
The opposite cases: A True first operand with and, or a False first operand with or, does not determine the final result. The later operand must then be evaluated.
Short-circuit evaluation skips a later operand only when an earlier operand already determines the result.
Common Reasoning Mistakes
Treating a boolean expression, a condition, and a conditional statement as identical terms.
A boolean expression produces True or False, a condition is that expression in a conditional context, and a conditional statement controls program flow.
Fix:
Identify whether you are discussing the value-producing expression, its use as a condition, or the larger statement.Assuming indentation is only for readability.
Indentation defines scope and is syntactically significant in Python compound statements.
Fix:
Use the header and matching indentation to determine the statements controlled by the condition.Evaluating every operand in a compound condition automatically.
Short-circuit evaluation skips a later expression when the earlier operand already determines the result.
Fix:
For and, stop after a decisive False. For or, stop after a decisive True.Confusing chained and nested conditionals.
Chained conditionals use multiple elif clauses, whereas nested conditionals place one conditional inside another.
Fix:
Look for the physical structure: a sequence of alternatives indicates chaining; a conditional inside a branch indicates nesting.
Practice
For each case, identify the result and whether the second operand needs to be evaluated: False and B; True and B; True or B; False or B. Then classify each situation as a boolean expression, a condition, or a conditional statement when appropriate.
Hints
- For and, a False first operand determines the combined result.
- For or, a True first operand determines the combined result.
- A condition is a boolean expression being used inside a conditional statement.
Describe the difference between these three designs: a chained conditional with several elif clauses, a nested conditional with one conditional inside another, and a guarded conditional that checks a condition before proceeding.
Hints
- Focus on whether the alternatives are in one sequence or inside another branch.
- For a guarded conditional, identify the condition that controls access to the guarded path.
Key Takeaways
- A boolean expression evaluates to True or False; a condition is a boolean expression used by a conditional statement.
- Comparison operators and logical operators build simple and compound conditions.
- A compound statement has a header ending with a colon and an indented body, and indentation defines its scope in Python.
- Chained, nested, and guarded conditionals organize alternative execution paths in different ways.
- Short-circuit evaluation skips later operands when and or or has already determined the result.
Key Takeaways
- Boolean expressions produce True or False, while conditions and conditional statements use those values to control program flow.
- Comparison operators and logical operators combine to form conditions and compound conditions.
- Truth tables show how and, or, and not transform input truth values.
- Indentation defines the scope of a Python compound statement.
- Short-circuit evaluation can prevent later operands from being evaluated.