Python boolean if statement | Example codes

Booleans represent one of two values: True or False. Is used in if statement in Python. If it’s true execute the block of code or else skip it.

Note: don’t forget capital T and F, it is case sensitive

Example boolean if in Python

Let’s check if boolean is true Python

var = True
if var:
    print('var is True')
else:
    print('var is False')

Output:

Python boolean if statement

More boolean Python examples

my_boolean = 1
print(bool(my_boolean))

my_boolean = 0
print(bool(my_boolean))

my_boolean = 10
print(bool(my_boolean))

print("Coding" == "fun")

Output:

True
False
True
False

Do comment if you have any doubts and suggestions on this Python basic 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.