CS10 : Comparative Methods

One more fundamental feature of computer programs is comparison. Comparing things is fundamental to making decisions about what to do next or how many times you want a particular operation to execute.

https://docs.google.com/presentation/d/1GDtSiHpQHvCt9Rrclp2XkemwbOaUt7KR63Kt_IXtKLw/preview?slide=id.g3cb6bc7fc1_0_0Learning Outcomes
Comparative Methods

We are learning ...
  • About the logical methods that computers can use to compare things
So that we can ...
  • Have practical experience of using standard comparative operations in a programming language
    - Equal to / the same as
    - Not equal to / not the same as
    - Less than
    - Less than or equal to
    - Greater than
    - Greater than or equal to

# Get Ready.png

https://docs.google.com/presentation/d/1GDtSiHpQHvCt9Rrclp2XkemwbOaUt7KR63Kt_IXtKLw/preview?slide=id.g3df8b56503_0_5Activity 1
Decision problems 

Task 1.1 Your own decision problems
Where we learn about decision problems (which have 'yes' or 'no' answers)

What do the answers to the following questions have in common?

Does the sun rise in the east?
Is a kangaroo a mammal?
Am I asleep?
Is 27 greater than 26?
Am I 46 years old?
Did I get a piece of coal for Christmas?

That's right - they all have a 'Yes' or 'No' answers. These sorts of questions are called decision problems - in computer science terms, they are decidable (that really is a word). 

In your notebook / on paper

Write down five of your own decision problems in your notebooks. Remember, decision problems always, and only have 'Yes' or 'No' answers which a computer would struggle to give.

Task 1.2 Your own decision statements
Where we learn about decision statements (which have 'true' or 'false' answers)

It's also possible to rephrase these questions into decision statements which have 'True / False' responses. Technically, decision statements do not have 'answers' - they are either 'True' or 'False' which is a standard Boolean datatype which computers are very comfortable handling.

The sun rises in the east.
A kangaroo is a mammal.
I am asleep.
27 is greater than 26.
I am 46 years old.
I got a piece of coal for Christmas.

In your notebook / on paper

Rephrase the questions you came up with in Task 1.1 as statements with either 'True' or 'False' responses. Write down the statements in your notebooks.

https://docs.google.com/presentation/d/1GDtSiHpQHvCt9Rrclp2XkemwbOaUt7KR63Kt_IXtKLw/preview?slide=id.g41ef82329c_0_12Activity 2
Special symbols

Just like the mathematical operators we met in a previous unit, there are some issues with the standard mathematical comparison operators we are used to using.

Task 2.1 Spot the operator
Where we learn about the built in comparison operators that can be typed directly at the keyboard

Look carefully at the following list of comparison operators ...

EQUAL TO                   : =
NOT EQUAL TO               : ≠
GREATER THAN               : >
LESS THAN                  : <

Like we did during a previous unit, look for the 'Not equal to', 'Greater than or equal to' and 'Less than or equal to' symbols on your computer keyboard. Can you find them?

Yes, but it means something different in Python!

Combinations of symbols are used for these comparison operators ...

NOT EQUAL TO               : != 

Hang on, the 'Equal to' symbol is also in orange! Remember that the equals sign (=) is already used for assignment and therefore, we can't use it for comparison. So ...

EQUAL TO                   : == 

In your notebook / on paper

Make a full list of the programming comparison operators (in blue) and explain the function each one of them performs.

Task 2.2 Testing them out
Where we learn how Python behaves when we give it decision statements

This task requires you to carry out some comparison operations at the prompt. There is no need to record any of the interactions with Python, but make sure you write about what you have found out.

At the prompt

Type in the following Python comparison statements, pressing the  ENTER  key after each one. Convince yourself that the comparison statements produce the expected output (the output that makes sense).

>>> 12 == 9
>>> 12 > 9
>>> 12 < 9
>>> 12 >= 9
>>> 12 <= 9
>>> 12 != 9

... and now with variables (there is no need to type the comments) ...

>>> shoes = 12     # this is an assignment and produces no output, so don't expect one!
>>> feet = 12      # this is an assignment and produces no output, so don't expect one!
>>> shoes == feet
>>> shoes != feet
>>> shoes > feet
>>> shoes < feet
>>> shoes >= feet
>>> shoes <= feet

Combining multiple decision statements with AND, OR and NOT

In your notebooks / on paper

Write about what have you found out from this task using some of the Python statements to help you with your explanation. What output do these kind of programming statements always produce?

Assessment Task (Homework)

Come up with three decision statements that evaluate to True and three which evaluate to False.

Grading rubric

MASTER :  You've produced six inventive decision statements, none of which are the same as mine.
APPRENTICE You've produced six decision statements, some of which are similar to mine.
NOVICE : You've produced six decision statements, all of which are the same as mine, really.

# Flash cards.png
Click to load key word list to help you make your own flash cards 

Hungry for more?

Try out the True Or False quiz at the Merriam-Webster dictionary website! Really, even though this is an 'I am hungry and I want to learn more' activity, I'll probably make you do it anyway cause it's nice.