Logical Operators: not
The third logical operator, not, is called negation. It turns a boolean into its opposite: not True is False and not False is True:
is_raining = False
if not is_raining:
print("No umbrella needed")This prints:
No umbrella neededis_raining is False, so not is_raining is True and the block runs. Read the condition as “if it is not raining”. You could also write is_raining == False. It means the same, but not is shorter and reads better.
Negation comes in handy when you combine conditions. “The shop is open and it isn't a holiday” is is_open and not is_holiday. not applies only to the part right after it, up to the next and or or: here, just to is_holiday.
Instructions
- 1
Write an
ifstatement that printsI will go outsideif the internet doesn't work. Usenot. - 2
Going outside is only fun when it isn't raining. Extend the condition with
andso that it's true only if the internet doesn't work and it isn't raining (not is_raining). Then add anelsebranch that printsI will read a book.