NOT
What Is the NOT Operator?
- The NOT operator
notin Python is used to reverse the boolean value of a statement.
How to Use It:
The basic way to use the NOT operator is:
pythonresult = not operandoperandis the condition you want to reverse.resultwill beTrueifoperandisFalse, andFalseifoperandisTrue.
Truth Table - NOT Operator:
Here’s how the NOT operator works:
Operand Return Value True False False True
Examples and Exercises:
Using NOT with Boolean Values:
python# not True a = True c = not a print('not', a, 'is:', c) # Output: not True is: False # not False a = False c = not a print('not', a, 'is:', c) # Output: not False is: TrueUsing NOT with Non-Boolean Values:
- In Python, any non-zero value is considered
Trueand zero is consideredFalse.
python# not True a = 5 c = not a print('not', a, 'is:', c) # Output: not 5 is: False # not False a = 0 c = not a print('not', a, 'is:', c) # Output: not 0 is: True- In Python, any non-zero value is considered
Summary:
- The NOT operator
notin Python is useful for reversing the boolean value of a condition. It helps in making decisions in your code. Try the examples to see how it works!