Skip to content

Python IF NOT statement | Basics

  • by

Python IF NOT statements mean 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 an example

A simple example code uses Python not a 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 prints 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 or suggestions on this Python Python if topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading