Python/Tutorial/Basic Types and Operators/Comparator Operators

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Math Comparators

These operators compare one numeric value with another. They are:

  • == (equality)
  • > (greater than)
  • >= (greater than or equal)
  • < (less than)
  • <= (less than or equal)
  •  != (not equal)

If the comparison is true, then the expression evaluates to True. Otherwise, it is False.

Note that for strings, it compares the two strings in alphabetical order.

For lists and tuples, it compares the list/tuple size and contents. Note that lists will never be equivalent to tuples.

For dicts, it checks the size and each key-value pair, as best it can.

Identity Comparison

  • is
  • is not

This tests to see if the object on the left and the object on the right are actually the same thing.

Containment Comparison

These test whether the value is contained inside the container.

  • x in y
  • x not in y

For lists, sets, and tuples, the container is obvious. For dicts, it only checks the keys. For strings, it checks to see if the left hand side is a substring of the right hand side.