Or Operator in Python

Introduction

You can combine two Boolean expressions into one compound expression using the Boolean OR operator. It makes no difference which of the subexpressions is true for the compound expression to be true. If both subexpressions are false, the expression is incorrect. This is the underlying logic of the OR operator.

Using or with Boolean Expressions

To create a Boolean expression with the Python or operator as a connector, you'll need two subexpressions. The following is the basic syntax for a Boolean expression with or:

 # Syntax for Boolean expression with or in Pythonexp1 or exp2

The expression is considered True if at least one of the subexpressions (exp1 or exp2) evaluates to True. The expression returns False if both subexpressions return False. This is known as an inclusive or definition because it allows for both possibilities as well as either.

Truth Table

The truth value of a Boolean expression, such as exp1 or exp2, is summarized in this table based on the truth values of its subexpressions.

Let us code some practical examples to demonstrate the resulting truth values shown in Table:

 >>> exp1 = 1 == 2>>> exp1False>>> exp2 = 7 > 3>>> exp2True>>> exp1 or exp2  # Return True, because exp2 is TrueTrue>>> exp2 or exp1  # Also returns TrueTrue>>> exp3 = 3 < 1>>> exp1 or exp3  # Return False, because both are FalseFalse

Using or with Common Objects

In general, the operands of an expression involving an OR operation should have Boolean values as shown in the Table and return a truth value as a result. When it comes to objects, Python is not too strict and internally implements a set of rules to determine whether an object is true or false:

An object is assumed to be true by default unless its class defines a __bool__() method that returns False or a __len__() method that returns zero when called with the object. The majority of the built-in objects that are considered false are as follows:

• False and none are defined as false constants.
• Any numeric zero: 0, 0.0, 0j, Decimal(0), Fraction (0, 1)
• ", (), [], set(), range, collections (0) and empty sequences.

If the operands of an or operation are objects rather than Boolean expressions, the Python or operator returns a true or false object rather than the expected True or False values. The truth value of this object is determined by the previously discussed rules.

This means Python does not force the outcome of an or operation to be a bool object. Regardless of its true value, if you use Python to test two objects, the operator will return the first object that evaluates to true or the last object in the expression

 >>> 2 or 32>>> 5 or 0.05>>> [] or 33>>> 0 or {}{}

Python or Operator Behavior While Testing Objects Instead of Boolean Expressions

Regardless of its truth value, the Python or operator returns the first object that evaluates to true or the last object in the expression.

This behaviour can be generalised by combining several operations in a single expression, as shown below:

 a or b or c or d

In this case, the Python or operator returns the first or last true operand found. This is the general rule for remembering how or works in Python.

Mixing Boolean Expressions and Objects

In an or operation, you can also combine Boolean expressions and common Python objects. The Python or operator will still return the first true operand or the last operand in this case, but the returned value could be True or False or the object you're testing:

Operator or Python Behavior While Testing Objects and Boolean Expressions

Let us look at some examples to see how this actually works:

 >>> 2 < 4 or 2  # Case 1True>>> 2 < 4 or []  # Case 2True>>> 5 > 10 or []  # Case 3[]>>> 5 > 10 or 4  # Case 44

The subexpression 2<4 was evaluated to True in Cases 1 and 2, and the returned value was True. In Cases 3 and 4, however, the subexpression 5 > 10 was evaluated to False, so the last operand was returned, and instead of True or False, you received an empty list ([]) and an integer (4).

Conclusion

In this article, you have learned how or condition in python works, it features and its conditions.

Python

Blog | Board Infinity

At Board Infinity we have authors leading in their profession sharing their insights, ideas and inspiration. Here influential thinkers, creators, makers and doers are found in one place.