Saturday, 23 September 2017

Comparing with >, < and !=

In a previous post I showed how an if structure could make a program do one of several possible things, depending on whether an input was equal to some value. I used == throughout that post to test equality.
However, that is only one possible way of comparing two values. There are other comparisons:
  • > (greater than)
  • >= (greater than or equal to)
  • < (less than)
  • <= (less than or equal to)
  • != (not equal to)
  • in (whether a value is also an element in a list or other collection)
In each case, Python checks whether or not the comparison is True or False. Why have I capitalised these? Because they are keywords in Python, and are the two possibilities of the Boolean data type. As a data type (along with strings, lists, floats & integers), Boolean data can be assigned to a variable (and even included in lists), but more often than not it is used in decision-making in the structure of the program - typically if...elif... structures, and while loops (which I haven't covered yet but will get round to).  

A word of warning about data types: Python can and usually will throw a type error if you try to compare two different data types - this is actually the same fundamental error as when Python cannot sort lists containing different types. Python cannot compare strings with numbers, or numbers with lists etc.  Integers and floats can be compared with each other because they are both numbers.
Interestingly Boolean values can be compared to numbers, with True being treated as 1 and False being treated as 0. For example on the Python command line:
>>> print(4.777 > 4)
True
>>> print(4.777 < 4)
False
>>> print(0.5 > True)
False
>>> print(0.5 > False)
True
>>> print(0 == False)
True
>>> print(True == 1)
True
>>> print(0.5 == 'String')
False
>>> print(0.5 < 'String')
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    print(0.5 < 'String')
TypeError: '<' not supported between instances of 'float' and 'str'

>>>

Strings can be compared to each other - in this case they are compared alphabetically/ASCIIbetically rather than numerically, the same way as they are sorted if in a list. As you can see here, Python is case sensitive - upper case letters are considered different characters from their lower-case counterparts. This is also true for variable names, and can be a source of errors.
>>> print('Hello World' == 'hello world')
False
>>> print('Hello World' < 'hello world')
True
>>> print('Hello World' == 'Hello World')
True
>>>
Lists are equal to each other if they are the same length and the elements in each index are the same as in the other list - i.e. the contents of the list are identical. Whether a list is greater or less than another list is based on first the length of the list (i.e. number of elements) and then the contents of the elements.

The final comparator in the bullet-point list is in. Strictly speaking it is not a comparator but an operator, and is also found in for loops, though here the context is different. It is useful for finding if something being searched for matches one or more elements in a list. For example on the command line:
>>> x = [10, 12, 15, 17]
>>> print(10 in x)
True
>>> print(11 in x)
False
>>> y = ['This', 'is', 'a', 'list', 'of', 'strings']
>>> print ('is' in y)
True
>>> print ('Hello' in y)
False
>>>


No comments:

Post a Comment