Logical Operators
A relational operator answers only one question. But many decisions need two questions to be answered together: is the age above 18 and is the licence valid? The three logical operators — and, or and not — join True and False values together to give one answer.
1Three operators, three rules
Their operands are boolean values, and their answer is also a boolean value. Each operator has one simple rule. Learn the rule, and you know the operator:
andTrue only when BOTH operands are True.
If even one operand is False, the answer is False.
orTrue when AT LEAST ONE operand is True.
The answer is False only when both operands are False.
notReverses its operand. True becomes False, and False becomes True.
It is different from the other two. It works on one operand only.
Going on the school trip
and — you must bring the permission slip and pay the fee. If even one of them is missing, you cannot go.
or — you may come by bus or come with your parents. Any one of the two is enough.
not — the trip will happen if it is not raining. Here only one fact is checked, and its answer is reversed.
2and — both, or nothing
There are four possible combinations. Out of these, and gives True for only one — the one where both operands are True.
3or — one is enough
This is the opposite of and. Out of the four combinations, or gives False for only one — the one where both operands are False.
4not — one operand, reversed
not is the only operator you have learnt so far that works on a single operand. It is not written between two values. It is written in front of one value, and it reverses that value.
5Make the truth tables yourself
Switch A and B on and off below. The three answers change at once, and the matching row lights up in the tables underneath. Try all four combinations of A and B. Together they make the complete truth table, and you have now worked it out yourself instead of learning it by heart.
True and FalseTrue only when BOTH operands are True.
True or FalseTrue when AT LEAST ONE operand is True.
not TrueReverses the operand. It uses only one operand, which is A here.
| True | and | True | True |
| True | and | False | False |
| False | and | True | False |
| False | and | False | False |
| True | or | True | True |
| True | or | False | True |
| False | or | True | True |
| False | or | False | False |
| not | True | False |
| not | False | True |
6Try it yourself
The operands of a logical operator need not be written as True and False. A comparison already gives a boolean, so a comparison can be an operand too. Try the last two below: the comparisons are solved first, and the logical operator then works on their answers.
7Recap
and gives True only when both operands are True. or gives True when at least one operand is True. not reverses its single operand. All three give a boolean value.What does print(True and False) show?
What does print(False or True) show?
Which logical operator works on only ONE operand?
What does print(not (2 > 3)) show?