Logical Operators: and, or, not
Most statements (logical lines) that you write will contain expressions . A simple example of an expression is 2 + 3 . An expression can be broken down into operators and operands.
A Decision with Several Conditions
A logical expression helps combine or change conditions. Python provides three logical operators: and, or, and not. Their meanings are similar to their meanings in everyday English, so they provide a compact way to express whether multiple conditions should be considered together.
An expression is a piece of code that can be evaluated. An expression can be broken down into operators and operands. In a logical expression, and, or, and not are operators, while the conditions or values they act on are operands.
The diagram compares the two binary operators. With and, the combined condition is true only when both operands are true. With or, the combined condition is true when at least one operand is true.
The Three Operators
| Operator | Meaning | True result |
|---|---|---|
| and | Combines two conditions | Both conditions are true |
| or | Combines two conditions | At least one condition is true |
| not | Changes one condition to its opposite logical result | A true condition becomes false, and a false condition becomes true |
Basic meanings of Python's logical operators
The operators have different jobs. The and operator is restrictive: every condition it joins must be true. The or operator is less restrictive: one true condition is enough. The not operator works with one condition and reverses its logical result.
A logical expression can therefore contain two operands around and or or, while not is placed before the operand whose result it changes. The operands are the conditions or values being acted on; the logical words are the operators.
Tracing a Combined Condition
Checking two requirements
Suppose a learner must have both a completed registration and an approved request. Represent the combined requirement with a logical operator.
Name the operands: The two operands are the condition that registration is complete and the condition that the request is approved.
Choose the operator: Because both requirements must be satisfied, use and rather than or.
Evaluate a case: If registration is complete but the request is not approved, one operand is true and the other is false. The combined and condition is false.
Compare another case: If both registration and approval are complete, both operands are true. The combined and condition is true.
Use and when every listed condition must be true. A single false operand makes the combined and condition false.
What do you think happens?
Suppose condition A is true and condition B is false. Which result should you expect from A or B?
Reveal answer
Answer: true
The or operator is true when at least one operand is true. Condition A supplies one true operand even though condition B is false.
The choice between and and or depends on the requirement being expressed. Use and for conditions that must all hold together. Use or when any one of the conditions can satisfy the combined requirement.
Reversing a Condition
The not operator does not combine two requirements. Instead, it changes the logical result of one operand. Applying not to a true condition produces false; applying it to a false condition produces true.
Expressing an exception
A process should continue when a condition is not true. Choose the logical operator that expresses this reversal.
Identify the single condition: There is one condition whose logical result needs to be changed.
Place not before it: The not operator is used before the operand it reverses.
Interpret the result: When the original condition is true, the expression with not is false. When the original condition is false, the expression with not is true.
Use not when the requirement is the opposite of one condition.
Values as Logical Operands
This rule means that a logical expression does not always have to be written using explicit words such as true or false. When a number is used as an operand, Python interprets a nonzero number as true. For clear beginner code, it is still useful to identify what each operand represents before selecting and, or, or not.
Mistakes with Logical Meaning
Using or when every condition must be satisfied.
The or operator accepts one true operand, so it does not express an all-requirements condition.
Fix:
Use and when both conditions must be true.Using and when either condition is sufficient.
The and operator requires both operands to be true.
Fix:
Use or when at least one condition is enough.Treating not as an operator that combines two conditions.
The not operator changes one operand's logical result; and and or are the operators that combine two operands.
Fix:
Place not before the single condition that should be reversed.Assuming every operand must be written as a boolean expression.
Python is not very strict about logical operands and interprets any nonzero number as true.
Fix:
Remember Python's rule when a number appears as a logical operand.
Simplifying Conditions
Logical operators often provide a way to simplify nested conditional statements. Instead of handling related conditions through several nested decisions, a single conditional can combine the conditions with and or or. The important design step is to describe the requirement in ordinary language first: decide whether all conditions are required, whether one is enough, or whether one condition must be reversed.
- List the conditions that matter.
- Mark each condition as required together, an alternative, or an opposite case.
- Use and for conditions required together.
- Use or when one true condition is sufficient.
- Use not when the desired meaning is the opposite of one condition.
- Check the result using true and false cases before relying on the expression.
Practice Check
For each requirement, choose and, or, or not, and explain why: a learner must complete two required steps; a learner may satisfy either of two alternatives; a process should run when one existing condition is not true.
Hints
- Ask whether every condition is required.
- Ask whether one condition is enough.
- For the final case, look for a single condition whose meaning must be reversed.
- The three operators have distinct roles: and requires both combined operands to be true, or is true when at least one combined operand is true, and not reverses one operand's logical result. Logical expressions are built from operators and operands, and Python interprets any nonzero number as true when it is used as an operand.
Key Takeaways
- Python has three logical operators: and, or, and not.
- Use and when all combined conditions must be true.
- Use or when at least one combined condition can be true.
- Use not to reverse the logical result of one condition.
- Logical operators act on operands, and Python interprets any nonzero number as true.