Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Not Operator

check

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:

  1. Create a variable named value_1 and assign it a numeric value greater than 50.

  2. Create a variable named value_2 and assign it a numeric value less than 100.

  3. Print an expression which evaluates to True using the value_1 variable and use the not operator so that False is printed in the terminal.

  4. Print an expression which evaluates to False using the value_2 variable and use the not operator so that True is printed in the terminal.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > False
    > True
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    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!