Introduction to Python NOT Operator
The NOT Operator is a logical operator in Python, which means that it falls within that category.
In Python, conditional statements that return a Boolean result that is either True or False are written using Logical Operators. This value may be either True or False. The program’s flow of execution is determined by the truth value of these conditions, thus it’s important to pay attention to them.
NOT When the operand is false, the operator will return the Boolean value true, and when the operand is true, the operator will return the Boolean value false.
A NOT Operator may be conceptualised as a person with a pessimistic outlook who achieves success and a someone with an optimistic outlook who falls short of their goals.
A pessimistic person may be compared to a statement, and the negative thinking that they have can be compared to the operand. Yet, despite having unfavourable ideas, the person is successful, which may be seen as a true Boolean value, which causes the result to be.
returned.
Examples of Python NOT Operator
Let me to illustrate the use of the NOT operator in Python with several examples:
–
1. Directly using Boolean values
Code:
a = True
Output:
b = False
print('Result of not a is : ', not a)
A Boolean value was used to initialise the variable a. True
The Boolean value False was assigned to the variable b as the initialization.
The use of the NOT conditional operator on the letter “a” inverts the Boolean value of the letter; hence, the answer is
False
2. Using comparison operators on integers to get Boolean values
Code:
a = 2>3
Output:
b = 3==3
print('result of not a is : ', not a)
Initialization of variable a performed using the condition 2>3, which evaluates to False.
Initialization of the variable b was performed using the condition 3==3, which turned out to be true.
The use of the NOT conditional operator on the letter “a” inverts the Boolean value of the letter; hence, the answer is
True
3. Using membership operators on lists to get Boolean values
Code:
a = 2 in [3,6,8,9,10]
Output:
b = "p" in "programming"
print('result of not a is : ', not a)
The condition that makes use of the membership operator to decide whether or not a Boolean value should be returned is what is used to initialise the variable a. Since number 2 is absent from the collection, the conclusion drawn from if is invalid.
The use of the NOT conditional operator on the letter “a” inverts the Boolean value of the letter; hence, the answer is
True
4. Using identity operators on strings to get Boolean values
Code:
a = "python" is "python"
Output:
print('result of not a is : ', not a)
The condition that employs the identity operator to get a Boolean result is what is used to set the value of the variable a during the initialization process. The terms “python” and “python” are synonymous; hence, the criterion is satisfied.
The use of the NOT conditional operator on the letter “a” inverts the Boolean value of the letter; hence, the answer is
False
5. Using Logical Operators (AND, OR) on NOT Operators
Code:
a = 2 in [3,6,8,9,10]
Output:
b = 3 == 3
print('result of not a is : ', not a and b)
as was previously said, the value of the Boolean variable an is set to False.
The Boolean value is stored in the variable b. This is correct, as was said before.
Not, then, and finally is the order of execution that takes priority. Hence, not a must first become True before and with True of b may provide a result that is True Code:
a = 2 in [3,6,8,9,10]
Output:
b = 3==3
print('result of not a is : ', not b or a)
As was previously said, the value of the Boolean variable an is set to False.
The Boolean value is stored in the variable b. This is correct, as was said before.
The sequence of events that will be carried out next is not, then, or. Hence, the consequence is that not b first becomes false, and then combined with the false value of a produces the result.
False
6. Using multiple NOT Operators
Code:
a = 2 in [3,6,8,9,10]
Output:
print('result of not a is : ', not not a)
As was previously said, the value of the Boolean variable an is set to False.
Initially, the expression not a(False) evaluates to True, and then applying not to that yields a value of False as the result. Observe that if we apply the NOT operation an odd number of times, we get the opposite of the original Boolean value, and if we apply the NOT operation an even number of times, we get the starting value itself.