What Are Comparison Operators in Python?
In Python, comparison operators are used to compare values or expressions. These operators always return a Boolean result — either True or False. Comparing values is fundamental to conditional logic, loops, and decision making in programs.
List of Python Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Comparison Examples (Input & Output Together)
Equal To (==)
Python
print(5 == 5) print(5 == 3) # Output: # True # FalseNot Equal To (!=)
Python
print(7 != 5) print(7 != 7) # Output: # True # FalseGreater Than (>)
Python
print(10 > 5) print(3 > 8) # Output: # True # FalseLess Than (<)
Python
print(2 < 7) print(9 < 4) # Output: # True # FalseGreater Than or Equal To (>=)
Python
print(6 >= 6) print(3 >= 5) # Output: # True # FalseLess Than or Equal To (<=)
Python
print(4 <= 8) print(10 <= 7) # Output: # True # False Using Comparison Operators with Variables
Comparison operators work on variables as well as literal values:
Python
a = 10 b = 15 print(a < b) print(a == b) print(a >= 10) # Output: # True # False # TrueComparison Operators in Conditional Statements
Comparison results (True or False) often control program flow using conditions:
Python
age = 18 if age >= 18: print("Eligible to vote") else: print("Not eligible") # Output: # Eligible to voteIn this example, the comparison age >= 18 evaluates to True, so the first block runs.
Important Notes
-
Comparison operators always return a Boolean value.
-
They work with numbers, strings, and other comparable types.
-
Strings are compared lexicographically (alphabetical/character order).
-
Be careful with spacing and syntax — missing or extra symbols can lead to errors.