LambdaLabTM
Computer Science · Class 11 · Expressions & Operators
ExpressionsBooleans⏱️ 10 min read

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:

and

True only when BOTH operands are True.

If even one operand is False, the answer is False.

or

True when AT LEAST ONE operand is True.

The answer is False only when both operands are False.

not

Reverses 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

Python prompt — interactive mode
>>> print(True and True)
True
>>> print(True and False)
False
>>> print(False and True)
False
>>> print(False and False)
False

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

Python prompt — interactive mode
>>> print(True or False)
True
>>> print(False or True)
True
>>> print(True or True)
True
>>> print(False or False) # the only combination that gives False
False

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.

Python prompt — interactive mode
>>> print(not True)
False
>>> print(not False)
True
>>> print(not (12 > 3)) # 12 > 3 is True, so not True is False
False

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.

Switch A and B on or off
True and False
False

True only when BOTH operands are True.

True or False
True

True when AT LEAST ONE operand is True.

not True
False

Reverses the operand. It uses only one operand, which is A here.

and
TrueandTrueTrue
TrueandFalseFalse
FalseandTrueFalse
FalseandFalseFalse
or
TrueorTrueTrue
TrueorFalseTrue
FalseorTrueTrue
FalseorFalseFalse
not
notTrueFalse
notFalseTrue

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.

Python prompt — interactive mode
# Join two comparisons using and / or, or reverse one using not.
>>>
try

7Recap

Key Takeaway
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.
Quick Check

What does print(True and False) show?

Quick Check

What does print(False or True) show?

Quick Check

Which logical operator works on only ONE operand?

Quick Check

What does print(not (2 > 3)) show?