<jupyter_start><jupyter_text>LoopsIf you want code to be executed repeatedly, you can make use of loops.See also* [Python loops Tutorial](https://www.youtube.com/watch?v=4dN4Cn4u2M0)* [Python for loops](https://www.w3schools.com/python/python_for_loops.asp)  For loopsFor looping over a range of numbers, we can use a simple `for` loop and the [range](https://www.w3schools.com/python/ref_func_range.asp) function.In the following cell, the `print(i)` command will be executed a couple of times for different values of `i`. We **i**terate over a range of values:<jupyter_code>for i in range(0, 5):
    print(i)<jupyter_output>0
1
2
3
4<jupyter_text>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:<jupyter_code>for i in range(0, 10, 3):
    print(i)<jupyter_output>0
3
6
9<jupyter_text>Iterating over arrays allows you to do something with all array elements:<jupyter_code>for animal in ["Dog", "Cat", "Mouse"]:
    print(animal)<jupyter_output>Dog
Cat
Mouse<jupyter_text>You can iterate over two arrays in parallel, pair-wise like this:<jupyter_code># 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))<jupyter_output>Paired measurements: 1 and 4
Paired measurements: 9 and 5
Paired measurements: 7 and 5
Paired measurements: 1 and 7
Paired measurements: 2 and 4
Paired measurements: 8 and 5
Paired measurements: 9 and 4
Paired measurements: 2 and 6
Paired measurements: 1 and 6
Paired measurements: 7 and 5
Paired measurements: 8 and 4<jupyter_text>If you want to know the index of the element in the list as well, use the [enumerate](https://realpython.com/python-enumerate/) function:<jupyter_code># numbering and iterating through collections
for index, animal in enumerate(["Dog", "Cat", "Mouse"]):
    print("The animal number " + str(index) + " in the list is " + animal)<jupyter_output>The animal number 0 in the list is Dog
The animal number 1 in the list is Cat
The animal number 2 in the list is Mouse<jupyter_text>Generating lists in loopsOne can generate lists using for loops. The conventional way of doing this involves multiple lines of code:<jupyter_code># we start with an empty list
numbers = []

# and add elements
for i in range(0, 5):
    numbers.append(i * 2)
    
print(numbers)<jupyter_output>[0, 2, 4, 6, 8]<jupyter_text>One can also write that shorter. The underlying concept is called [generators](https://wiki.python.org/moin/Generators).<jupyter_code>numbers = [i * 2 for i in range(0, 5)]

print(numbers)<jupyter_output>[0, 2, 4, 6, 8]<jupyter_text>The conventional combination involving an if-statements looks like this:<jupyter_code># 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)<jupyter_output>[2, 6]<jupyter_text>And the short version like this:<jupyter_code>numbers = [i * 2 for i in range(0, 5) if i % 2]

print(numbers)<jupyter_output>[2, 6]<jupyter_text>While loopsAnother 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:<jupyter_code>number = 1024

while (number > 1):
    number = number / 2
    print(number)<jupyter_output>512.0
256.0
128.0
64.0
32.0
16.0
8.0
4.0
2.0
1.0<jupyter_text>Interrupting loopsYou can interrupt loops at specific points in your code using the `break` command:<jupyter_code>number = 1024

while (True):
    number = number / 2
    print(number)
    
    if number < 1:
        break;
for i in range(10):
    print(i)
    if i > 5:
        break<jupyter_output>0
1
2
3
4
5
6<jupyter_text>Skipping iterations in loopsIf you want to skip iterations, you can use the `continue` statement. That often makes sense in combination with an `if`:<jupyter_code>for i in range(0, 10):
    if i >= 3 and i <= 6:
        continue
    print(i)<jupyter_output>0
1
2
7
8
9<jupyter_text>Exercise 1Assume 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".<jupyter_code>file_names = ['dataset1.tif', 'dataset2.tif', 'summary.csv', 'readme.md', 'blobs.tif']<jupyter_output><empty_output><jupyter_text>Exercise 2Assume you have a list of circle radii. Make a table (dictionary) with two columns: radius and area.<jupyter_code>radii = [3, 15, 67, 33, 12, 8, 12, 9, 22]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09927310488058151, 'fraction_numerical': 0.03925233644859813, 'mean_word_length': 4.6, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 48, 'https://': 5, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '95708', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1621, 'n_words': 628, 'path': 'docs/02_python_basics/08_loops.ipynb', 'repo_name': 'haesleinhuepf/BioImageAnalysisNotebooks', 'size': '4815'}