Python provides Boolean
types that consist of True
and False
values. In order to process the boolean type and work with the True and False values the boolean operators are used. The boolean operators are different than the mathematical operators as the values are different. The boolean operators may also call logical operators. The boolean operators consist of and
, or
, not
.
Boolean and Operator
One of the most popular boolean operators is the and
operator. The and operator has used to two check two or more boolean variables if all of them are True or not. If at least one of them is not True the return value is False. Take a look at the following calculation table.
First Value | Second Value | Result |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Boolean AND Operator
In the following examples, we calculate boolean values by using the and operator.
t = True
f = False
print(t and t)
print(t and f)
print(f and t)
print(f and f)
True False False False
Boolean or Operator
The or
operator is used to check if one of the provided boolean values is True. If at least one of the provided boolean values if True the result is True. If all of the provided boolean values are False the result is False.
First Value | Second Value | Result |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Boolean OR Operator
t = True
f = False
print(t or t)
print(t or f)
print(f or t)
print(f or f)
True True True False
Boolean not Operator
The boolean not
operator is used to returning the reverse of the boolean value. If the boolean value is True and used with the not operator the result is False. If the boolean value is False and used with the not operator the result is True.
Value | Result |
---|---|
True | False |
False | True |
Boolean NOT Operator
t = True
f = False
print(not t)
print(not f)
False True