Javatpoint Logo
Javatpoint Logo

"Not" Operator in Python

You can reverse the truth value of any Boolean expression or object with Python's not operator. This Python operator can be applied in boolean conditions like if-elif statements and for or while loops. It also functions in non-Boolean settings, enabling you to reverse the variables' truth values.

You can create precise negating Boolean expressions to govern the execution of your applications by efficiently using the Python not operator.

What is Boolean Logic in Python?

Boolean algebra, which exploits True and False values, was developed by George Boole. Additionally, it specifies the Boolean operations AND, OR, and NOT. Because they enable you to choose the path your programs should take, these Boolean elements and operators are useful in programming.

The Boolean, bool, data type in Python is a subclass of the Python int object.

Code

Output

True
Help on class bool in module builtins:

class bool(int)
 |  bool(x) -> bool
...

True and False, the only two possible values for this type, are built-in constants of Python and must be capitalized. Python internalizes these constants as integer numbers:

Code

Output

Data type of True:  <class 'bool'>
Data type of False:  <class 'bool'>
Is True an integer constant:  True
Is False an integer constant:  True
The integer value of True:  1
The integer value of False:  0

What is Python's not Operator?

The not operator is the logical operator that negates any boolean logic in Python. It just needs one operand because it is a unary operator. Any Python object or Boolean statement can serve as the not operator's operand. Even user-defined objects are functional. Reversing the truth value of the given operand is the work of not the operator.

We will get the False value if you apply this operator to an operand that equates to True. If we apply it to a false operand, you obtain True:

Code

Output

The value of 'not True':  False
The value of 'not Frue':  True

The not operator negates the truth value of the given operand. An operand that is true returns False. An incorrect operand yields True. The following two claims show what is called the truth table of a boolean logical operator:

Boolean value not <Boolean value>
True False
False True

Any Boolean statement or object's truth value can be negated with not. It is valuable due to this feature in the following circumstances:

  • In the scope of the if statements and the for or while loops, testing for unmet requirements.
  • Reversing an expression's truth value.
  • Determining if the given value is absent from a specific container.
  • Checking the identity of an object.

Additionally, you may not use standard Python objects like numbers, lists, tuples, dictionaries, strings, sets, user-defined objects, etc.

Code

Output

not 0: True
not 42:  False
not 0.0:  True
not 24.0:  False
not 0 + 0j:  True
not 24 + 1j:  False
not '':  True
not 'Hello':  False
not []:  True
not [1, 4, 6]:  False
not {}:  True
not {'a': 1, 'b': 2}:  False

Not negates or reverse the given operand's truth value in every example. Python employs the bool() function, which returns the values True or False based on the given object's truth value, to detect if an object is truthy or falsy.

Using the Python not Operator in the Boolean Context

Similar to the other two boolean logical operators, the not operator is particularly helpful in Boolean settings. Two statements in Python?if and else?define Boolean contexts. If statements enable conditional execution, you can choose between different actions depending on certain initial conditions.

While a certain condition is true, repetitive activities can be executed using while loops to execute conditional iteration.

What you might refer to as control flow expressions include these two forms. They aid in choosing the course of a program's execution. When using the not operator, one can choose what to do when a specific condition is not fulfilled.

if Statements

If the given condition is not satisfied, you can verify with the not operator using an if statement. You can use the not operator ahead of the relevant condition to make the required if statement, i.e., check if something didn't happen. Something True turns False and vice versa because the not operator gives the negated outcome.

An if condition with the not operator has the following syntax:

Syntax

In this case, the condition might be any valid Python object or a Boolean condition. For instance, a condition variable might contain a string, dictionary, list, set, or user-defined object.

Code

Output

The given number 5 is a prime number

In cases where we only wish to pass the condition for the composite numbers, we can use this function but with a negative conditional statement, i.e., using with the not operator:

Code

Output

The given number 8 is a composite number

Using the Not Operator with Assignment Operators

We can use the not operator with the assignment operators.

Code

Output

50 is less than or equal to 60
50 is outside the range 10 to 40

Using the Not Operator with a While Loop

The while loops are the next Boolean context with which we can employ the not operator. These loops continue to loop until a specified condition is satisfied or until you interrupt the loop using the loop control statements such as break, return, or by raising an exception. You can iterate while a specified condition is not fulfilled by using the not operator in a while loop.

Code

Output

2
3
4
5
7
11
13

Using the Python Not Operator in Non-Boolean Contexts

We can also use the Python not operator in the non-Boolean contexts because it accepts ordinary objects as an argument. In other words, you can utilize it without being inside a while loop or an if-else statement. The not operator can reverse a variable's truth value in non-Boolean contexts, possibly the most frequent application.

Code

Output

the current flag value is False
flag is False
the current flag value is True
flag is True
the current flag value is False
flag is False
the current flag value is True
flag is True

Using the Python Function-Based not Operator

The not operator in Python has an equivalent function-based version in the operator, which is not the case with the and and or operators. The operation is known as not_ (). It accepts an object as a parameter and produces the same result as the corresponding, not obj expression:

Code

Output

not_(0): True
not_(42):  False
not_(0.0):  True
not_(24.0):  False
not_(0 + 0j):  True
not_(24 + 1j):  False
not_(''):  True
not_('Hello'):  False
not_([]):  True
not_([1, 4, 6]):  False
not_({}):  True
not_({'a': 1, 'b': 2}):  False

When using Python higher-order functions like map() and filter(), it's helpful to use the not_() function rather than the not operator. Here is an illustration of how to sort a list of alphabets by appending empty strings to the end of the list using the not_() function and sorted():

Code

Output

The sorted list is:  ['c', 'd', 'h', 'e', '', '', '', '']

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA