Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In computer programming, comparison operators (also known as relational operators) test relationships between two operands. Every comparison in Python evaluates to a strict boolean result: either True or False.
These operators are the primary decision-makers in conditional logic (if, elif, else) and loop guards (while). Unlike languages like C, C++, or JavaScript, Python natively supports mathematical chained comparisons (e.g., 18 <= age < 60), allowing you to write clean, algebraic range checks without messy logical and operators.
+-------------------------------------------------------------------------+
| COMPARISON OPERATORS REAL-WORLD ANALOGY |
+-------------------------------------------------------------------------+
1. ASSIGNMENT (=) vs EQUALITY (==):
- Single Equals (=): Placing a voter ID inside your wallet (Action!).
voter_id = "ABC1234567"
- Double Equals (==): A polling booth officer scanning your ID:
"Is the scanned voter ID equal to the electoral roll name?" (Question!)
voter_id == registered_name -> True or False
2. THE NOTORIOUS C / JAVASCRIPT TRAP vs PYTHON:
- Check if age is between 18 and 60:
Let age = 85 (Senior citizen, NOT in range!)
- In JavaScript / C++: 18 <= age <= 60
Step 1: (18 <= 85) evaluates to true (1).
Step 2: 1 <= 60 evaluates to TRUE! (BUG! 85 is declared in range!)
- In Python: 18 <= age <= 60
Python translates this to: (18 <= age) and (age <= 60).
Evaluates to FALSE! Exactly matches human mathematical intuition!
+-------------------------------------------------------------------------+Python provides six standard relational operators:
Python allows you to chain multiple comparisons in a single line, exactly as in textbook mathematics:
When comparing strings, Python evaluates character by character based on their Unicode (ASCII) numerical code points:
'A' = 65) come before lowercase letters ('a' = 97).'Z' < 'a' is True!| Operator | Meaning | Example | Result | Notes |
|---|---|---|---|---|
== | Equal | 5 == 5.0 | True | Numeric promotion converts int to float |
!= | Not Equal | 10 != 20 | True | Inverted equality |
> | Greater Than | 15 > 10 | True | Strict inequality |
< | Less Than | 5 < 2 | False | Strict inequality |
>= | Greater Than or Equal | 10 >= 10 | True | Non-strict inequality |
<= | Less Than or Equal | 9 <= 10 | True | Non-strict inequality |
| Scenario | Anti-Pattern (Don't) | Best Practice (Do) | Why |
|---|---|---|---|
| Range Checks | if age >= 18 and age <= 60: | if 18 <= age <= 60: | Chained comparison is cleaner, faster, and algebraic. |
| Equality Check | if status = "ACTIVE": | if status == "ACTIVE": | Single = is assignment and raises a SyntaxError inside if. |
| Boolean Check | if is_verified == True: | if is_verified: | Redundant equality check is unpythonic; booleans are evaluated directly. |
| Float Equality | if float_val == 0.3: | if math.isclose(float_val, 0.3): | Floating-point binary representation errors cause exact == checks to fail. |
+-------------------------------------------------------------------------+
| COMPARISON OPERATORS CHEAT SHEET |
+-------------------------------------------------------------------------+
- Operators: ==, !=, >, <, >=, <=
- Output: Always returns boolean True or False
- Chaining: 18 <= age <= 60 translates to (18 <= age and age <= 60)
- Short-Circuit: In chained checks, evaluation stops at first False
- String Sorting: Compared by Unicode code points ('A' < 'a' is True)
- Boolean Idiom: Use 'if is_valid:' instead of 'if is_valid == True:'
+-------------------------------------------------------------------------+=) with Equality (==)A single equals sign = assigns a value. Two equals signs == checks for equality:
and / orBeginners often write if x == 1 or 2:, which always evaluates to True because non-zero integers are truthy!
10 <= 25 <= 50 evaluate to in Python?A. False B. True C. TypeError D. SyntaxError
A. <> B. != C. !== D. not ==
"Zebra" < "apple" evaluate to True in Python?A. Python calculates length, and "Zebra" has fewer characters B. Python compares characters by Unicode code points, where uppercase letters ('Z' = 90) have lower numerical values than lowercase letters ('a' = 97) C. It is a bug in the interpreter D. "Zebra" is alphabetically before "apple" in Dutch
if x = 10: in a Python conditional?A. Python assigns 10 to x and evaluates the condition as True B. Python raises a SyntaxError: invalid syntax. Maybe you meant '==' or ':='? C. Python ignores the statement D. Python crashes with a memory leak
5 == 5.0 in Python?A. False because the data types are different (int vs float) B. True because Python promotes the integer to a float before checking numeric equality C. TypeError D. None
Write a complete, runnable Python script that determines an employee's income tax bracket under the New Tax Regime (FY 2024-26) using Python's mathematical chained comparisons (0 <= income <= 3_00_000, etc.).
In the next lesson, we will continue your Python learning journey with Logical Operators (6: Operators).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Assignment Operators | Logical Operators |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.