Python NOT Operator
Python NOT
To perform logical NOT operation in Python, you can use the not keyword prior to the boolean value or boolean operand.
Syntax of not keyword
The syntax to use not operator is:
not operandnot operator takes only one operand. It returns True if the operand is False and returns False if the operand is True.
Truth Table - NOT Operator
Following is the truth table of not operator with boolean values.
| Operand | Return Value |
| True | False |
| False | True |
Examples
1. Python NOT with boolean values
In the following example, we will use the not keyword to perform logical not operation on True and False boolean values.
Python Program
#not True
a = True
c = not a
print('not',a,'is:',c)
#not False
a = False
c = not a
print('not',a,'is:',c)Explanation of the Code:
- First Case (not True): The result is
FalsebecauseTruebecomesFalsewhen the not operator is applied. - Second Case (not False): The result is
TruebecauseFalsebecomesTruewhen the not operator is applied.
Output
not True is: False
not False is: True2. NOT Operator with non-boolean operands
You can use non-zero values instead of True and zero for False. The not operator converts non-zero values into False and zero into True.
Python Program
#not 5
a = 5
c = not a
print('not',a,'is:',c)
#not 0
a = 0
c = not a
print('not',a,'is:',c)Explanation of the Code:
- First Case (not 5): The result is
Falsebecause the operand is a non-zero integer, which is consideredTrue, so not returnsFalse. - Second Case (not 0): The result is
Truebecause the operand is0, which is consideredFalse, so not returnsTrue.
Output
not 5 is: False
not 0 is: TrueSummary
In this tutorial, we learned how to use the Python not logical operator with boolean and non-boolean operands. The not operator inverts the truth value of its operand, returning True for False operands and False for True operands.