Skip to main content

7.1) Comparison, Equality And Truth


Just as you do operations on objects such as numbers, strings and lists, you can do comparisons of two objects for equality, relative magnitude and so on. Here are some examples with numbers for you to try (remember type it all in, don’t copy-paste):

>>> a=3
>>> b=3
>>> a==b # Are a and b equal?
True
>>> a>4 # is a greater than 4?
False

Here we’ve introduced two comparison operators, == and >. The first tests equality; don’t forget to put two equals signs! One equals sign will assign b’s value to a, rather than compare a to b. This is a common source of bugs in many programming languages.

These comparisons return either True or False (notice the capital letter!). This type of object is called 'Boolean' and can only take True/False values. You can of course assign the value True or False to a variable:

>>> choice=True