Mathematics, Sciences, and Technologies

Python for Scientists 2-1: Conditions and Boolean Values

,

True/False Conditions

This chapter will mainly revolve around the different types of control flow. One of its major aspects is conditional statements, which will (or will not) execute when the stated condition(s) is/are met. As the first showcase:

a = 3
b = 4
c = 5
if a + b > c:
print("A valid triangle!")

will print out A valid triangle! as expected since the if-condition \( a + b = 3+4 = 7 > 5 = c \) is fulfilled. The part a + b > c is a logical statement that in this case evaluates to True. In fact, this kind of comparison or equality checking will lead to the so-called boolean values, which are either True or False, quite self-explanatory. A key ingredient for that is the set of comparison operators (in the above example we have used >) as noted in the table below.

OperatorMeaning
==is equal to
!=is not equal to
<, >is smaller/greater than
<=, >=is smaller than or equal to/greater than or equal to

For instance,

x = 1
y = 2
z = 3
print(x + y == z)
print(y != 2)
print(x < y)
print(y > z)
print(x - y <= 0)
print(x + 1 >= z)

will yield (you may try to guess the outputs before reading below)

True
False
True
False
True
False

This also works for floats or strings. In the case of a list/tuple, we also have the in operator, which checks if an item is contained inside:

my_color1 = "Green"
my_color2 = "Brown"
valid_colors = ["Red", "Green", "Blue", "Yellow"]
print(my_color1 in valid_colors)
print(my_color2 in valid_colors)

which gives True and False respectively.

Logic Gates: AND, OR, NOT

Often we need to check the composition of multiple conditions. Gluing the smaller conditions together are the so-called logic gates, which mainly consist of and, or, and not. They carry the usual meaning in common natural languages. To demonstrate,

print(True and True)
print(True and False)
print(False and False)
print(True or True)
print(True or False)
print(False or False)
print(not True)
print(not False)

gives

True
False
False
True
True
False
False
True

A schematic with further explanation may be found at https://www.realdigital.org/doc/e127ebfa82dbc904b5c0dac5d1adce8e. Using some actual numbers/variables as an example, we have

p = -1
q = 2
print((p + q > 0) and (p != q))
print(not (p < 0) or (p == q))

that should lead to (think about it!) True False.

Exercise

Given the following lines:

x = 0
y = 1
z = 2
print(x + y >= z)
print(x * y != y * z)
print(x < x)
print((y > 0) and (x > z))
print((x > 0) or not (y == 0))
print(not ((-y < 0) and (y == z)))
print(True or False and False)

Predict the corresponding outputs. (Beware of the last one!)

Suggested Solution
False
True
False
False
True
True
True

For the last one, we note that the precedence for logical operators is NOT > AND > OR, so the and relation is considered first.

Leave a Reply

I’m Benjamin

Welcome to my Mathematical World! Here you can find posts and tutorials related to applied topics like Linear Algebra, Calculus, Differential Equations, and Programming. Feel free to leave comments and suggestions!

Discover more from Benjamin's Maths World

Subscribe now to keep reading and get access to the full archive.

Continue reading