Python or keyword

If any of the expressions, statements, or operands evaluate to true, the "or" keyword returns True. For example:

a = 50
b = 60
c = 70
d = 80

print(a>b or b>c or d>c)

The output is:

True

Or is a logical operator that combines boolean expressions. The "or" keyword returns True if at least one of the expressions evaluates to True; otherwise, it returns False.

The "or" keyword employs short-circuit evaluation, which means that if the first expression in a "or" expression evaluates to True, the second expression is not evaluated.

The "or" keyword has lower precedence than comparison operators (such as ==, >, <), but higher precedence than "and."

The "or" keyword returns the first value that is considered "True" in a boolean context when used with non-boolean values. If no value is considered "True", the last value will be returned.

The "or" keyword may also be used in list comprehension and generator expressions. The "or" and "not" keywords can be combined to create more complex boolean expressions. The "or" keyword is available in all Python versions as part of the standard library.

Common uses of the "or" keyword in programming include testing for multiple conditions simultaneously and assigning default values to variables.

The truth table of the Python "or" keyword

The table given below shows the truth table of the Python "or" keyword:

X Y X or Y
True True True
True False True
False True True
False False False

Python "or" keyword example

Here is an example of the Python "or" keyword:

print(True or False)
print(False or False)
print(True or True)
print(True or True or True or False or True or True)
print(False or False or False or False or False)

The output is:

True
False
True
True
False

Advantages of the "or" keyword in Python

Disadvantages of the "or" keyword in Python

Python Online Test


« Previous Tutorial Next Tutorial »