Comparison operators in Python allow us to compare two values and determine if they are equal, different, greater, or lesser:
The results of comparisons are boolean values (True
or False
), and they are fundamental for control flow logic.
Operator | Name | Description |
---|---|---|
== | Equal to | Compares if two values are equal |
!= | Not equal to | Compares if two values are different |
> | Greater than | Checks if one value is greater than another |
< | Less than | Checks if one value is less than another |
>= | Greater than or equal to | Checks if one value is greater than or equal |
<= | Less than or equal to | Checks if one value is less than or equal |
If you want to learn more about Comparison Operators
check the Introduction to Programming Course read more
List of comparison operators
Equality (==)
The equality operator (==
) is used to check if two values are equal.
a = 5
b = 5
is_equal = (a == b) # True, since a and b are equal
Inequality (!=)
The inequality operator (!=
) is used to check if two values are not equal.
a = 5
b = 3
is_not_equal = (a != b) # True, since a is not equal to b
Greater than (>)
The greater than operator (>
) is used to check if one value is greater than another.
a = 10
b = 5
is_greater = (a > b) # True, since a is greater than b
Less than (<)
The less than operator (<
) is used to check if one value is less than another.
a = 3
b = 7
is_less = (a < b) # True, since a is less than b
Greater than or equal to (>=)
The greater than or equal to operator (>=
) is used to check if one value is greater than or equal to another.
a = 10
b = 10
is_greater_equal = (a >= b) # True, since a is equal to b
Less than or equal to (<=)
The less than or equal to operator (<=
) is used to check if one value is less than or equal to another.
a = 5
b = 7
is_less_equal = (a <= b) # True, since a is less than b