Comparison Operators

Sometimes we need to test two values for equality, or to see which value is greater. The symbols we use for this are called comparison operators. Pay attention to the check for equality, and clearly distinguish between this check (==) and the assignment operator (=) we saw earlier.

In Python, to check if two values are equal, use the == operator. I tried these out at the Python command prompt, results are in the right-most column.

Symbol
Function
Example

==

Equal

>>> 1==1 True

!=

Not equal

>>> 1!=1 False

>

Greater than

>>> 1>1 False

<

Less than

>>> 1<1 False

>=

Greater than or equal to

>>> 1>=1 True

<=

Less than or equal to

>>> 1<=1 True