Booleans
Resources
In Python, Boolean values are stored in the bool
data type. Variables of the bool
data type can only store one of two values, True
or False
. So, a Boolean value is really the simplest data value we can imagine - it is a single binary bit representing a value of True
or False
.
To create a Boolean variable in Python, we can simply assign those values to a variable in an assignment statement:
a = True
b = False
print(a)
print(type(b))
When we execute that code, we’ll see the following output:
True
<class 'bool'>
In Python, we use the keywords True
and False
to represent Boolean values. In Python, it is very important to make sure these values are capitalized, otherwise the program will not work properly. Also, since these are keywords and not strings, we don’t need to put them in quotation marks.
Converting Between Data Types
Python includes the special bool()
function, which can be used to convert any data type into a Boolean value. The bool()
function follows these rules to determine what to return:
- If the input is the value
False
, the value0
, the valueNone
, or anything with 0 length, including the empty string, it will returnFalse
. - Otherwise, for all other values it will return
True
.
Let’s look at a couple of quick examples. First, let’s try to convert the strings "True"
and "False"
to their Boolean equivalents:
print(bool("True"))
print(bool("False"))
When this code is executed, we’ll see this output:
True
True
This seems a bit strange, since the string "False"
ended up creating the Boolean value True
. However, if we look at the rules above, since the string "False"
has a length greater than 1, and it is not any of the special values listed above, it should always result in the Boolean value True
.
In this example, we can check a couple of special values using the bool()
function:
print(bool(0))
print(bool(1))
print(bool(""))
In this case, we’ll see the following output:
False
True
False
Here, we see that the value 0
, as well as the empty string ""
, both result in a value of False
. However, the value 1 is True
, since it is a non-zero value.
In practice, we won’t use the bool()
function directly very often. Instead, if we want to determine if a user inputs a True
or False
value, we can just use one of the Boolean comparators that we’ll see later in this lab.