
Syntax: Not Operator
Flipping a true value to false and vice-versa with the Python "not" operator.
The Boolean "NOT" Operator in Python
There are times when you will want to flip a boolean value from true to false or from false to true. In order to do that in Python, you can use the Python not operator.
Note that when putting a not operator in front of True that the output will be false:
print(not True)
> False
You will also notice that the same thing can be done with using a not operator in front of a False value:
print(not False)
> True
This will also work with an expression that results in a boolean value:
value = 5
print(not value < 6)
> False
You will see above that even though the expression value < 6 is true, False is printed because of the not operator being used.
Practice: NOT Operations
Based on the information above. Complete the following steps:
-
Create a variable named value_1 and assign it a numeric value greater than 50.
-
Create a variable named value_2 and assign it a numeric value less than 100.
-
Print an expression which evaluates to True using the value_1 variable and use the
notoperator so thatFalseis printed in the terminal. -
Print an expression which evaluates to False using the value_2 variable and use the
notoperator so thatTrueis printed in the terminal.HINT*: Click here for a hint
> False
> True
SOLUTION*: Click here for an example solution
value_1 = 65 value_2 = 85 print(not value_1 > 50) print(not value_2 > 100)
When the above is completed and you are getting the proper output, you are ready to move onto the next exercise!