CS10 : Comparative methods


https://docs.google.com/presentation/d/1GDtSiHpQHvCt9Rrclp2XkemwbOaUt7KR63Kt_IXtKLw/preview
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.

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

CGP The Revision Guide Page 42, 48
CGP Exam Practice Workbook Page 56

# Get Ready.png

Activity 1 Decision problems    I   O   A   E 


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.


Activity 2 Special symbols  I   O   A   E 

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                  : <
GREATER THAN OR EQUAL TO   : ≥
LESS THAN OR EQUAL TO      : ≤

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               : != 
GREATER THAN OR EQUAL TO   : >=
LESS THAN OR 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.

Click to download revision cards
https://docs.google.com/document/d/1i4UMqCnALeZ3FJCe7i9Z-j6pyEd67c8-CTFG0u94jfk/export?format=pdf
Remember to print them single sided

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

https://goo.gl/forms/DxE4JqS7eJywAgDI2
Try to get 5/5!


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.