if not operator in python

Python

In this post, we are going to understand if not operator in python with examples that include if not list, if not dictionary, if not a tuple, and many more.

if not operator in python


The not operator is a logical operator in python that inverts the value of the boolean result. It is changed True result to False and vice-versa. The numeric zero, empty, none value is considered as False in Python. We will understand with example in this post.

Syntax

if not expression:
  statement(n)
  • When an expression is evaluated as False and not operator invert it to True and if not block statement executes.
  • When the expression is evaluated to true not-operator invert it to False else statement is executed.
  • In the case of collections when the collection is empty the if not block statement executes

How if not works in Python-Program Example


In this example, The value of num=20 is evaluated to True, The not-operator inverts it as False, So else block is executed. We have used two print statements to check expressions evaluated as true or false.

  • print(‘expression:’,num==20) is evaluted = True
  • print(‘expression with not :’,not num==20) is evaluted = False.

Let us understand with the below example how to use if not in Python with example

Program Example

#program if not in python
num= 20

print('expression:',num==20)
print('expression with not :',not num==20)

if not num==20:
 print('num is not 20')
else:
  print('num is:20 ')



Output

expression: True
expression with not : False
num is 20 

1. if not tuple in python


In this example, if value numeric zero, empty, none is considered as False in Python. This tuple is empty it so it is evaluated to False but the Not operator inverts it to True, So if the statement executes.

Program Example

#program if not tuple in python

mytuple = ()

print(bool(mytuple))
if not mytuple:
    print('mytuple is empty')
else:
    print(mytuple)

Output

false
mytuple is empty

2. if not string in Python


In this example, The string is empty it so it is evaluated to False but the Not operator inverts it to True, So if the statement executes.

Program Example

mystring = ''

print(bool(mystring))
if not mystring:
    print('mystring is empty')
else:
    print(mystring)

Output

False
mystring is empty

3. if not list in Python


In this example, The list is empty it so it is evaluated to False but the Not operator inverts it to True, So if the statement executes.

Program Example

#program if not list in python
mylist = []

print(bool(mylist ))
if not mylist:
    print('no element in list')
else:
    print(mystring)

Output

False
list is empty

4. if not dictionary in Python


In this example, The dictionary is empty it so it is evaluated to False but the Not operator inverts it to True, So if the statement executes.

Program Example

mydict = {}

print(bool(mydict))
if not mydict:
    print('mydict is empty')
else:
    print(mystring)

Output

mydict is empty

5. if not Set in Python


In this example, The Set is empty it so it is evaluated to False but the Not operator inverts it to True, So if the statement executes.

Program Example

myset = set({})

print(not myset)

print(bool(myset))
if not myset:
 print('myset is empty')
else:
  print('myset is not empty ')

Output

True
myset is empty

6. if not boolean in Python


In this example, The num is False but the Not operator inverts it to True, So if the statement executes.

Program Example

num = False

if not num:
    print('num is True:')
else:
  print('num is False: ')

Output

num is True

Summary

In this post, we have learned multiple ways to how to use if not in python with examples that include if not list, if not dictionary, if not a tuple, if not a string, if not a boolean how the if not-operator works in python