Python Boolean
Boolean True, False
# Example, Python boolean data type
is_holiday = True
is_holiday = False
type(is_holiday)
Truth values are represented by boolean data type. Python boolean values are True and False.
Both are built-in constant objects. They are case sensitive.
Do not write in lower case - true / false or all upper case - TRUE / FALSE. Its incorrect.
In python, bool are written as below.
variable = True variable = False
Boolean True vs 1, boolean False vs 0
From Python 3.x onwards, boolean type is subtype of integer. It contains only two objects.
Why 1 /0 should not be used as True / False in python?
- Readability (What programer think Vs What is written in code).
- Safety (What is written Vs How it is referred later Vs How it is executed).
- Instead, Use data type conversion functions as per need.
Type
class 'bool'
bool() function
Logical operators - or, and, not
There are 3 logical operators - or, and, not.
Logical operations are performed on
- boolean variables
- expression returning boolean result
# Example, say bool_x, bool_y are as below
bool_x = True
bool_y = False
Logical or Operator
Syntax:result = bool_x or bool_y
Result is True, if any one is True. Else, result is False.
Note: y part (i.e. right side of or) is not executed if x part (i.e. left side of or) is True.
# Example, boolean or
result = bool_x or bool_y
Logical and Operator
Syntax:result = bool_x and bool_y
Result is True, if both are True. Else, result is False.
Note: y part (i.e. right side of and) is not executed if x part (i.e. left side of and) is False.
# Example, boolean and
result = bool_x and bool_y
Logical not operator
Syntax:result = not bool_x
Result is inverse. If input is True, result is False. If input is False, result is True.
It is used to inverse / negate / reverse / toggle / flip / switch boolean value.
Warning: Do not mix your understanding about Boolean Operator - not with Comparison Operator - is not/is.
# Example, reverse boolean value
holiday = False
if not holiday:
print("I enjoy work.")
boolean if value check
If variable is holding boolean value, recommended and easy approach to check its value is as below.
Syntax:if bool_variable: # do stuff for True case else: # do stuff for False case
# Example, check boolean value using if
is_holiday = True
# recommended
if is_holiday:
print("I enjoy holiday.")
else:
print("I enjoy work.")
# not recommended
if is_holiday == True:
print("I enjoy holiday.")
# not recommended
if is_holiday is False:
print("I enjoy work.")
Using == True, == False for boolean
What is wrong in comparing boolean variable using == ?
According to Python PEP 8 Style Guide, it is recommended not to compare boolean values to True or False using ==
Rather ask yourself, isn't code more readable in above recommended approach?
Using is True, is False, is not True, is not False for boolean
It is recommended not to use "is" and "is not" operator for boolean value check.
Boolean expression check
If the condition is an expression (instead of simple bool_variable like above), you have to analyze all cases of its evaluation. Following questions are useful:
- Do you want complex expression checked?
- Are you trying to check if list/set is empty?
- Are you trying to check if list/set/variable defined? or Exists or not?
- Are you trying to check if any variable is None?
- OR are you trying to check if it is False?
# Example,
var_x = 1
var_y = 0.0
# Not recommended, numeric value turns 0
if var_x * var_y:
print("Do stuff for True.")
# Not recommended, empty tuple() as boolean check
my_tuple = tuple()
if my_tuple:
# What? Not sure, if this is for emptiness OR existence OR something else.
print("Do stuff for True.")
else:
print("Do stuff for False.")