We took the first steps in lesson 1. We programmed Python for the first time in the Google Colab environment. We looked at two different types of data: numbers and strings. Then we also stored numbers and strings in variables.
We will now move on to some important concepts of programming: comparing numbers and strings, and then running pieces of code conditionally based on these comparisons. This is very important, because often you want to decide in a computer program whether one or the other should be done.
An example: you want to program a game where the player with the most points wins. You want to let the computer decide who is the winner of the game. You will then have to compare the scores of different players. The winner is the player with the most points. How do you give the computer these instructions? Let's see how this works.
We can compare numbers with each other using different characters (operators):
is less than
is greater than
is equal to
is not equal to
is less than or equal to
is greater than or equal to
We will look at a few examples later. But the way you are going to compare numbers is with 1 number on the left and the other number on the right side of the sign. And what do you get as output? True or False. True, of course, means it is true, and False means it is not true.
a = 3 b = 4 print (a == b) print (a! = b) print (a <b) print (a> b) print (a> = b) print (a <= b)
Let's walk through this example. First of all, we store the numbers 3 and 4 in variables. This way we do not have to enter them all the time, and we cannot accidentally type the numbers incorrectly. But always read the number 3 at the place of a and the number 4 at the place of b.
We first check whether a is equal to b. This is not the case, because 3 is not equal to 4. So Python now prints False (not true).
Then we check whether a is NOT equal to b. This is true, because 3 is not equal to 4. So it is True.
This way you can go through the list and always check what the answer should be. It is an interesting exercise to have the students write this code first, and then to make up the output themselves. See if they understand the concept of equations. When they get it right, they can move on to more difficult comparisons.
Two or more comparisons
You can compare and then compare again. And again and again. You can put all kinds of comparisons together. This way you can check multiple conditions. Consider, for example, automatically making a selection of students. They cannot be older than 16 years old and they must have at least the grade > 7 for history. How can you program the computer so that you can automatically get this group? Right, do multiple comparisons!
You can “link” comparisons in two ways. You use the words for this in Python and and or.
If you link comparisons with and, then both comparisons must be true. But if you pair them with or, then it is enough that 1 of the 2 comparisons is true. Again Python always gives the output True or False, true or false.
a = 3 b = 4 c = 5 print (a <b and c <b) print (b> c and c <a) print (a <b or c <b) print (b> c or a> c)
We now first define three variables: a, b and c. Here we store three numbers, namely 3, 4 and 5. We do this again so that we can always easily reuse these numbers.
Let's look at the first double comparison: a < b and c < b. We use the word here and. So both comparisons must be True. We first check the left half: a < b, or 3 is less than 4. This is True. The right half is c < b or 5 is less than 4. This is False (not true). Since not both conditions are met, this will give the output False.
The second double equation (b> c and c < a) yields:
- b> c = False
- c < a = True
- Output: False
- (because this double comparison uses the operator and to link the comparisons).
So you can go on and let the students grapple with these tricky double comparisons. Always have them write the code themselves first and then think about what the output will be, to check whether they can also reason difficult, double equations with 3 or more numbers.
You can compare strings (text) in the same way as numbers. So you use the same operators (characters).
a = "Paris" b = "London" print (a == b) print (a! = b) print (a <b) print (a> b)
The first comparison checks whether the strings “Paris” and “London” are the same. This gives the output False.
The second equation checks the opposite. Since “Paris” and “London” are different strings, this will give the output True.
To sort strings, you can also use the less than or greater than sign. Please note that this is case sensitive!
With strings you can also use linked comparisons with and and or. This way you can also do very complex double or multiple checks.
Run code conditionally with if
Running code conditionally… that sounds tricky! What do we actually mean by that? Running code, let's get started with that. In Lesson 1, we talked about putting a hash in front of a line of code. This ensures that this code is not executed.
#3 + 5
This line of code does not produce any output. Placing the hash in front of the rule will not execute the rule.
In Python (and also in all other programming languages), you can execute code conditionally. That is, if a condition is met and not otherwise. Let's look at the following code.
a = 2 b = 3 if a <b: print ('running code block 1') if a> b: print ('running code block 2')
We first define two variables a = 2 and b = 3. We now use a so-called “if statement”. So read: if a is less than b. After the colon you start again on the next line, but indented. Indentation is automatic, but do not remove it. As soon as you are no longer indented, you are no longer within the condition. Indentation is important in Python. Other programming languages do this by, for example, putting brackets around the block of code that falls within the “if-statement”.
There are two "if statements" in the above code. However, if you run this code, you will only see the output “running code block 1”. Why don't you see the second print statement?
This is because the equation in the second if statement is not true (False). Therefore, the code in the second block is not executed (as if it has a hash sign in front of it).
You can make it even crazier with these conditional code blocks. This way you can link multiple blocks together with an if… else statement.
It actually says, if you pass the if check, then perform block 1.
If you DO NOT pass the if check, run the alternative code block 2.
Using if ... else we can write the previous example much more conveniently:
if a < b: print('running code block 1') else: print('running code block 2')
This produces the same result, but this way of writing is clearer.
Python (and many other programming languages too) offers the possibility to switch even more steps in addition to if and else. You can also put elif statements between the if and else. Read if..elif..else like this: if ... else if ... else ... So the first check is still the if statement. If this is false (False) then proceed to the equation at the next elif. Only if this is also not true (False) do you go to the else statement. As much as you want! Below is an example of an if ... elif ... else statement where the name is checked.
person = 'George' if person == 'Sammy': print('Welcome Sammy!') elif person =='George': print('Welcome George!') else: print("Welcome, what's your name?")
We first define the variable "person" = "George" here. First we check if this is equal to Sammy. That is not true. So we go to the second check. The elif comparison is true (True) and so the second block of code, under elif, is executed. The third block, under else, is no longer executed. This only happens if all if and elif comparisons turn out to be false (False).
So the output of this code is "Welcome George!".
Hopefully until the next lesson!