The Boolean Type

In this lesson, the Boolean type is presented, what its use is, what values booleans can have, and what operations can be applied to them. At the end, we also consider some style issues.
Concept
Surely, the simplest questions to answer are those that only admit two answers: yes or no (of course, questions that only have one answer are even simpler, but they are not at all interesting). For example, the answer to "Is the Earth a planet?" is yes, and the answer to "Are whales fish?" is "no" (they are mammals!). For the question "Is Anna pregnant?", we don't know what the answer is, but at any given moment it will be yes or it will be no, it cannot be "a little bit". On the other hand, a question like "What time is it?" does not have yes or no as an answer.
Likewise, statements can be true or false. For example, the statement "Apples are fruits." is true, but the statement "Paris is the capital of Andorra." is false. Moreover, statements can be combined into compound statements such as "Apples are fruits or Paris is the capital of Andorra.".
In programming, it is useful to have values that indicate whether the answer to a question is yes or no, and to be able to combine these values with logical operators. For this reason, Boolean Algebra is used
Values and Operations
In Python, logical values are represented with the bool type (for booleans). The false and true values are represented with the literals False and True. Watch out for the capital letter.
There is one unary operation (on a single boolean):
- negation, with the
notoperator.
There are also two fundamental binary operations (that is, with two operands):
- disjunction, with the
oroperator and - conjunction, with the
andoperator.
The following truth tables show, for each operation, the result of each possible combination of its operands.
Truth table for negation:
a | not a |
|---|---|
False | True |
True | False |
Truth table for disjunction:
a | b | a or b |
|---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
Truth table for conjunction:
a | b | a and b |
|---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
Thus, the disjunction of two booleans is only false if both are false, and the conjunction of two booleans is only true if both are true. And, of course, negating a boolean means choosing the other one.
You can also check whether two booleans are equal or different with the == and != operators. They are used very rarely.
Uses
The conditions of conditional and iterative statements are always boolean. For example, when you write
if temperatura <= 0: ... # it freezesthe expression temperatura <= 0 is of boolean type. Indeed, as we have already explained, relational operators (==, !=, <=, >=, < and >) return a boolean value. This is more clearly appreciated when several conditions are joined:
if temperatura <= 0 and llum == 0: ... # it freezes and it's darkMoreover, if needed we can store conditions in boolean type variables. For example, with
gela = temperatura <= 0;a new boolean variable called gela is created, with value True or False depending on whether the value of temperatura is negative or zero or strictly positive. You can verify that the type of gela is boolean by evaluating type(gela) in the interpreter. Likewise, you can create a boolean variable to store whether it is dark or not, and they can be combined with each other:
gela = temperatura <= 0
es_fosc = llum == 0
if gela and es_fosc:
...And, obviously, you can also create more boolean variables using their operators:
gela = temperatura <= 0
es_fosc = llum == 0
anar_amb_compte = gela and es_foscDe Morgan's Laws
De Morgan's laws are a pair of logical transformations that are essential in computer science. Written in Python, the first law states that not (a or b) is equivalent to (not a) and (not b). The second law says that not (a and b) is equivalent to (not a) or (not b). The proof of both laws is quite simple using truth tables.
Thus, we can see that the opposite of "it freezes and it's dark" is "it doesn't freeze or it's not dark". Beware: it is not "it doesn't freeze and it's not dark", which is a very common mistake.
Therefore, when we have a loop such as
while gela and es_fosc: ...we know that we will exit the loop when at least one of the variables gela and es_fosc is false.
Style
Usually, comparing a boolean directly with True or with False is considered bad style. For example, if trobat is a boolean, the fragment
if trobat == True: ...is better rewritten like this:
if trobat: ...Similarly,
if trobat == False: ...is worse style than
if not trobat: ...Also, it is confusing to use negated identifiers for boolean variables. For example, it is much simpler to understand the condition gela than not no_gela.
Using conditionals to initialize booleans reveals you to be a programming noob without knowledge of booleans. You should get used to not writing anything like
if temperatura <= 0: # 💩
gela = True
else:
gela = Falseand, instead, write it with good style, like this:
gela = temperatura <= 0 # 💜Finally, if you want to avoid the wrath of the programming gods, never write nonsense like fals = True 🤣.


Jordi Petit, Salvador Roura
Lliçons.jutge.org
© Universitat Politècnica de Catalunya, 2026
