Logical Operators
NOT is a logical operator that reverses a single condition. If the condition is true, NOT makes it false; if it is false, NOT makes it true. It works on exactly one condition, or on one group of conditions held together in brackets.
Because NOT flips the result, it is useful when an algorithm needs to check the opposite of a test. A ticket desk only shows the sold-out message when tickets are not left in stock. Writing that reversed test with NOT keeps the rule in one place.
Put not in front of the condition you want to reverse, and add brackets when the condition contains a relational operator, so it is clear which part is being reversed.
| Condition | Value of the condition | Value with NOT |
|---|---|---|
| tickets_left > 0 | True | False |
| tickets_left > 0 | False | True |
| speed <= 30 | True | False |
| speed <= 30 | False | True |
Every output is the opposite of the input, so NOT never produces a number: it only ever gives the Boolean value True or False.
NOT (tickets_left > 0) is true whenever no tickets remain. Reversing > by hand gives tickets_left <= 0, so both forms describe the same rule.
Where the reversal goes matters. A bike shed lends a bike only when (bike_free AND paid) is true, so a bike cannot be lent when NOT (bike_free AND paid) is true: that reversed group is true when the bike is out, when the payment is missing, or when both happen. Writing NOT bike_free AND paid would reverse only the first test and give a different rule, so put the brackets around the whole group.
The program below asks whether a locker is locked and reverses your answer with NOT, so it can decide whether to show the storage message.
locked_answer = input("Is the locker locked? Type 1 for yes or 0 for no: ")
locked = locked_answer == "1"
if not locked:
print("You can put your bag inside.")
else:
print("Find a member of staff for the key.")Output when the user types 0:
Is the locker locked? Type 1 for yes or 0 for no: 0
You can put your bag inside. Edexcel GCSE Computer Science · Logical Operators
Edexcel GCSE Computer Science · Logical Operators