How to use the Python OR Operator
The Python OR logical operator is used to combine multiple conditional statements. Using OR statements can make your code cleaner since you only need one if, elif or while statement to deal with multiple conditionals.
Python if OR Statement
In the example below, we will use the Python OR operator to check if at least one of two conditions are True.
if 2 > 2 or 3 > 2:
print('One of the conditions is True.')
One of the conditions is True.
Three is greater than one so the code inside the block will run. The code will not run if none of the conditionals in True
:
if 2 > 2 or 3 > 3:
print('One of the conditions is True.')
else:
print('Both are false.')
Both are false.
Python elif OR Statement
Here is another Python OR example, this time with an elif statement comparing multiple conditions.
if 2 > 2:
print('True')
elif 2 > 2 or 3 > 2:
print('One of the conditions is True.')
One of the conditions is True.