If not true in Python | Example code

If not true means the ‘not operator‘ is used in the if statements in Python. The ‘not’ is a Logical operator in Python that will return True if the expression is False.

if not var

If var is True, then not will evaluate as false, otherwise, True.

Example If not true in Python

Simple Python not-operator example with if statement.

x = 10

if not x > 0:
    print("x is not greater then 0")
else:
    print("x is greater then 0")

Output: As x>0 is False, so not-operator evaluated as True.

Python example if not with ‘in’

The elements that evaluated False as using ‘not’ did not display in the console.

a_List = [5, 10, 15, 20, 25, 30]

for a in a_List:
    if not a in (10, 25):
        print("List Item: ", a)

Output:

Python example if not in

Do comment if you have any doubts and suggestions on this Python basic tutorial.

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.