Skip to content

Python not True boolean

  • by

Not Consider the “not keyword” and True is a boolean operator in Python. The significance is that one can set flags, run a loop, and do many more things with it. Python not True works together as False.

We use not in if-statements. Sometimes, we want to flip or invert the value of a boolean variable.

Python not True

Simple example code.

value = True
print(value)

# Change True to False with not.
value = not value
print(value)

# Invert the value back to True.
value = not value
print(value)

Output:

Python not True

Check each number and print out whether it is prime or not.

num = int(input("Enter a number: "))
isPrime = not True
num_sqrt = int(num ** 0.5)

if num > 1:
    for i in range(2, num_sqrt + 1):
        if num % i == 0:
            isPrime = True
            break
    if isPrime == (not True):
        print("%d is prime" % num)
    else:
        print("%d is composite" % num)
else:
    print("%d is composite" % num)

Output:

Enter a number: 20
20 is composite

Do comment if you have any doubts or suggestions on this Python boolean 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