Control Flow & Complex data types

Week 2

Control Flow

Here we will learn above several ways to specify the flow of information as your code gets executed. We will learn about loops, which are constructs that allow you to repeat blocks of code multiple times, typically while changing the values of variables inside the repeating block. We will learn about conditionals, which allow you to execute different branches of code depending on the values of variables.

Readings

Loops

Loops are used when you have a chunk of code that you need to repeat over and over again, each time changing one (or more) parameters.

There are two kinds of loops: a for loop and a while loop. A for loop is used when you (or your code) know in advance of starting the loop, how many iterations to run through. A while loop is used when the number of iterations is not known in advance of starting the loop. You might use a for loop to load in a list of data files. You might use a while loop to iterate through an EEG waveform over samples (time) to search for the first value that exceeds some baseline threshold.

Here is a simple example for the purposes of demonstration. Let’s say you want to load data from 5 files, named data1.txt, data2.txt, …, data5.txt. Let’s say each file contains a one-dimensional array of 10 values. Let’s say you want to take the average of each data file and then report the overall mean and overall variance of those values. Here’s one way to do it:

For loops

d1 = load("data1.txt")
d1m = mean(d1)
d2 = load("data2.txt")
d2m = mean(d2)
d3 = load("data3.txt")
d3m = mean(d3)
d4 = load("data4.txt")
d4m = mean(d4)
d5 = load("data5.txt")
d5m = mean(d5)

# report overall mean and overall variance of 5 data file means
alldata = [d1m, d2m, d3m, d4m, d5m]
datamean = mean(alldata)
datavar = var(alldata)
print(f"mean={datamean:.3f} and variance={datavar:.3f}")

You can see that there is a lot of repetition in this code. What if we had to load data from 1000 data files? There would be a lot of copying and pasting of code chunks. This is error prone and inefficient. Instead let’s use a for loop. A for loop allows you to repeat a block of code some predetermined number of times, and includes a counter so that you know which iteration of the loop is currently running. Here is what the code above would look like if we used a for loop:

nfiles = 1000
alldata = np.zeros(1000)
for i in range(nfiles):
    d = load("data" + str(i) + ".txt")
    alldata[i] = mean(d)
datamean = mean(alldata)
datavar = var(alldata)
print(f"mean={datamean:.3f} and variance={datavar:.3f}")

Now all we would need to change if we have 1000 data files (or one million) is the value of our variable nfiles=1000 or nfiles=1e6—nothing else in the code would have to change. This makes our code much more resilient against programming errors.

You can see a for loop begins with the keyword for followed by a name of a variable (your choice) that will keep track of which iteration of the loop is currently running. Then for word in followed by a list of values to be iterated through. Next is the block of code to be repeated. Note that in Python, blocks of code like this are denoted using indentation. Python is very fussy indeed about indentation.

for i in range(1,15,3):
    print(f"i={i}")
i=1
i=4
i=7
i=10
i=13

For loops are executed in a serial fashion, one repetition after another.

While loops

There is a second sort of loop called a while loop. This kind of loop is typically used when the number of iterations is not known in advance. A while loop keeps repeating until the value of a logical expression changes from TRUE to FALSE (changes from 1 to 0). As a little demo, here is an example of a while loop that prints out successive integers starting from 1, until they exceed a critical value, in this case 10:

i = 1
while (i < 9):
    print(f"i={i}")
    i = i + 1
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8

The expression that determines whether a while loop will continue repeating can be any valid Python expression that evaluates to a boolean (true or false).

For both for loops and while loops, there are two keywords to know about that can break you out of a loop (break) and can move you to the next iteration of the loop (continue). I tend not to use these, but some people find them useful.

Conditionals

So far we have seen how to use loops to repeat sections of code over and over again as needed. The other major control flow mechanism in high-level languages is the conditional, which allows you to specify how code branches in one direction or another depending on some logical condition.

So for example let’s say you ask the user to enter a number, and if the number is even you output “The number is even.” and if the number is odd you output “The number is odd.”:

x = int(input("Enter a number: "))
if ((x % 2) == 0):
    print("The number is even")
else:
    print("The number is odd")

which produces:

Enter a number: 15
The number is odd

and

Enter a number: 12
The number is even

In general one can string together any number of elif branches (including none of them). For example:

x = int(input("Enter a number: "))
if (x < 10):
    print("The number is less than 10")
elif (x < 20):
    print("The number is less than 20")
elif (x < 30):
    print("The number is less than 30")
else:
    print("I don't have anything to say")

You don’t need any else statements at all if is suits your needs:

x = int(input("Enter a number: "))
if (x < 0):
    print("That is a negative number")

Practice Problems

In-class demo notebook