Open In App

not Operator in Python

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The not keyword in Python is a logical operator used to obtain the negation or opposite Boolean value of an operand.

  • It is a unary operator, meaning it takes only one operand and returns its complementary Boolean value.
  • For example, if False is given as an operand to not, it returns True, and vice versa

How to Use Not Operator in Python?

The not operator is simple to use. It is placed before a Boolean expression or a variable to invert its value.

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.

Python
a = True
print(not a)

Output
False

Practical Applications

The possible practical applications of the “not” keyword are: 

  1. This keyword is mostly used for altering the boolean value.
  2. It is used with an if statement. It is used to negate the condition in the if statement.
  3. The “not” keyword is also used with in keyword“. It is used with the “in” keyword when we are searching for a specific value in a collection of data.

More Examples on Not Operator

Let’s look at some examples of not operator in Python codes, each example shows different use-cases of “not” operator.

Python “not” operator with Variables

Basic example of “not” operator with variable.

Python
a = False
print(not a)

Output
True

Using the “not” Boolean Operator in Python with Specific condition

As basic property of the “not” keyword is that it is used to invert the truth value of the operand, lets explore how the “not” operator negates the final outcomes.

Here: “not” operator negates the truth value of each expression, flipping True to False and False to True, whether it’s applied to direct Boolean values ( #1 and #2), logical operations (#3 and #4), or comparison results (#5).

Python
# 1 Not with False boolean value
print(not False)
# 2 Not with true boolean value
print(not True)_
# 3 Not with result of and operation
print(not(True and False))
# 4 Not with result of or operation
print(not(True or False))
# 5 Not with result of compare operation
print(not (5 > 7))

Output
True
False
True
False
True

Using the Not Operator with different Value

Here: when using the “not” keyword with different data types, it considers non-empty values as True and empty values as False and negates them.

Python
s = "geek"
print(not s)
# Not with list boolean value
a = [1, 2, 3, 4]
print(not a)
# Not with dictionary
d = {"geek": "sam", "collage": "Mit"}
print(not d)
# Not with Empty String
es = ""
print(not es)
# Not with Empty list
el = []
print(not el)
# Not with Empty dictionary
ed = {}
print(not ed)

Output
False
False
False
True
True
True

Logical NOT operator with the list

Here in this example, we are using “not” operator with the list:

Python
a = [5, 10, 20, 59, 83]
if not a:
    print("Inputted list is Empty")
else:
    for i in a:
        if not(i % 5):
            if i not in (0, 10):
                print(i,"is not in range")
            else:
                print(i, "in range")
        else:
            print(i,"is not multiple of 5")

Output
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

“not” keyword is used in different ways here

  • first, it checks if the list “l” is empty then, inside the loop
  • it verifies whether each number is a multiple of 5
  • and finally, it checks if a number is not in the given range (0,10)

By using the not operator effectively, you can write clearer and more efficient logic in your Python programs.



Next Article
Practice Tags :

Similar Reads