8. Loops and conditions

In the Pipeline notebook, we learned how to package our workflow into a function. To make the pipeline work correctly we discovered that we needed:

  • a way to decide if we wanted to perform a certain action (plotting) or not using if

  • repeatedly use the same function for multiple images using for

Here we explore more in details how these two code organizing feature work.

Conditional statements

When automating operations, we sometimes want to make sure that we only do the operation in some particular case. We say “if this condition is true, then do the operation”. The way to write this in Python is with an if statement. The syntax is similar to the for loop with the if statement followed by a condition and : and then an indented (shifted to the right) block:

a = 3
if a < 5:
    print('yes')
yes

If we want to execute another operation if the condition is not met we can use an elif statement:

a = 1
if a < 2:
    print('smaller than 2')
elif a < 5:
    print('smaller than 5')
smaller than 2

Note that the if statement stops the first time a condition is verified. Finally we can use the else statement if none of the previous conditions is realized.

a = 10
if a < 2:
    print('smaller than 2')
elif a < 5:
    print('smaller than 5')
else:
    print('larger than 5')
larger than 5

Simple looping

The for statement allows us to repeatedly execute a code of block that follows this statement. for is executed for each element in a list. That element of the list can be only used for counting purposes (I want to repeat the operation 3 times, so for is applied for each element in the list [0, 1, 2] or I can actually use the elements if the list if they have a meaning.

Let’s see an example with a simple list:

mylist = [8,3,9,20,27]

Let’s imagine now that we want to calculate the square of each of these numbers. In English, one would say that “for each element in mylist, calculate the square of the element”.

Luckily the Python syntax is designed to be intuitive and the Python code that does this is:

for x in mylist:
    x**2

x just stands for the currently selected element from mylist. Note that:

  1. The for loop starts with the for statement

  2. The list used for iteration is specified, here mylist

  3. Again the for loop definition ends with :

  4. The content of the loop is indented

You can also note that when we execute the cell nothing happens. This is because no graphical output is generated fro for loops. If we want to see the actual value we have to use the print() function:

for x in mylist:
    value_squared = x**2
print(value_squared)
729

Only the last value is printed because we put the print() function outside the loop. If we want to see each value we have to indent the print() call so that it is included in the loop:

for x in mylist:
    value_squared = x**2
    print(value_squared)
64
9
81
400
729

Looping using a range

Often we don’t want to loop over the content of a list but just want to do some operation N times or for indexes from 0 to N. To do that, we can use the built-in range() function that just does this: it provides numbers within a certainrange. The function doesn’t really produce a list per se but can be used as if it were one. For example:

for x in range(8):
    print(x)
0
1
2
3
4
5
6
7

Note that as always the first index is not 0 but 8. Of course we could use these indexes to access specific parts of a list. Coming back to the previous example, we might want to calculate the square only of the three first numbers:

for x in range(3):
    value_squared = mylist[x]**2
    print(value_squared)
64
9
81

With mylist[x] we simply use the numbers generated by range() as indexes of our list.

Another type of loop available in most programming languages is the while loop. We are not going to used it today, but you should definitely learn about it.

Mixing for and if

Let’s mix both if and for logics and say now that we only want to calculate the square of numbers larger than 10 in mylist. We can integrate our if condition in the for loop:

mylist = [8,3,9,20,27]

for x in mylist:
    
    if x > 10:
    
        value_squared = x**2
        print(value_squared)
400
729

Note how we create different levels of indentation. Everything which belongs to the for loop is indented once, including the if statement. But everything that belongs to the if statement is also indented relative to it, and so ends up indented twice.