Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
operand_left or operand_right

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.

# Standard boolean evaluation
True or False        # Evaluates to: True


# First operand is truthy: Returns the first operand
"Python" or ""       # Evaluates to: "Python"
[1, 2] or [3, 4]     # Evaluates to: [1, 2]
10 or 0              # Evaluates to: 10


# First operand is falsy: Returns the second operand
0 or 20              # Evaluates to: 20
"" or "String"       # Evaluates to: "String"
None or []           # Evaluates to: []


# Both operands are falsy: Returns the second operand
0 or False           # Evaluates to: False
"" or 0              # Evaluates to: 0

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.
def side_effect():
    print("Evaluated!")
    return True


# side_effect() is NOT called because 1 is truthy
result_1 = 1 or side_effect() 

# result_1 is 1


# side_effect() IS called because 0 is falsy
result_2 = 0 or side_effect() 

# Prints: "Evaluated!"

# result_2 is True

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.

# Stops at 5 (first truthy value)
result = 0 or [] or 5 or "String"  # Evaluates to: 5


# Evaluates all, returns the last falsy value
result = 0 or [] or "" or None     # Evaluates to: None

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.

# 'and' is evaluated before 'or'

# Equivalent to: "A" or ("" and "B") -> "A" or "" -> "A"
"A" or "" and "B"    # Evaluates to: "A"


# Equivalent to: ("" or "A") and "B" -> "A" and "B" -> "B"
("" or "A") and "B"  # Evaluates to: "B"
Master Python with Deep Grasping Methodology!Learn More