AppDividend
Latest Code Tutorials

Python If Not Operator with List, Tuple, String, Dict, Boolean

0

Not keyword in Python is an invert an expression, so if it returns False, it is now True. Sometimes, we want to invert or flip the value of the boolean variable, and in that case, if not is very useful. With not keyword, we change the meaning of expressions.

Python If Not Operator

Python If is a boolean expression and Not is a logical operator. You can use the logical not operator in Python If boolean expression.

Syntax

if not value:
    statement(s)

Here the value can be any Python data structures like boolean, string, list, set, dictionary, etc.

If the value is a type of boolean, then NOT acts as a negation operator. Please don’t confuse it.

If the value is False, not value would be True, and the statement(s) in if-block will execute.

If the value is True, not value would be False, and the statement(s) in if-block will not execute.

If the value is of data type string, then if-block will execute if the string is empty.

If the value is of type list, then if-block will execute if the list is empty.

Python if not operator with List

To check if the list is empty or not, use an if not operator in Python.

data = []

if not data:
    print('Python list is empty')
else:
    print(data)

Output

Python list is empty

As you can see that if not operator is used to check if the list is empty or not.

Python if not operator with Dictionary

To check if the dictionary is empty or not, use an if not operator in Python.

data = {}

if not data:
    print('Python dict is empty')
else:
    print(data)

Output

Python dict is empty

As you can see that if not operator is used to check if the empty is empty or not. We got the output that the dictionary is, in fact, is empty.

Python if not operator with Tuple

To check if the tuple is empty or not, use an if not operator in Python.

data = ()

if not data:
    print('Python tuple is empty')
else:
    print(data)

Output

Python tuple is empty

As you can see that if not operator is used to check if the tuple is empty or not. We got the output that the tuple is, in fact, is empty.

Python if not operator with Set

To check if the set is empty or not, use an if not operator in Python.

data = set({})

if not data:
    print('Python set is empty')
else:
    print(data)

Output

Python set is empty

As you can see that if not operator is used to check if the set is empty or not. We got the output that the set is, in fact, is empty.

Python if not operator with Boolean

data = False

if not data:
    print('Data is False')
else:
    print(data)

Output

Data is False

Python if not operator with String

To check if the string is empty or not, use an if not operator in Python.

data = ""

if not data:
    print('Python String is empty')
else:
    print(data)

Output

Python String is empty

As you can see that if not operator is used to check if the empty is empty or not. We got the output that the dictionary is, in fact, is empty.

That is it for this tutorial.

See also

Difference Between If Not And Is Not Operators

Python if-else statement

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.