1.2.7Introduction to Programming (Python)

Comparison operators — ==, !=, - , - , - =, - =

1,780 words8 min readdifficulty · medium5 backlinks

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:

a>bb<aab¬(b<a)ab¬(a<b)a==b¬(a<b)  ¬(b<a)ab¬(a==b)\begin{aligned} a > b &\equiv b < a \\ a \le b &\equiv \neg(b < a) \\ a \ge b &\equiv \neg(a < b) \\ a == b &\equiv \neg(a < b)\ \wedge\ \neg(b < a) \\ a \ne b &\equiv \neg(a == b) \end{aligned}

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:

True=1,False=0\texttt{True} = 1, \qquad \texttt{False} = 0

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

Figure — Comparison operators — ==, !=,  - ,  - ,  - =,  - =

Forecast-then-Verify


Active recall

What does a comparison operator always return?
A Boolean — 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?
Floating-point rounding; the sum is 0.30000000000000004.
How do you safely compare floats?
Check abs(x - y) < tiny_tolerance.
Difference between == and is?
== = equal value; is = same object identity.
What integer values do True and False equal?
True = 1, False = 0.
Why is "Zebra" < "apple" True?
Uppercase letters have smaller ASCII codes than lowercase.
How can sum(x > 0 for x in data) count positives?
Each comparison is 1 or 0; sum adds the 1s.

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

Concept Map

returns

defines

are part of

neither side smaller means

contrasts with

equals

enables

combine into

expands to

drives

Comparison operators

Boolean True or False

Less-than as primitive

Other operators == != > <= >=

Single = is assignment

Double == is comparison

True is 1 False is 0

Comparisons act as counters

Chained a lt b lt c

Powers if and while

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.

Test yourself — Introduction to Programming (Python)

Connections