2. Basic Types: Booleans
True or False
The first built-in Python type we will discuss are the booleans and .
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 and are the only two possible instances of the type. The 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 |
Disjunction | Returns if either or or both are , otherwise . | ||
Conjunction | Returns if both and are , otherwise . | ||
Negation | Returns if is , otherwise . |
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 and 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.