Back

Relational operators

Q1. What do the following symbols mean?

Symbol Meaning
>  
>=  
<  
<=  
!=  
==  
=  

Q2. Predict the answers to the following expressions. The answer to each expression is either True or False. Don't use Python just yet!

Expression True or False?
 4 > 10  
 6 < 19  
 30 != 30  
 9.2 = 45  
 23 <= 23  
 100 == 100  
 32 <= 23  
 3 = 3  
 32 >= 99  
 99 >= 88  
 34 < 20  
 20 < 40  
 56 != 20  
 30 == 20  
 55 != 55  

Q3. When you have predicted as many answers as possible, compare your answers to a friend's answers. Then use the Python shell to chack each answer.

Q4. You can also compare strings using relational operators. To do understand how this works, you will need to search the Internet for an ASCII table and print it off. When Python compares two strings, it gets the ASCII code for the first letter of each string and compares them to produce the result. If they are the same, Python looks at the ASCII code of the second letters, and so on. If one string is longer than the other, Python compares one character with the ASCII character for 'None'. e.g.

'all' < 'ball' is True because the ASCII code for a (97) is less than the ASCII code for b (98)
'all' < 'All' is False because the ASCII code for a (97) is greater than the ASCII code for A (65)
'swim' > 'swam' is True because i (105) is greater than a (97).
'ant' > 'ants' is False because null (0) is less than s (115)

Evaluate the following expressions:

Expression True of False?
'fred' == 'fred'  
'Bell' >= 'Bill'  
'Rhino' != 'elephant'  
Jack != John  
'massive' > 'greater'  
'Dog' == 'Dogs'  
'smash and grab' > 'smashing'  
'Mars' < 'Mars Bar'  
Bee < Honey  
'shirt' > 'shirt and tie'  
'**#?' > '%##?'  
'' > '    '  
'$20' >= 'Twenty dollars'  
'My house' != 'Your house'  
'cat' = 'cat'  

Q5. When you have predicted as many answers as possible, compare your answers to a friend's answers. Then use the Python shell to chack each answer.

Back