Comparison operators — ==, !=, - , - , - =, - =
WHAT they are
HOW the answer is built (derivation from first principles)
A comparison isn't magic — it reduces to one fundamental idea: ordering / identity of values.
Step 1 — the only primitive we truly need is < (less-than).
Why? Because every other comparison can be defined using < plus logical NOT and equality:
Why this step? If neither a < b nor b < a is true, the two values cannot be ordered apart — so they must be equal. This is how a sort algorithm, knowing only <, can still tell things are equal.
Step 2 — each operator returns a Boolean, and Python Booleans are secretly integers:
Why this matters: True + True == 2 is valid Python. So sum(x > 0 for x in data) literally counts how many items are positive — comparisons become counters.
Worked examples

Forecast-then-Verify
Active recall
What does a comparison operator always return?
True or False.Difference between = and ==?
= assigns a value; == tests equality.Why is 7 == 7.0 True?
== compares value, not type; int is widened to float.What does a < b < c expand to in Python?
(a < b) and (b < c), with b evaluated once.Why is 0.1 + 0.2 == 0.3 False?
How do you safely compare floats?
abs(x - y) < tiny_tolerance.Difference between == and is?
== = equal value; is = same object identity.What integer values do True and False equal?
Why is "Zebra" < "apple" True?
How can sum(x > 0 for x in data) count positives?
Recall Feynman: explain to a 12-year-old
Imagine a referee who only ever answers yes or no. You hand the referee two things and a question: "Is this bigger?" "Are these the same?" The referee never gives long answers — just a thumbs up (True) or thumbs down (False). Your program then does different things depending on the thumb. That's all a comparison operator is: a tiny yes/no referee. And here's a secret — thumbs up secretly counts as 1 and thumbs down as 0, so if you add up a bunch of thumbs-up you can count how many times "yes" happened!
Connections
- Boolean values and type bool — the output type of every comparison
- Logical operators — and, or, not — combine comparisons into bigger conditions
- if-elif-else statements — comparisons drive every branch
- while loops — loop condition is a comparison
- Floating point representation — explains the
==float trap - ASCII and Unicode — explains string ordering
- Identity vs equality (is vs ==) — the deeper
is/==story
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Comparison operators ek chhote referee jaise hote hain jo sirf haan ya naa bolte hain — yaani True ya False. Jab bhi tum do cheezon ke beech sawaal poochte ho ("kya yeh barabar hai?", "kya yeh bada hai?"), tab ==, !=, <, >, <=, >= ka use hota hai. Inka answer hamesha ek Boolean hota hai, aur yahi Boolean tumhare if aur while ko chalata hai.
Sabse common galti: = aur == ko mix kar dena. Yaad rakho — ek = value store karta hai (assignment), do == value check karta hai (test). "One equals sets, two equals tests." Agar tum if x = 5: likhoge to Python error de dega, jo actually achhi baat hai.
Ek mast trick: Python me True ka matlab 1 aur False ka matlab 0 hota hai. Isliye sum(s >= 50 for s in scores) seedha count kar deta hai ki kitne students pass hue — bina kisi if ke! Aur Python me chaining allowed hai: 18 <= age <= 60 bilkul maths ki tarah likh sakte ho, aur Python ise (18 <= age) and (age <= 60) me khol deta hai.
Ek warning: floats ke saath == mat karo. 0.1 + 0.2 == 0.3 actually False hota hai kyunki computer binary me exact 0.1 store nahi kar paata. Iske liye abs(a - b) < 1e-9 jaise tolerance check use karo. Yeh chhoti chhoti baatein hi tumhe ek strong programmer banati hain.