Skip to content

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 or suggestions on this Python basic tutorial.

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.

3 thoughts on “If not true in Python | Example code”

  1. Sir, I need your assistance with this exam question. Could you help me please?
    Suppose you are writing a program to capture the information of a student with the following details.

    Name: Nishadhi
    Age: Ten years
    Height: One point two meters
    Address: 11, Palanwatta, Pannipitiya.

    Assign the above details to following four variables with the correct data type. For example, age should be a number with no decimal points.

    #Edit the following lines

    Name=
    Age=
    Height=
    Address=

Leave a Reply

Your email address will not be published. Required fields are marked *