1.2.7 · HinglishIntroduction to Programming (Python)

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

1,714 words8 min readRead in English

1.2.7 · Coding › Introduction to Programming (Python)


YEH HAIN KYA


JAWAB KAISE BANTA HAI (first principles se derivation)

Comparison koi jaadu nahi hai — yeh ek fundamental idea tak reduce hoti hai: values ki ordering / identity.

Step 1 — humein asli mein sirf ek primitive chahiye: < (less-than). Kyun? Kyunki har doosri comparison ko < plus logical NOT aur equality se define kiya ja sakta hai:

Yeh step kyun? Agar na a < b true hai na b < a, toh dono values ko alag order nahi kiya ja sakta — matlab woh equal hone chahiye. Isi tarah ek sort algorithm, sirf < jaante hue bhi, bata sakti hai ki cheezein equal hain.

Step 2 — har operator ek Boolean return karta hai, aur Python Booleans secretly integers hain:

Yeh kyun matter karta hai: True + True == 2 valid Python hai. Toh sum(x > 0 for x in data) literally count karta hai ki kitne items positive hain — comparisons counters ban jaati hain.


Worked examples

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

Forecast-then-Verify


Active recall

Ek comparison operator hamesha kya return karta hai?
Ek Boolean — True ya False.
= aur == mein kya fark hai?
= ek value assign karta hai; == equality test karta hai.
7 == 7.0 True kyun hai?
== value compare karta hai, type nahi; int ko float mein widen kiya jaata hai.
Python mein a < b < c kya expand hota hai?
(a < b) and (b < c), jisme b ek baar evaluate hota hai.
0.1 + 0.2 == 0.3 False kyun hai?
Floating-point rounding ki wajah se; sum 0.30000000000000004 ban jaata hai.
Floats ko safely kaise compare karte hain?
abs(x - y) < tiny_tolerance check karo.
== aur is mein kya fark hai?
== = equal value; is = same object identity.
True aur False kis integer value ke barabar hain?
True = 1, False = 0.
"Zebra" < "apple" True kyun hai?
Uppercase letters ke ASCII codes lowercase se chote hote hain.
sum(x > 0 for x in data) positives kaise count karta hai?
Har comparison 1 ya 0 hai; sum 1s ko jodhta hai.

Recall Feynman: 12-saal ke bachche ko samjhao

Ek referee imagine karo jo sirf haan ya naa mein hi jawab deta hai. Tum referee ko do cheezein aur ek sawaal dete ho: "Kya yeh bada hai?" "Kya yeh same hain?" Referee kabhi lamba jawab nahi deta — bas ek thumbs up (True) ya thumbs down (False). Tumhara program phir thumb ke hisaab se alag-alag kaam karta hai. Bas itna hi ek comparison operator hai: ek chota sa haan/naa referee. Aur yahan ek raaz hai — thumbs up secretly 1 count hota hai aur thumbs down 0, toh agar tum bahut saare thumbs-up ko jodhao tum count kar sakte ho ki kitni baar "haan" hua!


Connections

  • Boolean values and type bool — har comparison ka output type
  • Logical operators — and, or, not — comparisons ko bade conditions mein combine karo
  • if-elif-else statements — comparisons har branch chalate hain
  • while loops — loop condition ek comparison hai
  • Floating point representation== float trap explain karta hai
  • ASCII and Unicode — string ordering explain karta hai
  • Identity vs equality (is vs ==)is/== ki gehri kahani

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