Updated March 28, 2023
Introduction to Python NOT Operator
NOT Operator in Python is falling under the category of Logical Operators.
Logical Operators in Python are used for conditional statements which return a Boolean value that can be either True or False. Depending upon the truth value of these conditions, the program decides its flow if execution.
NOT Operator returns the Boolean value true when the operand is false, and it returns the Boolean value false when the operand is true.
In lay man’s terms, a NOT Operator can be thought of as a pessimistic individual having success and an optimistic individual having a failure.
A pessimistic individual can be thought of as a statement, and his negative thought can be thought of as the operand. However, in spite of having negative thoughts, the individual has success which can be thought of as Boolean true, which gets returned.
Examples of Python NOT Operator
Let us cite some examples of the NOT operator in Python: –
1. Directly using Boolean values
Code:
a = True
b = False
print('Result of not a is : ', not a)
Output:
Variable an initialized with Boolean value True
Variable b initialized with Boolean value False
NOT conditional operator on “a” reverses the Boolean value of a; hence the result comes out to be False
2. Using comparison operators on integers to get Boolean values
Code:
a = 2>3
b = 3==3
print('result of not a is : ', not a)
Output:
Variable an initialized with condition 2>3, which comes out to be False
Variable b initialized with condition 3==3 which comes out to be True
NOT conditional operator on “a” reverses the Boolean value of a; hence the result comes out to be True
3. Using membership operators on lists to get Boolean values
Code:
a = 2 in [3,6,8,9,10]
b = "p" in "programming"
print('result of not a is : ', not a)
Output:
Variable a is initialized with the condition that uses the membership operator to determine a Boolean result. 2 is not present in the list; hence the result if becomes False
NOT conditional operator on “a” reverses the Boolean value of a; hence the result comes out to be True
4. Using identity operators on strings to get Boolean values
Code:
a = "python" is "python"
print('result of not a is : ', not a)
Output:
Variable a is initialized with the condition that uses the identity operator to determine a Boolean result. “python” and “python“ are the same; hence the condition becomes True.
NOT conditional operator on “a” reverses the Boolean value of a; hence the result comes out to be False
5. Using Logical Operators (AND, OR) on NOT Operators
Code:
a = 2 in [3,6,8,9,10]
b = 3 == 3
print('result of not a is : ', not a and b)
Output:
above Variable a takes the Boolean value False as discussed
Variable b takes the Boolean value True as discussed above
The order of precedence of execution is not, then and. So first not a becomes True and then and with True of b gives result True
Code:
a = 2 in [3,6,8,9,10]
b = 3==3
print('result of not a is : ', not b or a)
Output:
Variable a takes the Boolean value False as discussed
Variable b takes the Boolean value True as discussed above
The order of precedence of execution is not, then or. So first not b becomes False and then and with False of a gives result False
6. Using multiple NOT Operators
Code:
a = 2 in [3,6,8,9,10]
print('result of not a is : ', not not a)
Output:
Variable a takes the Boolean value False as discussed
First, not a(False) becomes True, and then applying not on that gives the result as False
Conclusion
With this, we draw closure to this topic. In this article, we looked at many different examples and understood the working of NOT Operator in Python. It is time to get our hands dirty and start using NOT operators in your programs.
Recommended Articles
This is a guide to Python NOT Operator. Here we discuss the different examples and the working of NOT Operator in Python. You may also have a look at the following articles to learn more –