Conditions#

In this chapter we will learn how to make decisions in our code. This is done by using conditions. Conditions are based on the content of variables. The basic data type for conditions are booleans. Booleans can have two values: True or False.

Operators#

To evaluate variables we can use operators. The basic operators are: comparison operators, logical operators, membership operators.

Comparison Operators#

With those we can compare the content of two variables. This can be especially useful when we want to make decisions based on the content of a variable. The output of a comparison is a boolean.

print(1 == 1) #are the two values equal?
print(1 != 1) #are the two values not equal?
print(1 > 1) #is the first value greater than the second?
print(1 < 1) #is the first value less than the second?
print(1 >= 1) #is the first value greater or equal to the second?
print(1 <= 1) #is the first value less or equal to the second?
True
False
False
False
True
True

Logical Operators#

To combine booleans, we can use logical operators. The basic logical operators are: and, or, not. And is only True if both booleans are True. Or is True if at least one of the booleans is True. Not inverts the boolean.

a = True
b = False

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

Task: Can you fill out the following table?

a

b

a and b

a or b

not a

not b

(a and b) and (a or b)

(not (a and b)) and (a or b)

T

T

T

F

F

T

F

F

Membership Operators#

With membership operators we can check if a value is in a list or a string. The basic membership operators are: in, not in.

a = [1, 2, 3, 4, 5]
print(1 in a)
print(6 in a)
print(1 not in a)
True
False
False

IF#

On the basis of an evaluated condition (True/False), we can decide if we want to execute a block of code or not. For this we use the if statement.

Note: In Python, the block of code is defined by the indentation and not by curly braces as in C# or ended with an end statement as in MATLAB.

a = True

if a:
    print("a is True")
else:
    print("a is False")

if 7 < 6:
    print("math doesn't math anymore")
else:
    print("math still maths")
a is True
math still maths

As in MATLAB we can use the elif statement (in MATLAB: elseif) to add more conditions. Those conditions should be exclusive.

a = 4

if a < 4:
    print("a is less than 4")
elif a == 4:
    print("a is 4")
else:
    print("a is greater than 4")
a is 4

Nested Conditions#

We can also nest conditions. This means that we can put an if statement inside another if statement. This can be useful if we want to check multiple conditions.

a = 4
b = 5

if a == 4:
    if b == 5:
        print("a is 4 and b is 5")
    else:
        print("a is 4 but b is not 5")
else:
    print("a is not 4")
a is 4 and b is 5

Task: Can you write a code that checks if a number is even or odd? An even number is a number that is divisible by 2 without a remainder. Google the modulo operator in Python.

NumPy and Conditions#

NumPy also supports conditions. We can use the comparison operators to compare arrays. The output is an array of booleans.

import numpy as np

a = np.array([1, 2, 3, 4, 5, 7])

print(a == 3)
[False False  True False False False]

We can also use the logical operators to combine arrays of booleans. To combine those array we can use the following operators: &, |. Do not use and, or, not and also use parentheses around your conditions.

print((a == 3) & (a > 2)) 
print((a == 3) | (a > 2))
[False False  True False False False]
[False False  True  True  True  True]

np.where#

np.where is a function that can be used to return the indices of the elements that fulfill a condition.

print(np.where(a == 3))
print(np.where(a.reshape(2,-1) == 3))
(array([2], dtype=int64),)
(array([0], dtype=int64), array([2], dtype=int64))

np.any#

np.any is a function that can be used to check if at least one element fulfills a condition. The output is a boolean.

print(a == 3)
print(np.any(a == 3))
[False False  True False False False]
True

np.all#

np.all is a function that can be used to check if all elements fulfill a condition. The output is a boolean.

print(a == 3)
print(np.all(a == 3))
[False False  True False False False]
False

Task: Explain the following code. What does it do? What does each line do?

np.random.seed(0) #btw: this is a way to make random numbers reproducible

a = np.random.rand(5, 5) #creates a 5x5 array with random numbers between 0 and 1
print(a)
resIndices = np.where(a > 0.5)
print(resIndices)
[[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548 ]
 [0.64589411 0.43758721 0.891773   0.96366276 0.38344152]
 [0.79172504 0.52889492 0.56804456 0.92559664 0.07103606]
 [0.0871293  0.0202184  0.83261985 0.77815675 0.87001215]
 [0.97861834 0.79915856 0.46147936 0.78052918 0.11827443]]
(array([0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int64), array([0, 1, 2, 3, 0, 2, 3, 0, 1, 2, 3, 2, 3, 4, 0, 1, 3], dtype=int64))
if np.any((resIndices[0] == 0) & (resIndices[1] == 0)):
    print("There is a value greater than 0.5 in the first place.")
else:
    print("There is no value greater than 0.5 in the first place.")
There is a value greater than 0.5 in the first place.