Skip to main content
The or operator in Python is a logical operator that performs inclusive disjunction. It evaluates two expressions and returns the first operand if it resolves to a truthy value; otherwise, it evaluates and returns the second operand. Unlike some languages where logical operators strictly return boolean types (True or False), Python’s or operator returns the actual object reference of the evaluated operand.

Evaluation Mechanics

Python evaluates the or operator using short-circuit evaluation. The interpreter processes the operands from left to right based on their boolean context (truthiness).
  1. operand_left is evaluated.
  2. If operand_left is truthy, the expression immediately returns operand_left. The operand_right expression is completely ignored (short-circuited).
  3. If operand_left is falsy, the interpreter evaluates and returns operand_right.

Truthiness and Return Values

In Python, objects like None, 0, 0.0, empty sequences ("", [], ()), and empty mappings ({}) are considered falsy. All other objects are generally considered truthy.

Short-Circuit Demonstration

Because of short-circuiting, the right-hand operand is only executed if the left-hand operand is falsy. This is observable when the right-hand operand contains a function call or an operation with side effects.

Chaining Multiple or Operators

When multiple or operators are chained, the interpreter evaluates from left to right and stops at the very first truthy operand it encounters, returning that operand. If no truthy operand is found, it returns the final falsy operand.

Operator Precedence

The or operator has the lowest precedence among Python’s logical operators (not, and, or). It evaluates after and but before assignment operators.
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More