When writing programs, we often need to compare two values.
For example:
Is one number greater than another?
Are two values equal?
Is a user old enough?
Did a student score more than 40 marks?
Python provides comparison operators for exactly this purpose.
What are Comparison Operators?
Comparison operators are used to compare two values and return either True or False.
For example:
age = 20
print(age >= 18)Output:
TrueBecause 20 is greater than or equal to 18.
Python Comparison Operators
Operator | Meaning | Example | Result |
|---|---|---|---|
| Equal to |
|
|
| Not equal to |
|
|
| Greater than |
|
|
| Less than |
|
|
| Greater than or equal to |
|
|
| Less than or equal to |
|
|
Let's understand them one by one.
Equal to (==)
The == operator checks whether two values are equal.
a = 10
b = 10
print(a == b)Output:
TrueDon't confuse == with =.
x = 10 # Assigns 10 to x
x == 10 # Checks whether x is 10Not Equal (!=)
The != operator checks whether two values are different.
a = 10
b = 5
print(a != b)Output:
TrueGreater Than (>)
Checks whether the left value is greater than the right value.
age = 20
print(age > 18)Output:
TrueLess Than (<)
Checks whether the left value is smaller than the right value.
age = 16
print(age < 18)Output:
TrueGreater Than or Equal To (>=)
Checks whether a value is greater than or equal to another value.
marks = 40
print(marks >= 40)Output:
TrueThis is useful when the boundary value should also be accepted.
Less Than or Equal To (<=)
Checks whether a value is less than or equal to another value.
age = 18
print(age <= 18)Output:
TrueUsing Comparison Operators with if
Comparison operators are especially useful with conditional statements.
marks = 75
if marks >= 40:
print("You passed!")Here, Python checks whether marks is greater than or equal to 40.
Since it is, the condition becomes True.
Comparing Strings
Comparison operators can also be used with strings.
password = "python123"
if password == "python123":
print("Correct password")Output:
Correct passwordString comparisons are case-sensitive:
print("Python" == "python")Output:
FalseBecause uppercase P and lowercase p are different characters.
Comparing Multiple Conditions
You can combine comparisons using logical operators such as and and or.
age = 25
if age >= 18 and age <= 60:
print("Eligible")Both comparisons must be true because we used and.
You can also use comparison chaining in Python:
age = 25
if 18 <= age <= 60:
print("Eligible")This is a convenient Python feature and means the same thing as checking both boundaries separately.
A Practical Example
Let's check whether a student passed and received a distinction:
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Excellent!")
elif marks >= 40:
print("You passed.")
else:
print("You failed.")Here, comparison operators allow Python to make decisions based on the student's marks.
Remember
Comparison operators don't change the values. They simply compare them and give a Boolean result:
True
FalseYou'll use them constantly with if, elif, loops, and other decision-making logic in Python.