2. Basic Types: Booleans
True or False
The first built-in Python type we will discuss are the booleans #\mathtt{\text{True}}# and #\mathtt{\text{False}}#.
In computer science, a boolean is a datatype that has one of two possible values: true or false. It is intended to represent the two truth values used in logic.
In Python, the booleans #\mathtt{\text{True}}# and #\mathtt{\text{False}}# are the only two possible instances of the #\color{#4271ae} {\mathtt{\text{bool}}}# type. The #\color{#4271ae}{\mathtt{\text{bool}}}# type supports a few logical operators that mimic basic logic. Logical operators can be formalised as functions with one or two inputs that return a boolean themselves. The input can be any valid expression that results in a boolean when evaluated.
Operator | Name | Usage | Returns |
#\mathtt{or}# |
Disjunction | #\mathtt{\text{x}}# #\mathtt{or}# #\mathtt{\text{y}}# | Returns #\mathtt{\text{True}}# if either #\mathtt{\text{x}}# or #\mathtt{\text{y}}# or both are #\mathtt{\text{True}}#, otherwise #\mathtt{\text{False}}#. |
#\mathtt{and}# | Conjunction | #\mathtt{\text{x}}# #\mathtt{and}# #\mathtt{\text{y}}# | Returns #\mathtt{\text{True}}# if both #\mathtt{\text{x}}# and #\mathtt{\text{y}}# are #\mathtt{\text{True}}#, otherwise #\mathtt{\text{False}}#. |
#\mathtt{not}# | Negation | #\mathtt{not}# #\mathtt{\text{x}}# | Returns #\mathtt{\text{True}}# if #\mathtt{\text{x}}# is #\mathtt{\text{False}}#, otherwise #\mathtt{\text{False}}#. |
You might be wondering why there aren't more such operators. The answer is actually quite simple; they are not needed. These three operators can create any other boolean function when combined. In fact, even just #\mathtt{\text{and}}# and #\mathtt{\text{not}}# suffice for functional completeness. The logical operators can be combined with booleans to form logical expressions that result in booleans:
>>> True or False
|
True
|
>>> True and True
|
True
|
>>> not False
|
True
|
Or visit omptest.org if jou are taking an OMPT exam.