Booleans represent one of two values: True or False in Python. Boolean expression is needed in if statement, for loop, comparison Operations to evaluate the outcome True or False in Python.
True False Python Examples
Simple example code.
Comparing Numbers
print(2 > 9)
print(0 == 10)
print(1 < 9)
Output:
If statement
Execute if block code based on condition in if statement, Python returns True or False.
a = 100
b = 10
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Output: b is not greater than a
Evaluate Values and Variables
You can evaluate any value and give you True or False in return.
Every value in Python is either True or False, numbers are True if they are not zero, and other values are True if they are not empty.
print(bool("Hello"))
print(bool(5))
num = 0
print(bool(num))
Output:
True
True
False
Any list, tuple, set, and dictionary is True, except empty ones.
print(bool("abc"))
print(bool(123))
print(bool(["A", "B", "C"]))
Output:
True
True
True
Do comment if you have any doubts and 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.