In Python, the Boolean data type is used to represent two possible values: True
and False
. Boolean values are commonly used in conditional statements and loops to control program flow.
Boolean values can be created using the bool()
function. Any non-zero or non-empty value will be evaluated as True
, while zero or empty values will be evaluated as False
.
x = bool(0)
y = bool(1)
z = bool("hello")
print(x) # False
print(y) # True
print(z) # True
Python provides several operators for working with Boolean values:
and
: returns True
if both operands are True
, otherwise returns False
or
: returns True
if either operand is True
, otherwise returns False
not
: returns the opposite of the operand
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False