Control Structures#

In this notebook, we will consider if, for and while statements.

The ‘if’ statement#

In the last section, we covered how to compare 2 objects. With that in mind, can you write code in the box below to check whether variables number1 and number2 are equal?

number1 = 10
number2 = 20/2

#Put your answer here...

If we wanted to do something ONLY when number1 is equal to number2 we would need to use an if statement, this is exactly what it sounds like, e.g. below only prints out the string “CONGRATULATIONS” when number1 divided by 2 is 5.

if number1 / 2 == 5:
    print("CONGRATULATIONS")

The code above won’t print anything unless we change the value assigned to number1, can you alter the code so that the message is printed to the screen?

Then can you alter the above code to only print out a message when number1 is equal to number2?

Often, when using an if statement, we want to run a different piece of code when it’s true, to when it’s false. In this case we can use a few other tricks. For example, we can finish it off with an else statement, or carry on using conditional statements elif.

else will run anytime the if statement (or any elif statement) is found to be false, it’s like the default case. elif stands for ‘else if’, this will run after the initial if statement. An example is shown below:

if number1 == 10:
    print("number1 = 10")
elif number1 > 5:
    print("number1 > 5")
else:
    print("number1 != 10 and < 5")

Note that if and elif will only run until something is found to be true, then it leaves the if statements and moves onto the next bit of code.

The ‘for’ statement#

The for statement is really useful when you want to do something to a lot of things and you know how many things there are. In order to best show this, we need to introduce a new data structure, known as a list.

In python, lists can contain any number of variables (of any type - e.g. numbers (ints or floats), strings (words) and any other objects (remember everything in python is an object). We can make them by in many ways, for example, we can explicitly define them:

my_first_list = [0, 1, 2, 3, 4]

Now, if we wanted to multiply each element of my_first_list by 2 we could do the following:

for item in my_first_list:
    print(item*2)

That’s okay, but what if we need to store our answers in a new array? We can do that too:

#We need to make an empty list for the results to go into
my_second_list = []
for item in my_first_list:
    #To add items to the end of a python list, we need to 'append' them to the end
    my_second_list.append(item*2)
print(my_second_list)

This does create a second list, my_second_list, but it’s not the most efficient way, especially for large lists. A much more efficient way is to use a technique called list comprehension.

my_second_list = [item * 2 for item in my_first_list]

This flattens the for loop into a single line, specifying what should be stored in the list before the for loop.

We can also start to combine for and if:

# Overwrite the my_second_list variable
my_second_list = []
for item in my_first_list:
    if item % 2 == 0:
        my_second_list.append(item*2)
print(my_second_list)

Or using list comprehension:

my_second_list = [item * 2 for item in my_first_list if item % 2 == 0]

The ‘while’ statement#

The while statement also allows you to move through a list or sequence of things, but the length of it doesn’t have to be predefined, in pseudocode:

While something is true, do something.

An example of this:

x = 0
while x < 5:
    print(x)
    x += 1

This will run forever until the conditional is satisfied, so here x must be equal or larger than 5. The final statement x += 1 increases x by 1, each time, it’s a shorthand way to write x = x + 1.

Using the knowledge you have gained so far, can you write a short piece of code that will double a variable with a starting value of 2 until it’s bigger than 10000, and print out the number of steps it takes?

#Put your code here - this may require some thought, try to use pseudocode to help you solve it

Task#

Imagine you have been given a file that contains benchmarking information (testing how fast some code runs). The file defined so that the first line and every second line after that represents the size of the system, and the lines in between contain the time it took to calculate the required property.

We have not covered how to load in a file yet, so the code below will recreate the raw data in a list called raw_data for you to use.

raw_data = [
    1,
    0.31,
    10,
    8.43,
    100,
    96.38,
    1000,
    394.45,
    10000,
    6078.06,
]

Once you’ve run the code above, your task is to take data and split it into 2 lists.

#Put your code here