Ravindra BagaleCourses & study guides मराठी Track your progress

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

  1. Put the value on the left.
  2. Pick the operator.
  3. Put the limit on the right.
  4. 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.