if not python

“not” is the logical operator that prints revert output to the input. It prints output when the condition is False and vice versa. Use if condition with not operator in python when the condition is …

if not python

“not” is the logical operator that prints revert output to the input. It prints output when the condition is False and vice versa. Use if condition with not operator in python when the condition is false.

Use the “not” logical operator in python with an if statement that returns output when defined conditions are empty. It returns the output if and if only when the condition is false or when the condition is NONE or empty. This means the condition may help to flip or revert the statements. 

That’s not always the case, there are subtle differences: “if not” will execute when it is any kind of zero or an empty container, or False. Many people do use “not equal” where they do mean the condition is None.

So, if the condition is [ ] empty then it will execute. There are some examples of empty sets, empty tuples, empty dictionaries, empty strings, etc.

Note: “If not” condition is same works as “==” condition as print output whenever we know the condition true for equal statement.

a = False
if not a: 
  print ("a is False") 
# IS SAME AS:
if a == False:
  print ("a is False")

Difference between “! =” and if not operator in Python

‘If not’ checks for truth and ‘! =’ checks for equality.

“if not” returns no value when the statement value is true and a return output when the statement value is false. As the below syntax shows “a” will execute output when its input is empty or false and does not display output when input is true. 

Syntax

a = False
if not a:
  #Does this
a = True
if not a:
  #Doesn't do this

How do you write the “if not” condition in Python?

There is an example of Odd and even program that will help you to understand how to write the “if not” condition in python. 

  • This condition “(number % 2) == 0”  in below code is use for finding the even numbers
  • The condition says “if” entered number is “not” even print “number is odd”.
number = int (input ("Enter a number: "))  
if not (number % 2) ==0:
    print ('number is odd'.format(number))
else:
    print ('number is even'.format(number))

Output

Enter a number: 7
number is odd

Another example of “if not python”. It prints condition false when the condition is true, and the compiler evaluated it as false. 

  • When there is no value of “a”, it will be false. 
  • when “a” contains a value and it is not empty, so it is true.
a= 5;
if not a:
    print ("a value does not exist")
else:
    print ("a value is: " +str(a))
#Output
# a value is: 5 

Python if not: Boolean

This is the simplest example of using Boolean that contains only two conditions either it is True, or it is False. This may help you to understand the basic concept of if not in python.

b = False
if not a:
  print ("b is False")
else:
 print ('b is true')
#Output
#b is False

If not in list python

the list is an organized datatype that we are using to print an empty list with the if not condition in python.

list1 = []
if not list1:
    print ('List is empty')
else:
    print ("list contains values:" +str(list1))
#Output
#List is empty

Dictionary is an organized data type of keys with their values in python. In this example, we are going to print an empty dictionary with python if not conditions.

d = dict ({})
if not d:
    print ('Dictionary is blank')
else:
    print ('Dictionary is not empty ')
#Output
#Dictionary is blank

If not in tuple python

A tuple is an organized data type in python. We are applying if not in python condition to the tuple data type.

t = tuple ()
if not t:
    print ('Tuple is empty')
else:
    print(t)
 # Output
 # Tuple is empty

If not in set python

The set used a unique data type and does not repeat elements as defined in the “list of lists in python”. In this example, we are going to print an empty set with python if not conditions.

s = set ({})

if not s:
    print ('Set is empty')
else:
    print(s)
#Output 
#Set is empty

If not in string python

String defines the character and if it is empty means, it does not contain any data characters. In this example, we are printing an empty string with the help of python if not condition.

string1 = ''
if not string1:
    print ('String is empty')
else:
    print(string1)
#Output
#String is empty

Conclusion

There are several methods to use multiple conditions with sure output that will perform simple conditions.  But use “if not” in python is when you are sure about the output is false and empty and it works better than “not equal” in python.

Suggested Read: Remove Brackets from list Python

Leave a Comment