Python Or Operator

5/5 - (2 votes)

Python’s or operator performs the logical OR operation that returns True if at least one of the operands evaluates to True. The operator performs an optimization called short-circuiting, so if the first operand evaluates to True, it returns the first right away without further evaluating the second, and if the first operand evaluates to False, it returns the second operand.

As you read through the article, you can also watch my video for supporting explanations:

Python OR Operator - Deep Dive

Python or Operator – on Booleans

Here’s the result of the or operator when applied to Boolean operands:

First Operand ASecond Operand BA or B

You can see those examples in the following script:

>>> False or False
>>> False or True
>>> True or False
>>> True or True

Python or Operator – on Integers

You can also apply the or operator to integers:

First Operand ASecond Operand BA or B

The same can be seen in the following Python script:

>>> 0 or 0
>>> 0 or 1
>>> 1 or 0
>>> 1 or 1

Python or Operator – on Objects

Interestingly, you can also apply the logical OR operator on arbitrary Python objects. The base idea is the “truthiness” of Python objects, i.e., every Python object has an associated Boolean value as determined by the __bool__() magic method.

In the following code, you create a custom class called My_Obj and use the “or” operator on instances of this class.

>>> class My_Obj:

>>> My_Obj() or My_Obj()
<__main__.My_Obj object at 0x0000025108D897F0>
>>> 0 or My_Obj()
<__main__.My_Obj object at 0x0000025108D897F0>
>>> 1 or My_Obj()

You may wonder why some of the outputs are as strange-looking as they are. The reason is simple: every custom object has an associated implicit Boolean value:

>>> bool(My_Obj())

If the first operand of the or operation evaluates to False, Python performs the short-circuiting optimization, i.e., it immediately returns the second operand. This makes sense because the second operand logically determines the overall result (see tables below where the first operand is False).

First Operand ASecond Operand BA or B

Thus, it is not needed to even evaluate the second operand—and Python simply returns it without modification. That’s why the result of the or operation can be a custom object instead of a Boolean value.

Python or Operator – on Lists

You can apply the or operator on arbitrary objects, including lists. Every list object has an associated Boolean value as determined by the bool(list) method—all lists evaluate to True except the empty list. If you apply the or operator on two lists, Python returns the first list if it is non-empty (due to short-circuiting), and the second list otherwise.

>>> [1, 2, 3] or [4, 5, 6]
[1, 2, 3]
>>> [] or [4, 5, 6]
[4, 5, 6]
>>> [1, 2, 3] or []
[1, 2, 3]
>>> [] or []

So, you’d use the Python or operator on lists, for example, if you’d had a number of backup lists (the second, third, fourth, …) that should be used instead of the higher-priority ones in case they’re empty.

Python or Operator Short Circuit

Short-circuiting is a common optimization in programming languages that use the following observation: if the first operator evaluates to True, the whole or expression must evaluate to True too. Thus, it is not needed to evaluate subsequent operands to know the result of the overall logical expression. Only if the first operand is False, it moves further to the second operand—and, potentially, the third and so on. This can save significant time when evaluating complicated logical expressions.

A minimal example of short-circuiting on the or operator is shown here:

>>> True or print('Am I evaluated?')
>>> False or print('Am I evaluated?')
Am I evaluated?

We use a simple trick to check if the second operand is evaluated or not due to the short-circuiting optimization—the second operand is a print() statement that always returns None but generates visible side-effects when executed. You can see that in the first example, it is not evaluated because the first operand already determines the result of the whole expression. But in the second example, it is evaluated because the first operand does not provide a conclusive answer on the result of the overall expression.

Python or Operator vs |

Python’s “or” operator performs a logical OR operation that returns True if one or more operands are True. Python’s “|” symbol is a bitwise operator that works on the bit representations of the operands and performs a bit by bit operation. So, “or” tests whether at least one operand is logically True whereas “|” performs bitwise OR operation on the operands.

When considering only a single bit, semantically, such as when comparing Booleans, the operators are the same:

>>> True or False
>>> True | False

However, the difference becomes apparent if you use integer operands:

>>> 32 or 16
>>> 32 | 16

The first expression 32 or 16 determines that the integer value 32 is associated to the Boolean True, so it returns the first operand 32 as it performs the short-circuiting optimization.

The second expression 32 | 16 operates on the bit representations 10000 (decimal 32) and 01000 (decimal 16) and performs bit-wise OR. The result is the bit representation 11000 which represents the decimal 48:

First Operand A100000
Second Operand B010000
A | B110000

Similarly, let’s have a look at another example to showcase the bitwise OR operator:

>>> 31 | 15

You can see this in the following visualization:

First Operand A11111
Second Operand B01111
A | B11111

The decimal representation of the bit sequence 11111 is 31.

Python or Operator Evaluation Order

Python evaluates the left before the right operand. It uses short-circuiting when evaluating expressions involving the logical and or or operators, so it doesn’t evaluate the second operand unless it is necessary to determine the result. The logical and takes precedence before the logical or.

In the following example, you see that the “and” operator is evaluated first because it has precedence over the “or” operator.

>>> False and False or True
>>> (False and False) or True
>>> False and (False or True)

The same precedence can be seen here:

>>> True or False and False
>>> (True or False) and False
>>> True or (False and False)

This also rules out what many people wrongly assume—that there’s a first-comes-first-serve operator precedence. In this example, the “or” operator is evaluated last even though it comes first.

Python or Operator in If Condition

If you want to use the logical OR operator in an if condition, use the keyword or instead of the double || symbol you may know as the logical OR from Java or C++. For example, if A or B would be correct whereas if A || B would be incorrect.

Here’s the correct use of the logical AND in an if condition:

# YES!
if 2+2 == 4 or 2-2 == 0:

The result of this code snippet is 'YES!'.

Here’s the wrong way of doing it as a C++ or Java coder going Python:

# NO!
if 2+2 == 4 || 2-2 == 0:

The result of this code snippet is a SyntaxError:

To resolve this, simply replace the faulty double symbol || with the correct keyword or.

Logical Operators

Logical operators are shortcuts to perform Boolean comparisons, but can be used on integers and other objects as well.

andReturns True if both operands are True, and False otherwise.(True and True) == True
orReturns True if at least one of the two operands is True, and False otherwise.(False or True) == True
notReturns True if the single operand is False, and False otherwise.(not True) == False