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

First operand | Second operand | Result

True | True | True

True | False | False

False | True | False

False | False | False

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

First operand | Second operand | Result

True | True | True

True | False | True

False | True | True

False | False | False

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 True becomes False.
  • not False becomes True.

not truth table

Operand | Result

True | False

False | True

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
  1. Evaluate the first comparison: 5 < 3 is False.
  2. Evaluate the second comparison: 5 < 33 is True.
  3. Apply and: False and True is False.
  4. Apply not to the grouped result: not False is True.

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

  1. Find each individual comparison or Boolean value.
  2. Evaluate each one as exactly True or False.
  3. Use the relevant truth table for and, or, or not.
  4. Write Python Boolean values with an uppercase first letter: True and False.
Expression: 5 < 7 and 3 == 4

5 < 7  -> True
3 == 4 -> False
True and False -> False

Common mistakes

  • Expecting and to be true when only one condition is true: both operands must evaluate to True.
  • Expecting or to 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 True or False before the logical operator is applied.
  • Using one equals sign for equality testing: use == to compare values. A single = is used for assignment.

Quick reference

Operator | Meaning | True result

and | Both conditions must be true | True only if both operands are True

or | At least one condition must be true | True if either or both operands are True

not | Reverse one condition | True when its operand is False

Logical operators combine the Boolean results of expressions. Remember: and needs both conditions, or needs at least one condition, and not reverses one result.