![]() |
VOOZH | about |
The not keyword in Python is a logical operator used to obtain the negation or opposite Boolean value of an operand.
Example: Basic example of not operator with True. Here, we used "not" operator to change the true value to false which is the negation of True.
False
The possible practical applications of the "not" keyword are:
Let's look at some examples of not operator in Python codes, each example shows different use-cases of "not" operator.
Basic example of "not" operator with variable.
True
Explanation: The not operator negates the value of a, turning False into True.
This example shows various ways to use the not operator with different Boolean values and expressions.
True False True False True
Explanation: The not operator negates each Boolean expression. For example, not True becomes False and not(False and True) becomes True.
This example demonstrates the behavior of the not operator with different data types like strings, lists and dictionaries.
False False False True True True
Explanation: The not operator returns False for non-empty values (strings, lists, dictionaries) and True for empty ones. For example, not "geek" is False and not "" is True.
This example uses the not operator in a condition to check the properties of items in a list.
5 is not in range 10 in range 20 is not in range 59 is not multiple of 5 83 is not multiple of 5
Explanation: The not operator is used to check if the list a is empty or if a number is a multiple of 5. If the list is empty, it prints "Inputted list is Empty". It also checks for numbers not in the range [0, 10].
Related Articles: