Python IF NOT statement | Basics

Python IF NOT statements means inside if block executes only if the value(boolean) is False or if the value(collection) is not empty.

if not value:
    statement(s)

Python IF NOT example

Simple example code uses Python not logical operator in the boolean expression of Python IF.

a = False

if not a:
    print('A is', a)

Output:

Python IF NOT statement

Another example if not String in Python

Python if not expression to print the statement only if the string is not empty.

s = ''

if not s:
    print('String is empty.')
else:
    print(s)

Output: String is empty.

Where to use if not expression in Python?

Answer: Use if not expression to conditionally execute a block of statements only if the value is not empty or not False.

Do comment if you have any doubts and suggestions on this Python Python if the topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

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