VMware ESXi and vSphere Cluster Management
Python Logical Operators: and, or, and not
Learn how Python's and, or, and not logical operators combine Boolean expressions, use truth tables, and evaluate grouped conditions.
Logical operators let a Python program combine or negate conditions. They are especially useful in if statements and other expressions that need to make decisions.
A Boolean is a truth value represented in Python by True or False. A logical operator combines Boolean conditions or reverses one. Python has three logical operators: and, or, and not.
An operand is a value or expression on which an operator acts. For example, in 5 < 7 and 5 > 3, the two comparison expressions are the operands of and.
Boolean and comparison expressions
An expression is code that evaluates to a value. A comparison expression uses operators such as <, >, <=, >=, ==, and != and produces a Boolean result.
>>> 5 < 7
True
>>> 3 == 3
True
>>> 5 != 5
False
Logical operators use these results as operands and produce a complete logical result. When reading an expression, determine the value of each comparison first, then apply the logical operator.
The and operator
and is a binary logical operator, meaning it works with two operands. It returns True only when both operands are True. Every other two-operand combination returns False.
and truth table
Examples with and
>>> 5 < 7 and 5 > 3
True
>>> 3 > 3 and 55 > 30
False
>>> 15 / 3 >= 200 and 3 == 3
False
>>> 55 == 55 and 3 <= 3
True
In the second example, 3 > 3 is False and 55 > 30 is True. The combination is therefore False and True, which is False.
In the arithmetic example, 15 / 3 equals 5.0, so 15 / 3 >= 200 is False. Even though 3 == 3 is True, and requires both conditions to be true.
The or operator
or is also a binary logical operator. It returns True when at least one operand is True. It returns False only when both operands are False.
or truth table
Examples with or
>>> 3 == 3 or 5 < 3
True
>>> 15 < 3 or 5 > 3
True
>>> 55 == 55 or 3 <= 3
True
>>> 12 <= 1 or 5 < 1
False
In the first example, the first condition is true, so the complete or expression is true. In the second, the first condition is false but the second is true, so the result is still true. In the third, both conditions are true. The final example is false because both comparisons are false.
The not operator
not is a unary logical operator, meaning it works with one operand. It reverses a Boolean value or Boolean expression:
not TruebecomesFalse.not FalsebecomesTrue.
not truth table
Examples with not
>>> not True
False
>>> not False
True
>>> not 5 > 3
False
The comparison 5 > 3 evaluates to True. Applying not reverses that result, so the complete expression is False.
Evaluating grouped logical expressions
Parentheses are grouping symbols that make it clear which expression should be evaluated first. They are especially important when not should reverse the final result of a compound condition.
>>> not (5 < 3 and 5 < 33)
True
- Evaluate the first comparison:
5 < 3isFalse. - Evaluate the second comparison:
5 < 33isTrue. - Apply
and:False and TrueisFalse. - Apply
notto the grouped result:not FalseisTrue.
The parentheses make the intended operation explicit: first combine the two comparisons with and, then reverse the final Boolean result.
A reliable method for reading Boolean results
- Find each individual comparison or Boolean value.
- Evaluate each one as exactly
TrueorFalse. - Use the relevant truth table for
and,or, ornot. - Write Python Boolean values with an uppercase first letter:
TrueandFalse.
Expression: 5 < 7 and 3 == 4
5 < 7 -> True
3 == 4 -> False
True and False -> False
Common mistakes
- Expecting
andto be true when only one condition is true: both operands must evaluate toTrue. - Expecting
orto require both conditions to be true: either operand, or both operands, may be true. - Negating only part of a compound condition unintentionally: use parentheses when you mean to negate the combined result, such as
not (condition_a and condition_b). - Treating a comparison as text: evaluate each comparison first; it becomes
TrueorFalsebefore the logical operator is applied. - Using one equals sign for equality testing: use
==to compare values. A single=is used for assignment.
Quick reference
Logical operators combine the Boolean results of expressions. Remember: and needs both conditions, or needs at least one condition, and not reverses one result.