Loops#

If you want code to be executed repeatedly, you can make use of loops.

See also

For loops#

For looping over a range of numbers, we can use a simple for loop and the range function.

In the following cell, the print(i) command will be executed a couple of times for different values of i. We iterate over a range of values:

for i in range(0, 5):
    print(i)

Note that the above code that is indented will only be excuted for the first given number (0) and continue until the last number (5) but not including it.

You can also loop over a range of numbers with a defined step, for example step 3:

for i in range(0, 10, 3):
    print(i)

Iterating over arrays allows you to do something with all array elements:

for animal in ["Dog", "Cat", "Mouse"]:
    print(animal)

You can iterate over two arrays in parallel, pair-wise like this:

# going through arrays pair-wise
measurement_1 = [1, 9, 7, 1, 2, 8, 9, 2, 1, 7, 8]
measurement_2 = [4, 5, 5, 7, 4, 5, 4, 6, 6, 5, 4]

for m_1, m_2 in zip(measurement_1, measurement_2):
    print("Paired measurements: " + str(m_1) + " and " + str(m_2))

If you want to know the index of the element in the list as well, use the enumerate function:

# numbering and iterating through collections
for index, animal in enumerate(["Dog", "Cat", "Mouse"]):
    print("The animal number " + str(index) + " in the list is " + animal)

Generating lists in loops#

One can generate lists using for loops. The conventional way of doing this involves multiple lines of code:

# we start with an empty list
numbers = []

# and add elements
for i in range(0, 5):
    numbers.append(i * 2)
    
print(numbers)

One can also write that shorter. The underlying concept is called generators.

numbers = [i * 2 for i in range(0, 5)]

print(numbers)

The conventional combination involving an if-statements looks like this:

# we start with an empty list
numbers = []

# and add elements
for i in range(0, 5):
    # check if the number is odd
    if i % 2:
        numbers.append(i * 2)
    
print(numbers)

And the short version like this:

numbers = [i * 2 for i in range(0, 5) if i % 2]

print(numbers)

While loops#

Another way of looping is using the while loop. It works by checking a condition, similar to the if statement. It will interrupt execution as soon as the condition is no longer true:

number = 1024

while (number > 1):
    number = number / 2
    print(number)

Interrupting loops#

You can interrupt loops at specific points in your code using the break command:

number = 1024

while (True):
    number = number / 2
    print(number)
    
    if number < 1:
        break
for i in range(10):
    print(i)
    if i > 5:
        break

Skipping iterations in loops#

If you want to skip iterations, you can use the continue statement. That often makes sense in combination with an if:

for i in range(0, 10):
    if i >= 3 and i <= 6:
        continue
    print(i)

Exercises#

Exercise 1#

Assume you have a list of filenames and you want to do something with them, for example print them out. Program a for loop which prints out all file names which end with “tif”.

file_names = ['dataset1.tif', 'dataset2.tif', 'summary.csv', 'readme.md', 'blobs.tif']

Exercise 2#

Consider the image below. The for loop shown splits it into 2 horizontal pieces. Write a second for loop inside the one already there that furhter splits each horizontal part into 2 vertical parts, ending up with 4 tiles.

from skimage.data import camera
from skimage.io import imshow
import matplotlib.pyplot as plt
cameraman_image =camera()

imshow(cameraman_image)

print(cameraman_image.shape)
tile_size = 256

for i in range(0, cameraman_image.shape[0], tile_size):

    
    tile = cameraman_image[i: i + tile_size, :]
    print('Starting row index: ' + str(i))
    print('Ending row index: ' + str(i + tile_size))

    plt.figure()
    imshow(tile)