The bool Type in Python

Life is 10% what happens to us and 90% how we react to it. If you don't build your dream, someone else will hire you to help them build theirs.

The bool Type in Python

Let’s discuss the Boolean type in Python. It’s one of the simpler classes that is available for data types in Python, where there are only two instances; true and false. If you have a non-zero value like x, which is equal to 1, the bool function of x will show that that is a true value, whereas, if you have a zero value, and you use the bool function on it, then that will show a false value. This is partly so because the bool class is actually a sub class of the integer class; int. And all zero values are considered false, any non-zero value is considered true. There’s some other special values which are considered False in Python as well, like None. None meaning there’s no value at all, is considered False.

# The only two instances of the boolean class are True or False 
x = 1 
print('bool(x) =', bool(x)) 
y = 0 
print('bool(y) =',bool(y)) 
# The bool class is a subclass of 'int' 
# Zero values are considered False, non-zero True 
# Values considered False in Python 
if not None: print('None is False') 
if not False: print('False is False')

Of course False is false and any kind of zero, whether it’s an integer zero, a floating point zero, or an imaginary zero of a complex number, is also considered false. Any kind of empty sequence, like an empty string, an empty list, or an empty tuple; those empty sequences are considered false. Empty dictionaries and empty sets; those mappings are considered false. Now with the Boolean operators, we just have “or,” “and,” and “not.” With Boolean “or,” it will return the first true or the last false value. In other words, it will short circuit. If it finds a true value, it’ll immediately return that. Otherwise, it will evaluate all the values, if the first one is found to be false. And if the last one is found to be false, it’ll return that.

The Boolean operator “not” basically negates whatever was false to be true or vice-versa. So it’ll return false if its operand is True. So Not True, of course, will be returning false; not 1 will return false or something like a non-empty sequence or mapping, would return false. The Boolean not will return true if its operand is False. So not False will have it return true; not a False value, like 0, will return true; and not a False value, like an empty sequence or an empty mapping, will return true. Now you see a brief overview of the Boolean type in Python.

 # Boolean Not returns False if operand is True 
 print('Not True returns', not True) 
 print('Not 1 returns', not 1) 
 print('Not "text" returns', not "text") 
 # Boolean Not returns True if operand is False 
 print('Not False returns', not False)
 print('Not 0 returns', not 0) 
 print('Not "" returns', not "") 
 
 The output: 
 Not True returns False 
 Not 1 returns False 
 Not "text" returns False 
 Not False returns True 
 Not 0 returns True 
 Not " " returns True