Python Comparison Operators

Python comparison operators are one of the operator types and allow us to compare two values and return a boolean result (True or False).


Let me show you some examples.

Equal

We can check if two items are equal. Here is an example where we compare two numbers:

You can also compare two strings:

Or other items, perhaps two lists:

Not Equal

This is the same logic as the equal operator, but the other way around. Let’s compare two numbers:

Or compare two strings:

Greater Than

The greater than operator checks if a number is greater than another number. For example:

The number has to be greater than, if they are equal then the output is false:

Less Than

The less than operator is the same as the greater than operator, but the other way around. For example:

10 is less than 11 so this equals true. 10 is not less than 10 however:

Greater than or equal to

We can also use the greater than or equal to operator. For example:

5 is greater than 4 so this equals true. Another example:

5 is equal to 5 so this is true. Last one:

5 is not greater than 6 so this is false.

Less than or equal to

Or we can use the less than or equal to operator. Example:

9 is less than 10 so this is true. 9 is equal to 9 so this is also true:

9 is not lower than 8 so this is false:

Conclusion

You learned how to compare two values with the Python comparison operators:

  • Equal: Check if two items are equal.
  • Not equal: Check if two items are unequal
  • Greater than: Check if an item is greater than the other item.
  • Less than: Check if an item is smaller than the other item.
  • Greater than or equal to: Check if an item is equal or greater than the other item.
  • Less than or equal to: Check if an item is equal or smaller than the other item.

I hope you enjoyed this lesson. If you have any questions, please leave a comment.


Ask a question or start a discussion by visiting our Community Forum