Python if, elif and else Statements with Real Examples for Beginners
P5.1 Python Comparison Operators (==, !=, >, <, >=, <=)
Why. Every decision starts with a question that has a yes/no answer. Comparison operators ask those questions.
What. A comparison gives True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | equal to | 5 == 5 | True |
| != | not equal to | 5 != 3 | True |
| > | greater than | 7 > 9 | False |
| < | less than | 2 < 4 | True |
| >= | greater or equal | 5 >= 5 | True |
| <= | less or equal | 6 <= 5 | False |
How to read this table: each row is one operator with a small example and its True/False result.
How.
Ask a yes/no question
- Put the value on the left.
- Pick the operator.
- Put the limit on the right.
- Print the result to see True or False.
Worked example A — Simple comparisons.
Step 1 — What we do. A delivery in Berlin took 42 minutes. The promise was 30 minutes.
Step 2 — Type and run this code.
minutes = 42
print(minutes > 30)
print(minutes == 30)
Step 3 — Output you should see.
True
False
Step 4 — What it means. The first is True (late), the second False.
Worked example B — Compare text.
Step 1 — What we do. We check the payment method of an order.
Step 2 — Type and run this code.
method = "UPI"
print(method == "UPI")
print(method == "upi")
Step 3 — Output you should see.
True
False
Step 4 — What it means. Text comparison is case-sensitive. "UPI" and "upi" are different. Use .lower() before comparing user input.
Ravindra Bagale's Tip
= stores a value, == compares. Mixing them is the number one beginner error.
Ravindra Bagale's Tip – मराठी
= value ठेवतो, == तुलना करतो. दोघांची गल्लत ही beginners ची नंबर एक चूक आहे.
Ravindra Bagale's Tip – हिंदी
= value रखता है, == तुलना करता है. दोनों को मिलाना beginners की नंबर एक गलती है.