Introduction to Python Comparison Operators
Comparison operations are used to compare the values. The output can either be boolean True or False. They return True if the condition is satisfied, else they return False. They are also known as Relational Operators.
Operator | Name | Example | Description |
---|---|---|---|
== | Equal to | a == b | Returns True if a is equal to b else False. |
!= | Not Equal to | a != b | Returns True if a is not equal to b else False. |
> | Greater than | a > b | Returns True if a is greater then b else False. |
< | Less than | a < b | Returns True if a is less then b else False. |
>= | Greater than or equal to | a >= b | Returns True if a is greater then equal to b else False. |
<= | Less than or equal to | a <= b | Returns True if a is less then equal to b else False. |
Comparison Operators
Equal to operator
Equal to operator compares left-hand operand with right-hand operand and returns True if both are equal, else it returns False.
a = 2
b = 2
print(a == b)
a = 2
b = 3
print(a == b)
Not equal to operator
Not Equal to operator compares left-hand operand with right-hand operand and returns True if both are not equal, else it returns False.
a = 2
b = 3
print(a != b)
a = 2
b = 2
print(a != b)
Greater than operator
If left-hand operand is greater than right-hand operand, then greater than operator returns True, else it returns False.
a = 3
b = 2
print(a > b)
a = 6
b = 7
print(a > b)
Less than operator
If left-hand operand is less than right-hand operand, then less than operator returns True, else it returns False.
a = 2
b = 3
print(a < b)
a = 7
b = 6
print(a < b)
Greater than or equal to operator
If the left-hand operand is greater than or equal to the right-hand operand, then the greater than equal to operator returns True, else it returns False.
a = 3
b = 2
print(a >= b)
a = 3
b = 3
print(a >= b)
a = 6
b = 7
print(a >= b)
Less than or equal to operator
If the left-hand operand is less than or equal to the right-hand operand, then the less than equal to operator returns True, else it returns False.
a = 2
b = 3
print(a <= b)
a = 3
b = 3
print(a <= b)
a = 7
b = 6
print(a <= b)
Resources
References
- What are comparison operators in Python?
- Different Comparison operators in Python
- Python Comparison Operators