Open In App

Python IF with NOT Operator

Last Updated : 26 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We can use if with logical not operator in Python. The main use of the logical not operator is that it is used to inverse the value. With the help of not operator, we can convert true value to false and vice versa. When not applied to the value so it reverses it and then the final value is evaluated by the if condition. So according to its final value the if block or else block is executed.

If not Python Syntax

The syntax for an if statement with the not logical operator is:

if not condition:

# Code

Why do we use the ‘If not’ Python Statement

The main purpose of using if with not is to conditionally execute code based on the absence of a specific value. This helps in handling such cases where certain elements are not present. Here the condition can be of any type boolean, string, list, dict, set, tuple, etc.

Basic example of Python Not

Python3




s = True
print(not s)
 
q = False
print(not q)


Output

False
True

Python if not with Boolean

In the code, If the value of the boolean is True then by the use of the not operator it becomes false so the statements inside the else block are executed. In case,when the value of b if False then by the use of not operator it becomes True and the statements inside the if block are executed.

Python3




b=False
if not b:
  print("Inside the if block")
else:
  print("Inside the else block")


Output:

 Inside the if block

Python if not in String Value

In the code it is checked , whether the string is empty or not. if the string is null then its equivalent to false so by the use of not operator it becomes true and it is printed that “String is empty”.

Python3




s=""
print(not s)
 
if not s:
  print("string is empty")
else:
  print("String is not empty")


Output:

True
string is empty

Check if a String is Empty or not in PythonPython if not in List

In the code it is checked if the list is empty or not .If the list is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed.

Python3




list1=[1, 2]
if  not list1:
  print("List is empty")
else:
  print("List is not empty")


Output:

List is not empty

Python if not in Dictionary

In the code it is checked if the dictionary is empty or not .If the dictionary is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed.

Python3




a={}
if not a:
  print("Dictionary is empty")
else:
  print("Dictionary is not empty")


Output:

Dictionary is empty

Python if not in Set

In the code it is checked if the set is empty or not .If the set is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed.

Python3




a=set()
if not a :
  print("set is empty")
else:
  print("set is not empty")


Output:

set is empty

Python if not in Tuple

In the code it is checked if the tuple is empty or not .If the tuple is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed.

Python3




a=tuple()
if not a :
  print("tuple is empty")
else:
  print("tuple is not empty")


Output:

tuple is empty

Fastest Way to Check if a Value exists in a List

In the code it is checked if element is present in the list or not . If element is present then it returns true and then by the use of not it becomes false so else block is executed . If the element is not present then it returns false so by use of not it becomes true and if block is executed.

Python3




list1=[1,2,3,4,5,6]
a=4
if not a in list1 :
  print("Element is not present in the list")
else:
  print("Element is present in the list")


Output:

Element is present in the list

Check if String Contains Substring

In the code it is checked if substring is present in the string or not . If substring is present then it returns true and then by the use of not it becomes false so else block is executed . If the substring is not present then it returns false so by use of not it becomes true and if block is executed.

Python3




str="abcdefghi"
str1="deft"
if not str1 in str :
  print("substring is not present in string")
else:
  print("substring is present in string")


Output:

substring is not present in string



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads