After completing this chapter, you will be able to:
- Understand Comparison Operators.
- Explain why comparison operators are used.
- Learn all comparison operators.
- Compare different values.
- Write programs using comparison operators.
- Understand Boolean results.
Comparison Operators are used to compare two values.
The result of every comparison is either:
- True
- False
These operators are mainly used in:
- if statements
- while loops
- Decision making
- Login validation
- Eligibility checking
Without comparison operators, programs cannot make decisions.
A Comparison (Relational) Operator compares two operands and returns a Boolean value (True or False).
Example
a = 20
b = 15
print(a > b)Output
True
Comparison operators help to:
- Compare two values.
- Make decisions.
- Control program flow.
- Validate user input.
- Check conditions.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not Equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or Equal to | a >= b |
| <= | Less than or Equal to | a <= b |
Returns True if both operands are equal.
a = 10
b = 10
print(a == b)True
Returns True if both operands are different.
a = 10
b = 20
print(a != b)True
Returns True if the left operand is greater than the right operand.
a = 25
b = 15
print(a > b)True
Returns True if the left operand is smaller than the right operand.
a = 10
b = 25
print(a < b)True
Returns True if the left operand is greater than or equal to the right operand.
marks = 75
print(marks >= 35)True
Returns True if the left operand is less than or equal to the right operand.
age = 18
print(age <= 25)True
Write a Python program to demonstrate all comparison operators.
a = 20
b = 10
print("a == b :", a == b)
print("a != b :", a != b)
print("a > b :", a > b)
print("a < b :", a < b)
print("a >= b :", a >= b)
print("a <= b :", a <= b)a == b : False
a != b : True
a > b : True
a < b : False
a >= b : True
a <= b : False
START
Create two variables
Compare using all comparison operators
Display the results
STOP
a = 20
b = 10Creates two integer variables.
Each print() statement compares the two values and displays either True or False.
Comparison operators always return a Boolean value.
Example
print(15 > 5)
print(15 == 5)Output
True
False
Comparison operators are used in:
- Login Authentication
- ATM Applications
- Banking Software
- Online Examinations
- Student Result Systems
- Age Verification
- E-Commerce Websites
- Supports decision making.
- Controls program execution.
- Produces Boolean values.
- Easy to understand and use.
| = | == |
|---|---|
| Assignment Operator | Comparison Operator |
| Assigns value | Compares values |
| Does not return True/False | Returns True or False |
❌ Incorrect
if age = 18:
print("Eligible")Output
SyntaxError
✅ Correct
if age == 18:
print("Eligible")print(10 == "10")Output
False
Reason:
One value is an integer and the other is a string.
Comparison operators compare two values and return either True or False.
Six:
- ==
- !=
-
- <
-
=
- <=
Boolean (True or False).
=assigns a value.==compares two values.
Yes.
Example
print("Apple" == "Apple")Output
True
==→ Equal To!=→ Not Equal To>→ Greater Than<→ Less Than>=→ Greater Than or Equal To<=→ Less Than or Equal To- Always returns
TrueorFalse.
In this chapter, you learned:
- What Comparison Operators are.
- Types of Comparison Operators.
- Practical examples.
- Boolean results.
- Real-world applications.
- Common mistakes.
- Interview questions.