Boolean values True
and False
are essential to the programming language. The not
function is an essential operator that helps us use and manipulate Boolean values. In Python, the not
function is used to reverse a Boolean value. A Boolean value is reversed when True
becomes False
and False
becomes True
.
This tutorial will explore the not
keyword and its syntax with multiple examples to explain how we can reverse Boolean values. We will also illustrate the difference between the not
keyword and the !
symbol for reversing Boolean values in Python.
Syntax
The not
keyword is straightforward to use as it has only one syntax. We place the not
keyword in front of a Boolean value.
not True not False
Both lines above represent reverse Boolean values. When Python evaluates them, the first returns False
, and the second one returns True
.
Examples
Let’s take a look at some examples of using the not
keyword to reverse a Boolean value.
Example 1: Reversing a Boolean Value
In this example, we are reversing a Boolean value True
using the not
function.
x = True y = not x print(x) print(y)
Output:
True False
Here, we declared x as True, then we declared y as the opposite of x which is False, thanks to the not
keyword.
Example 2: Reversing an Object
In this example, we can apply the not
keyword to objects that achieve Boolean functionality, such as strings, lists, or numbers.
x = "Hello World!" y = not x print(x) print(y)
Output:
Hello World! False
Here, we assigned x as a string object “Hello World!” and y reversed x, returning False
.
Example 3: Using the !
Symbol to Reverse a Boolean Value
Although the not
keyword is the preferred method for reversing Boolean values, Python supports using the !
symbol as well.
x = True y = not x z = !x print(x) print(y) print(z)
Output:
True False True
In this example, we used both the not
keyword and the !
symbol together. We set x to True
, reversed its value with not
, and stored the reverse value False
in variable y. Then we reversed x’s value with the !
symbol and stored the reverse value True
in variable z.
Example 4: Using not
with in
Keyword
The in
keyword returns a Boolean value that indicates whether a specified element is present in an object or not. In this example, we’ll see how to use the not
keyword with the in
keyword to get the reverse value of the Boolean outcome.
my_list = [1, 2, 3, 4, 5] print(3 in my_list) # True print(6 in my_list) # False print(not 3 in my_list) # False print(not 6 in my_list) # True
Output:
True False False True
As you can see, the not
keyword is used to reverse the Boolean values returned when using the in
keyword. When we used not
with 3, it returned false, but when applied to 6, it returned true as it is not in my_list
.
Example 5: Using the not
keyword with the and
Keyword
The not
keyword can be combined with other operators such as the and
keyword in Python.
x = 5 y = "Hello" print(not x == 5 and y == "Hello") # False print(not x == 4 and y == "Hello") # True
Output:
False True
As you can see, the not
keyword is applied to both the conditions to reverse their Boolean outcomes. When both conditions were met in the first line, the output was False
. When only the second condition was met in the second line, the output was True
.
Difference between not
Keyword and !
Symbol
The not
keyword and !
symbol are two different ways of achieving the same thing. The not
keyword is an operator in Python, while the !
symbol was introduced in Python 2. When using the !
symbol in Python 2, it is necessary to use parentheses around the Boolean expression to get the opposite value:
x = True y = not(x) print(x) print(y)
Output:
True False
However, in Python 3, the !
symbol can no longer be used to reverse Boolean values, and using it will generate a syntax error.
Conclusion
The not
keyword is used to reverse Boolean values in Python. This essential operator is used to manipulate Boolean values, making programming easier by providing a way to reverse conditions quickly. In this tutorial, we learned all about using the not
keyword together with other functions to achieve reverse Boolean outcomes.