Comparison Operators
Comparison operators are used to compare two values:
| Operator | Name | Example | Try it |
|---|---|---|---|
| == | Equal | x == y | |
| != | Not equal | x != y | |
| > | Greater than | x > y | |
| < | Less than | x < y | |
| >= | Greater than or equal to | x >= y | |
| <= | Less than or equal to | x <= y |
Examples
Comparison operators return True or False based on the comparison:
Example
x = 5
y = 3
print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)
Chaining Comparison Operators
Python allows you to chain comparison operators:
Example
x = 5
print(1 < x < 10)
print(1 < x and x < 10)