Menu

Logical Operators

OR Operator

OR is a logical (Boolean) operator that joins two conditions into one. The joined condition is true when at least one of the conditions is true, and it is false only when every condition inside it is false.

A truth table lists every combination of inputs and the result OR produces:

Condition ACondition BA OR B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Look at the first row. OR is inclusive: when both conditions are true, the result is still true. It does not mean "one or the other but not both". If a rule can be met in two ways and you meet both of them, you have certainly met the rule.

Use OR in an algorithm when a rule can be satisfied in more than one way. A cinema sells child tickets to anyone whose age < 12 OR who has a family card. A 9-year-old with no card qualifies because the first condition is true; a 14-year-old with a card qualifies because the second condition is true; a 9-year-old with a card also qualifies, because a true condition on either side is enough. Only someone aged 12 or over with no card fails, since both conditions are then false.

Four rows for the cinema rule age under 12 OR family card: True or False for each condition on the left, an arrow, and the OR result on the right. Only the row where both are False gives False.

age = int(input("How old are you? "))
card = input("Do you have a family card? yes or no: ")

if age < 12 or card == "yes":
    print("Child ticket price")   # one true test is enough
else:
    print("Full ticket price")    # both tests were false

Output when the user types 9, no:

How old are you? 9
Do you have a family card? yes or no: no
Child ticket price

The two tests are joined by or, so the program takes the reduced-price branch as soon as one test passes. Run through the four combinations of the two tests and you get exactly the pattern in the truth table.

Practise this