PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Comparison and Logical Operators: and, or, not (पायथन तुलनात्मक ऑपरेटर्स और लॉजिकल ऑपरेटर्स (and, or, not)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python comparison operators (==, !=, <, <=, >, >=) evaluate value relationships and support chained comparisons, whereas logical operators (and, or, not) combine truthy and falsy expressions using short-circuit evaluation. Crucially, Python's and and or return one of their actual operand objects rather than strictly converting to bool.
अध्याय सार: पायथन के तुलनात्मक ऑपरेटर्स (==, !=, <, <=, >, >=) मानों की तुलना करते हैं और चेन्ड तुलना (chained comparison) का समर्थन करते हैं, जबकि लॉजिकल ऑपरेटर्स (and, or, not) शॉर्ट-सर्किट मूल्यांकन द्वारा ट्रूथी/फॉल्सी अभिव्यक्तियों को जोड़ते हैं। विशेष बात यह है कि and व or हमेशा bool के बजाय अपने ऑपरेंड्स में से एक वास्तविक मान लौटाते हैं।
Python Comparison and Logical Operators: and, or, not: Detailed Notes & Architecture (पायथन तुलनात्मक ऑपरेटर्स और लॉजिकल ऑपरेटर्स (and, or, not))
1. Comparison Operators and Chained Comparisons (तुलनात्मक ऑपरेटर्स और चेन्ड तुलना (Chained Comparisons))
[EN] The six relational operators (==, !=, <, <=, >, >=) compare values and return True or False for built-in types. Comparing incompatible types for ordering (like 5 < '5') raises TypeError in Python 3, whereas equality checks (5 == '5') safely return False. Python allows chaining comparisons arbitrarily: 10 <= x <= 20 is evaluated as (10 <= x) and (x <= 20), with x evaluated only once. Note a subtle pitfall: 1 != 2 != 1 evaluates to (1 != 2) and (2 != 1), which is True even though the outer values match.
[HI] छह तुलनात्मक ऑपरेटर्स (==, !=, <, <=, >, >=) मानों की तुलना करते हैं और बिल्ट-इन प्रकारों के लिए True या False लौटाते हैं। पायथन 3 में असंगत प्रकारों की क्रम तुलना (जैसे 5 < '5') करने पर TypeError आता है, जबकि समानता जाँच (5 == '5') सुरक्षित रूप से False देती है। पायथन में चेन्ड तुलना संभव है: 10 <= x <= 20 का अर्थ (10 <= x) and (x <= 20) होता है, जहाँ x का मूल्यांकन केवल एक बार होता है। ध्यान देने योग्य बिंदु यह है कि 1 != 2 != 1 का मान True आता है, क्योंकि यह (1 != 2) और (2 != 1) की जाँच करता है।
2. Truth Value Testing (Truthy vs Falsy Objects) (ट्रुथ वैल्यू टेस्टिंग (ट्रूथी बनाम फॉल्सी मान))
[EN] Any Python object can be tested in a boolean context (if, while, and, or, not) via __bool__() or fallback __len__(). Objects considered Falsy are: False, None, numeric zeros (0, 0.0, 0j), and empty containers or sequences ('', [], (), {}, set(), range(0)). All other objects are Truthy, including non-empty strings like 'False' or '0', negative numbers like -1, and non-empty collections like [0] or (None,). Calling bool(obj) reveals the exact boolean truthiness of any value.
[HI] पायथन की हर वस्तु का बूलियन संदर्भ (if, while, and, or, not) में __bool__() या __len__() द्वारा परीक्षण किया जा सकता है। फॉल्सी (Falsy) माने जाने वाले मान हैं: False, None, शून्य संख्याएँ (0, 0.0, 0j) तथा खाली कंटेनर ('', [], (), {}, set(), range(0))। इनके अतिरिक्त अन्य सभी मान ट्रूथी (Truthy) होते हैं, जिनमें 'False', '0', -1, [0] और (None,) जैसे मान भी शामिल हैं। किसी भी ऑब्जेक्ट की ट्रूथ वैल्यू जानने के लिए bool(obj) फंक्शन का उपयोग किया जाता है।
3. Short-Circuit Evaluation and Return Semantics of and, or, not (शॉर्ट-सर्किट मूल्यांकन तथा and, or, not के रिटर्न नियम)
[EN] Unlike many languages where logical operators always produce a boolean, Python's 'and' and 'or' return one of their operands directly. In x and y, Python evaluates x first; if x is falsy, it short-circuits and returns x immediately without evaluating y; otherwise it evaluates and returns y. In x or y, Python evaluates x first; if x is truthy, it short-circuits and returns x immediately; otherwise it evaluates and returns y. Only the unary 'not' operator is guaranteed to always return a strict bool (True or False).
[HI] अन्य कई भाषाओं के विपरीत, पायथन में 'and' और 'or' हमेशा True/False देने के बजाय अपने ऑपरेंड्स में से ही कोई एक मान लौटाते हैं। x and y में यदि x फॉल्सी है, तो यह शॉर्ट-सर्किट होकर y को जाँचे बिना तुरंत x लौटाता है; अन्यथा y का मूल्यांकन कर y लौटाता है। x or y में यदि x ट्रूथी है, तो यह तुरंत x लौटाता है; अन्यथा y का मान लौटाता है। केवल यूनरी ऑपरेटर 'not' ही हमेशा सख्त रूप से bool (True या False) लौटाने की गारंटी देता है।
4. Precedence Rules and Defensive Coding Idioms (प्राथमिकता के नियम और व्यावहारिक कोडिंग उपयोग)
[EN] Among these operators, comparison operators (<, <=, >, >=, ==, !=) have the highest precedence, followed by 'not', then 'and', and finally 'or' (lowest). Therefore, True or False and False parses as True or (False and False) -> True, not (True or False) and False. Similarly, a == not b raises SyntaxError because == binds tighter than 'not'; parentheses a == (not b) are required. Short-circuiting enables safe guard expressions like (divisor != 0 and total / divisor > 2) and concise default values like name = input_name or 'Guest'.
[HI] इन ऑपरेटर्स में तुलनात्मक ऑपरेटर्स की प्राथमिकता सबसे अधिक होती है, उसके बाद 'not', फिर 'and', और सबसे कम प्राथमिकता 'or' की होती है। इसीलिए True or False and False को True or (False and False) मानकर True परिणाम आता है। चूंकि == की प्राथमिकता 'not' से अधिक है, इसलिए a == not b लिखने पर SyntaxError आता है; इसके लिए a == (not b) लिखना आवश्यक है। शॉर्ट-सर्किटिंग का उपयोग divisor != 0 and total / divisor > 2 जैसे सुरक्षित गार्ड बनाने और डिफ़ॉल्ट मान सेट करने में होता है।
Syntax, Formula & Technical Example (Python Comparison and Logical Operators: and, or, not)
x = 15
print(10 < x <= 20) # True
print(1 != 2 != 1) # True
print(0 and 1 / 0) # 0 (short-circuit)
print('' or 'Default') # Default
print(not [], not '0') # True False
print(True or False and False) # True
Python Comparison and Logical Operators: and, or, not Quick Reference & Comparison Table
| Operator | Evaluation Rule | Key Exam Fact |
|---|---|---|
| == / < | Value & ordering comparison | Chained a < b < c evaluates b once |
| and | Returns 1st falsy or last value | Short-circuits if left operand is falsy |
| or | Returns 1st truthy or last value | Short-circuits if left operand is truthy |
| not | Logical negation (always bool) | Lower precedence than ==, higher than and |
Python Comparison and Logical Operators: and, or, not Top 20 Bilingual MCQs with Answers
Q1. What is the output of: print(5 or 10) in Python?
(पायथन में print(5 or 10) का आउटपुट क्या होगा?)
(A) False
(B) 10
(C) 5
(D) True
Correct Answer: (C)
[EN] Because 5 is truthy, 'or' short-circuits and returns the first operand 5 directly rather than True.
[HI] चूंकि 5 ट्रूथी है, इसलिए 'or' शॉर्ट-सर्किट होकर True के बजाय सीधे पहला ऑपरेंड 5 लौटा देता है।
Q2. What does the expression ('Python' and [1, 2]) evaluate to?
(व्यंजक ('Python' and [1, 2]) का मूल्यांकन क्या होगा?)
(A) False
(B) True
(C) [1, 2]
(D) 'Python'
Correct Answer: (C)
[EN] The left operand 'Python' is truthy, so 'and' evaluates and returns the second operand [1, 2].
[HI] बायाँ ऑपरेंड 'Python' ट्रूथी है, इसलिए 'and' दूसरे ऑपरेंड [1, 2] का मूल्यांकन कर उसे लौटाता है।
Q3. What happens when Python executes: 0 and (10 // 0)?
(जब पायथन 0 and (10 // 0) चलाता है, तो क्या होता है?)
(A) Evaluates to False
(B) Evaluates to 0 without raising ZeroDivisionError
(C) Raises ZeroDivisionError
(D) Raises TypeError
Correct Answer: (B)
[EN] Since 0 is falsy, 'and' short-circuits immediately and never evaluates (10 // 0).
[HI] 0 फॉल्सी होने के कारण 'and' तुरंत शॉर्ट-सर्किट हो जाता है और (10 // 0) का मूल्यांकन ही नहीं होता।
Q4. What is the output of: print(bool('False'), bool([0]))?
(print(bool('False'), bool([0])) का आउटपुट क्या होगा?)
(A) False True
(B) False False
(C) True False
(D) True True
Correct Answer: (D)
[EN] Both 'False' (a non-empty string) and [0] (a non-empty list) are truthy in Python.
[HI] 'False' (गैर-खाली स्ट्रिंग) और [0] (गैर-खाली लिस्ट) दोनों ही पायथन में ट्रूथी होते हैं।
Q5. Which of the following values is considered Falsy in Python?
(पायथन में निम्नलिखित में से कौन सा मान फॉल्सी (Falsy) माना जाता है?)
(A) (-1,)
(B) '0'
(C) range(0)
(D) {None}
Correct Answer: (C)
[EN] Empty ranges like range(0) have length 0 and are falsy; '0', (-1,), and {None} are all non-empty and truthy.
[HI] खाली रेंज range(0) की लंबाई 0 होती है अतः यह फॉल्सी है; जबकि '0', (-1,) और {None} गैर-खाली होने से ट्रूथी हैं।
Q6. What does True or False and False evaluate to?
(True or False and False का परिणाम क्या होगा?)
(A) None
(B) SyntaxError
(C) True
(D) False
Correct Answer: (C)
[EN] 'and' has higher precedence than 'or', so it parses as True or (False and False) = True or False = True.
[HI] 'and' की प्राथमिकता 'or' से अधिक होती है, इसलिए यह True or (False and False) = True देता है।
Q7. What is the result of: 5 == '5' and 5 < '5' respectively in Python 3?
(पायथन 3 में क्रमशः 5 == '5' और 5 < '5' का परिणाम क्या होगा?)
(A) False and False
(B) True and False
(C) TypeError and TypeError
(D) False and TypeError
Correct Answer: (D)
[EN] Equality (==) between different types safely returns False, while ordering (<) raises TypeError in Python 3.
[HI] अलग-अलग प्रकारों के बीच == सुरक्षित रूप से False देता है, जबकि < (क्रम तुलना) से TypeError आता है।
Q8. What does the chained expression 1 < 5 < 3 evaluate to?
(चेन्ड व्यंजक 1 < 5 < 3 का मान क्या होगा?)
(A) True
(B) SyntaxError
(C) False
(D) TypeError
Correct Answer: (C)
[EN] 1 < 5 < 3 is equivalent to (1 < 5) and (5 < 3), which is True and False -> False.
[HI] 1 < 5 < 3 का अर्थ (1 < 5) and (5 < 3) है, जो True and False -> False देता है।
Q9. What is the output of: 1 != 2 != 1 in Python?
(पायथन में 1 != 2 != 1 का आउटपुट क्या होगा?)
(A) SyntaxError
(B) False
(C) True
(D) 1
Correct Answer: (C)
[EN] Chained comparison expands to (1 != 2) and (2 != 1), which are both True, so the overall result is True.
[HI] चेन्ड तुलना (1 != 2) and (2 != 1) में बदलती है; दोनों सत्य होने के कारण कुल परिणाम True आता है।
Q10. Why does the statement x = (5 == not False) raise a SyntaxError?
(कथन x = (5 == not False) में SyntaxError क्यों आता है?)
(A) Because == has higher precedence than 'not', requiring 5 == (not False)
(B) Because 'not' returns an integer
(C) Because == only compares numbers
(D) Because 'not' cannot be applied to False
Correct Answer: (A)
[EN] Comparison operators bind tighter than 'not', so 'not' cannot appear as an unparenthesized right operand of ==.
[HI] तुलनात्मक ऑपरेटर्स की प्राथमिकता 'not' से अधिक होती है, इसलिए == के दाईं ओर बिना कोष्ठक के 'not' नहीं लिखा जा सकता।
Q11. What is the correct precedence order from highest to lowest?
(उच्चतम से निम्नतम प्राथमिकता का सही क्रम कौन सा है?)
(A) Comparisons > or > and > not
(B) and > or > not > Comparisons
(C) not > Comparisons > and > or
(D) Comparisons > not > and > or
Correct Answer: (D)
[EN] In Python, comparison operators evaluate first, followed by unary 'not', then 'and', and finally 'or'.
[HI] पायथन में पहले तुलनात्मक ऑपरेटर्स, फिर 'not', उसके बाद 'and' और अंत में 'or' का मूल्यांकन होता है।
Q12. What is the output of: print(not '', not None, not -5)?
(print(not '', not None, not -5) का आउटपुट क्या होगा?)
(A) 1 1 0
(B) True True False
(C) False True True
(D) True False False
Correct Answer: (B)
[EN] '' and None are falsy (so 'not' yields True), while non-zero -5 is truthy (so 'not -5' is False).
[HI] '' और None फॉल्सी हैं (अतः not इन्हें True बनाता है), जबकि -5 ट्रूथी है (अतः not -5 का मान False होगा)।
Q13. What does ('' or [] or 0 or None) return?
(('' or [] or 0 or None) क्या मान लौटाता है?)
(A) ''
(B) 0
(C) False
(D) None
Correct Answer: (D)
[EN] When all operands in an 'or' chain are falsy, Python returns the last evaluated operand, which is None.
[HI] जब 'or' श्रृंखला के सभी ऑपरेंड फॉल्सी होते हैं, तो पायथन अंतिम ऑपरेंड (यहाँ None) को लौटाता है।
Q14. What is the output of: float('nan') == float('nan')?
(float('nan') == float('nan') का आउटपुट क्या होगा?)
(A) ValueError
(B) None
(C) True
(D) False
Correct Answer: (D)
[EN] Per the IEEE 754 floating-point standard followed by Python, NaN is never equal to anything, including itself.
[HI] IEEE 754 मानक के अनुसार NaN स्वयं के भी बराबर नहीं होता, इसलिए परिणाम False आता है।
Q15. In 0 <= f() <= 10, how many times is f() called?
(0 <= f() <= 10 में फंक्शन f() को कितनी बार कॉल किया जाता है?)
(A) Twice
(B) Once
(C) Zero times
(D) Three times
Correct Answer: (B)
[EN] Python evaluates the middle expression of a chained comparison exactly once and caches the result for both checks.
[HI] चेन्ड तुलना में मध्य व्यंजक f() का मूल्यांकन केवल एक बार ही होता है।
Q16. Which statement about Python logical operators is NOT true?
(पायथन लॉजिकल ऑपरेटर्स के बारे में कौन सा कथन सत्य नहीं है?)
(A) The 'not' operator always returns a bool value
(B) Both empty strings and empty lists evaluate as falsy
(C) Short-circuit evaluation skips the right operand when the result is already determined
(D) The 'and' and 'or' operators always return a bool (True or False)
Correct Answer: (D)
[EN] 'and' and 'or' return one of their operand values without converting it to bool; only 'not' always returns a strict bool.
[HI] 'and' और 'or' हमेशा bool लौटाने के बजाय अपने एक ऑपरेंड का वास्तविक मान लौटाते हैं; केवल 'not' ही हमेशा bool देता है।
Q17. Within Python Comparison and Logical Operators: and, or, not, which operator directly maps to: "Value & ordering comparison"?
(Python Comparison and Logical Operators: and, or, not में कौन सा Operator सीधे "Value & ordering comparison" से संबंधित है?)
(A) and
(B) not
(C) or
(D) == / <
Correct Answer: (D)
[EN] In Python Comparison and Logical Operators: and, or, not, '== / <' handles 'Value & ordering comparison' (Chained a < b < c evaluates b once).
[HI] Python Comparison and Logical Operators: and, or, not में '== / <' का कार्य/विवरण 'Value & ordering comparison' (Chained a < b < c evaluates b once) है।
Q18. Which option accurately describes 'and' in Python Comparison and Logical Operators: and, or, not?
(Python Comparison and Logical Operators: and, or, not के संदर्भ में 'and' की सही विशेषता कौन सी है?)
(A) Returns 1st truthy or last value
(B) Value & ordering comparison
(C) Logical negation (always bool)
(D) Returns 1st falsy or last value
Correct Answer: (D)
[EN] 'and' provides 'Returns 1st falsy or last value' — key point: Short-circuits if left operand is falsy.
[HI] 'and' का मुख्य विवरण 'Returns 1st falsy or last value' है (Short-circuits if left operand is falsy)।
Q19. In Python Comparison and Logical Operators: and, or, not, identify the correct parameter for the specification: "Returns 1st truthy or last value".
(Python Comparison and Logical Operators: and, or, not में "Returns 1st truthy or last value" विवरण किस विकल्प के लिए सही है?)
(A) not
(B) and
(C) == / <
(D) or
Correct Answer: (D)
[EN] 'or' corresponds to 'Returns 1st truthy or last value' (Short-circuits if left operand is truthy).
[HI] 'or' का संबंध 'Returns 1st truthy or last value' (Short-circuits if left operand is truthy) से है।
Q20. Considering Python Comparison and Logical Operators: and, or, not, what key behavior defines 'not'?
(Python Comparison and Logical Operators: and, or, not में 'not' की मुख्य भूमिका या मान क्या है?)
(A) Returns 1st falsy or last value
(B) Value & ordering comparison
(C) Returns 1st truthy or last value
(D) Logical negation (always bool)
Correct Answer: (D)
[EN] 'not' represents 'Logical negation (always bool)' (Lower precedence than ==, higher than and).
[HI] 'not' का अर्थ/मान 'Logical negation (always bool)' है (Lower precedence than ==, higher than and)।
Python Comparison and Logical Operators: and, or, not — Exam FAQs
Q1: Why did (10 or 20) print 10 instead of True?
(10 or 20) चलाने पर True के बजाय 10 क्यों प्रिंट हुआ?
[EN] Python's 'or' and 'and' operators preserve the actual operand value that determined the result rather than casting to bool. Since 10 is truthy, 'or' stops immediately and returns 10.
[HI] पायथन के 'or' और 'and' ऑपरेटर्स परिणाम को bool में बदलने के बजाय निर्णायक ऑपरेंड का मूल मान लौटाते हैं। चूंकि 10 ट्रूथी है, 'or' तुरंत 10 लौटा देता है।
Q2: What is the benefit of chained comparisons like 1 <= x <= 100?
1 <= x <= 100 जैसी चेन्ड तुलना लिखने का क्या लाभ है?
[EN] It is both more readable and slightly faster than writing (1 <= x) and (x <= 100), because Python evaluates x only once instead of twice.
[HI] यह (1 <= x) and (x <= 100) की तुलना में अधिक पठनीय है और इसमें बीच का ऑपरेंड x केवल एक ही बार मूल्यांकित होता है।
Q3: When should I use 'not x' instead of 'x == False'?
मुझे 'x == False' के बजाय 'not x' कब उपयोग करना चाहिए?
[EN] Use 'not x' whenever you want to check general falsiness (like empty lists, None, 0, or False). Notice '0 == False' is True in Python, whereas '[] == False' is False even though 'not []' is True.
[HI] जब आप किसी भी खाली कंटेनर, None, 0 या False की जाँच करना चाहते हों तो 'not x' का प्रयोग करें; क्योंकि [] == False असत्य होता है जबकि not [] सत्य होता है।
Post a Comment
Post a Comment