Comparison operators test the relative sizes of two pieces of data and give either True or False as the result. The symbols for these operators are as follows:
You can use comparison operators by themselves or in combination. For example, you can test whether something is less than or equal to something else by using the operator
REMEMBER You use == (two = symbols) to test whether something is "equal to" something else. A single = symbol gives a name to a value.
You know about comparing the sizes of numbers—for example, 3 > 2. But Python can compare values of most other data types, too. (There are a few types you can't compare, such as complex numbers.) When you compare items by using these operators, the result is either True or False (except or some special data types in third-party modules). Here are a couple of examples:
• Strings are compared using alphabetical order, but all capital letters come before (are smaller than) all lowercase letters.
• Each item in a list or tuple is compared against the item with the same index number in the other list or tuple.
Also, lists and tuples are compared by length.
Was this article helpful?