Python “if not” Statement Tutorial

Python provides the if not condition statement where used to compare provided objects if they are not the same. The “if not” statement is very useful to check different objects and variable types like boolean, string, list, dictionary, set, tuple and variables.

if not Statement Syntax

The “if not” statement has the following syntax. The “not” statement is used to convert the VALUE to reverse.

if not VALUE:
   STATEMENTS
...
  • VALUE is a variable, value, boolean, string, list, dictionary, set or tuple.
  • STATEMENTS are single or more lines which are executed when the “not VALUE” returns True.

if not Statement Boolean Example

Boolean values can be used with the “if not” statement. A boolean type variable or value is reversed with the “not” statement.

b = False

if not b:
   print('b is False')

if not Statement String Example

The “if not” statement can be used with string type and values and variables. An empty string is equal to the False boolean value but the “not” statement convert it to True .

name=''

if not name:
   print('Name is empty')
else:
   print('Name is ',name)

if not Statement List Example

The list is another type that can be used with the “if not” statement. The “if not” statement checks if the provided list is empty or not. If the provided list is empty the “if not” statements are executed.

l = []

if not l:
   print("List is empty")

if not Statement Set Example

The “if not” statement can be used with the Set type. The set type is used to create a set with unique items. If the set is empty the “if not” statements are executed.

s = ({})

if not s:
   print("The set is empty")

Leave a Comment