TL ; DR:
The logical
notoperator is used to check the opposite of a condition. For example:is_hungry = True if not is_hungry: print("I can wait a little longer") else: print("I need food!")
Full lesson:
The logical not operator is used to invert the value of a boolean condition. For example:
7 <= 10 # Evaluates to True
not 7 <= 10 # Evaluates to False
12 != 12 # Evaluates to False
not 12 != 12 # Evaluates to True
!True # Evaluates to False
It is oftenly used with boolean variables:
hungry = True
if not hungry:
print("I can wait a little longer")
else:
print("I need food!")
not hungry evaluates to False, so we enter the else and print "I need food!".
Assignment
Follow the Coding Tutorial and play with the not operator.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
