Selection Operators

When we are comparing two values like in an if statement there are a number of operators than can be used:

Screenshot 2016-08-30 at 11.50.23 AM

In the example below the print line will only be executed if the temperature value is above 25:


If we entered 25 exactly then the print line beneath it would not run because technically it is not above 25. We could use the greater than or equal to sign ( >= ) as in this case it would run the print line if you entered 25 because it is greater than or equal to 25:


You can use the less than ( < ) and less than or equal to sign ( <= ) in a similar way (and implement an elif at the same time):


Now there’s only one thing missing from this program – we need an else statement for any value that is inputted between:


Can you work out which sentence would be printed if we entered 8, 10 and 24?

The opposite of equal to (==) is not equal to (!=); for example:


Practice Task 1

Selection not working? Tick off these common errors:

Correct colon placement?
Have you indented properly?
Is elif and else in line with if?
Double equals sign?
Speech marks for a string?
Is your operator correct?

< Back to Section Four