Lesson Contents
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: