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 the boolean is true Python
var = True
if var:
print('var is True')
else:
print('var is False')
Output:
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 or suggestions on this Python basic 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.