Xtralien Scientific Python: Loops

 

When programming you will often need to run a section of code or perform a certain series of options a number of times.

In Python you can do this using different types of loops. Nominally you will use both the for and while loops, and both are explained below.

 for loops

for loops are a common type of loop in Python.

When using the for loop in Python you use the format for element in iterable. This is the only form that a for loop takes. It will cycle through each element in an interable and run the code inside the loop scope (The additional indentation after the loop).

iterable = [1, 2, 3]

for element in iterable:
    # Perform operations on the element
    print(element)

for loops exist to process data rather than perform a set number of loops, although this behaviour can be forced by using the range function, which produces a list of a set size. A simple example of this can be seen below.

for i in range(10):
    print(i)

This example will print \(10\) numbers, from \(0\) to \(9\) inclusive. While this example is not particulary useful you can use loops like this alongside the built-in enumerate function. This allows you to move through data with an index.

data = [1, 2, 3, 1, 2, 3]

for index, value in enumerate(data):
    data[index] = value * 2

This example shows the power that you gain by using for with the enumerate function. Using the above you can process and update a list of data.

while loops

While loops are slightly simpler because they are based on a condition. Every time this loop starts a cycle there is a check to ensure that the loop should run. If this check is truthy then the loop will run the code inside the while loop.

While loops are useful when monitoring systems due to their intrinsic function of inspecting a condition.

i = 0

while i < 10:
    print(i)
    i += 1

The example is similar to the example seen in the for loop section. The output will also be the same, printing the numbers \(0\) to \(9\) inclusive.

Breaking out

It is sometimes useful to change the flow of loops or break out of them entirely. to do this there are two useful keywords, continue and break. The continue keyword ends the current cycle of the loop and moves back to checking the condition, resetting the loop.

i = 0
while i < 10:
    i += 1
    if i % 2 == 0:
        continue
    
    print("i is odd")

The above example uses this behaviour to only print when numbers are odd, even though the loop will execute 10 times.

Conversely, The break keyword will stop the loop in place and then exit the loop, not returning to the start. Commonly this is used with a while loop to perform some action forever, unless a specific condition is met.

while True:
    result = test_something()
    if result == 0:
        break

This will continue running until the function test_something returns 0.

Bot of the above constructs are useful when developing an application that needs to repeat something for a number or times.