PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Membership Operators: in and not in (Sequence Search) (सदस्यता ऑपरेटर्स: 'in' और 'not in' (सीक्वेंस में तत्व खोजना)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python membership operators (in and not in) test whether an item is contained within a sequence or collection, returning a boolean True or False. Crucially, 'in' checks contiguous substrings on strings, exact top-level elements on lists/tuples, keys only on dictionaries, and runs in O(1) average time on sets and dicts versus O(n) on lists.
अध्याय सार: पायथन के सदस्यता ऑपरेटर्स (in और not in) यह जांचते हैं कि कोई तत्व किसी सीक्वेंस या कलेक्शन में मौजूद है या नहीं और True/False लौटाते हैं। स्ट्रिंग पर 'in' सबस्ट्रिंग खोजता है, लिस्ट/टपल पर शीर्ष-स्तर का तत्व, डिक्शनरी पर केवल कुंजियाँ (keys), तथा सेट/डिक्शनरी पर यह O(1) समय में चलता है।
Python Membership Operators: in and not in (Sequence Search): Detailed Notes & Architecture (सदस्यता ऑपरेटर्स: 'in' और 'not in' (सीक्वेंस में तत्व खोजना))
1. Core Mechanics and Protocol of in / not in (in और not in की कार्यप्रणाली और प्रोटोकॉल)
[EN] The expression 'x in container' checks membership by calling container.__contains__(x) if defined, falling back to sequential iteration via __iter__() or old-style __getitem__() indexing. The operator 'not in' returns the exact boolean negation of 'in' (not (x in container)), and PEP 8 recommends 'x not in c' over 'not x in c'. Both operators share comparison-tier precedence (higher than 'not', 'and', 'or') and always evaluate to bool (True or False).
[HI] व्यंजक 'x in container' पहले __contains__(x) मेथड की मदद से सदस्यता जांचता है, या उसके अभाव में __iter__() अथवा __getitem__() द्वारा क्रमिक खोज करता है। 'not in' ऑपरेटर 'in' का ठीक विपरीत बूलियन परिणाम देता है, और PEP 8 में 'not x in c' के बजाय 'x not in c' लिखने की सलाह दी जाती है। दोनों की प्राथमिकता तुलनात्मक ऑपरेटर्स के बराबर ('not', 'and', 'or' से अधिक) होती है और ये हमेशा bool लौटाते हैं।
2. Substring Search on Strings vs Element Search on Lists/Tuples (स्ट्रिंग में सबस्ट्रिंग खोज बनाम लिस्ट/टपल में तत्व खोज)
[EN] On strings (str) and bytes, 'in' performs contiguous substring matching rather than single-character checking: 'on' in 'Python' is True, and the empty string '' in 'abc' is always True. Passing a non-string left operand to a string (e.g., 5 in '12345') raises TypeError because strings require string substrings. Conversely, on lists and tuples, 'in' checks only top-level elements using (elem is item or elem == item) — so [1, 2] in [1, 2, 3] is False, whereas [1, 2] in [[1, 2], 3] is True.
[HI] स्ट्रिंग (str) और बाइट्स पर 'in' एक लगातार सबस्ट्रिंग (contiguous substring) की खोज करता है: 'on' in 'Python' सत्य है और खाली स्ट्रिंग '' in 'abc' भी हमेशा True होती है। स्ट्रिंग में गैर-स्ट्रिंग खोजने पर (जैसे 5 in '12345') TypeError आता है। इसके विपरीत लिस्ट और टपल पर 'in' केवल शीर्ष-स्तरीय तत्वों (top-level elements) की तुलना करता है; अतः [1, 2] in [1, 2, 3] False होगा, जबकि [1, 2] in [[1, 2], 3] True होगा।
3. Dictionary Key Membership and Hash Table Performance (डिक्शनरी की (Key) सदस्यता और हैश टेबल प्रदर्शन)
[EN] When applied to a dict, 'k in d' searches ONLY the dictionary's keys, ignoring its values; to search values or (key, value) pairs, explicitly use d.values() or d.items(). Time complexity differs drastically across containers: 'in' performs an O(n) linear scan on lists and tuples, whereas on sets, frozensets, and dicts it uses a hash table for O(1) average-case lookup. Remarkably, Python 3's range object computes 'x in range(start, stop, step)' in O(1) constant arithmetic time without building elements in memory.
[HI] डिक्शनरी पर 'k in d' केवल उसकी कुंजियों (keys) में खोज करता है, मानों (values) में नहीं; मान खोजने के लिए d.values() या जोड़ों के लिए d.items() का उपयोग करना होता है। लिस्ट और टपल में 'in' का समय O(n) रैखिक होता है, जबकि सेट, फ्रोजेनसेट और डिक्शनरी में हैश टेबल के कारण औसत समय O(1) होता है। पायथन 3 का range ऑब्जेक्ट भी 'x in range(...)' की गणना बिना मेमोरी में सूची बनाए O(1) गणितीय समय में करता है।
4. Unhashable Lookups, Edge Cases, and Chaining (अनहैशेबल तत्व, विशेष स्थितियाँ और चेनिंग)
[EN] Because dict and set rely on hashing, probing with an unhashable object like [1, 2] in {1, 2} raises TypeError: unhashable type: 'list' (whereas [1, 2] in [1, 2] safely returns False). Strings are case-sensitive under 'in', so 'py' in 'Python' is False unless case-normalized. Because 'in' and 'not in' belong to the comparison tier, they support Python comparison chaining: 'a' in 'ab' in 'abc' evaluates as ('a' in 'ab') and ('ab' in 'abc'), returning True.
[HI] चूंकि dict और set हैशिंग का उपयोग करते हैं, इसलिए उनमें [1, 2] जैसा अनहैशेबल (म्यूटेबल) मान खोजने पर TypeError: unhashable type: 'list' आता है। स्ट्रिंग में 'in' केस-सेंसिटिव होता है ('py' in 'Python' False है)। तुलनात्मक श्रेणी में होने के कारण ये चेन्ड तुलना भी सपोर्ट करते हैं: 'a' in 'ab' in 'abc' का अर्थ ('a' in 'ab') and ('ab' in 'abc') अर्थात True होता है।
Syntax, Formula & Technical Example (Python Membership Operators: in and not in (Sequence Search))
print('py' in 'python', '' in 'abc') # True True
nums = [10, 20, 30]
print(20 in nums, [10, 20] in nums) # True False
d = {'name': 'Raj', 'age': 25}
print('name' in d, 'Raj' in d) # True False
print('Raj' in d.values()) # True
print(500 in range(0, 1000, 5)) # True
Python Membership Operators: in and not in (Sequence Search) Quick Reference & Comparison Table
| Container Type | What 'in' Searches | Average Lookup Complexity |
|---|---|---|
| str | Contiguous substring (case-sensitive) | O(n * m) worst / fast C scan |
| list / tuple | Exact top-level element | O(n) linear scan |
| set / frozenset | Hashed set member | O(1) average hash lookup |
| dict | Dictionary keys only (not values) | O(1) constant-time key hash |
Python Membership Operators: in and not in (Sequence Search) Top 20 Bilingual MCQs with Answers
Q1. What is the output of: print('' in 'Python')?
(print('' in 'Python') का आउटपुट क्या होगा?)
(A) False
(B) ValueError
(C) True
(D) None
Correct Answer: (C)
[EN] The empty string '' is mathematically and programmatically considered a substring of every string in Python.
[HI] पायथन में खाली स्ट्रिंग '' हर स्ट्रिंग का एक वैध सबस्ट्रिंग मानी जाती है, इसलिए परिणाम True आता है।
Q2. What does [2, 3] in [1, 2, 3, 4] evaluate to?
([2, 3] in [1, 2, 3, 4] का मूल्यांकन क्या होगा?)
(A) True
(B) TypeError
(C) False
(D) SyntaxError
Correct Answer: (C)
[EN] On lists, 'in' checks top-level elements (1, 2, 3, 4) rather than sublists; none of them equals [2, 3].
[HI] लिस्ट पर 'in' सब-लिस्ट के बजाय केवल शीर्ष-स्तरीय तत्वों की जाँच करता है; यहाँ [2, 3] एक तत्व के रूप में मौजूद नहीं है।
Q3. What is the output of: d = {'a': 1, 'b': 2}; print(1 in d)?
(d = {'a': 1, 'b': 2}; print(1 in d) का आउटपुट क्या होगा?)
(A) True
(B) False
(C) KeyError
(D) TypeError
Correct Answer: (B)
[EN] 'in' on a dictionary searches only its keys ('a' and 'b'), not its values (1 and 2).
[HI] डिक्शनरी पर 'in' केवल उसकी कुंजियों (keys) की जाँच करता है, मानों (values) की नहीं।
Q4. Which container provides O(1) average-case time complexity for the 'in' operator?
('in' ऑपरेटर के लिए कौन सा कंटेनर औसतन O(1) समय जटिलता प्रदान करता है?)
(A) str
(B) set
(C) list
(D) tuple
Correct Answer: (B)
[EN] Sets and dictionaries use hash tables for O(1) average membership checks, whereas lists and tuples scan linearly in O(n).
[HI] सेट और डिक्शनरी हैश टेबल का उपयोग करते हैं जिससे सदस्यता जाँच औसतन O(1) समय में पूरी होती है।
Q5. What happens when Python executes: 3 in '12345'?
(पायथन में 3 in '12345' चलाने पर क्या होता है?)
(A) Returns True
(B) Converts 3 to '3' automatically
(C) Raises TypeError
(D) Returns False
Correct Answer: (C)
[EN] String membership requires a string on the left side; passing an int raises TypeError.
[HI] स्ट्रिंग के साथ 'in' में बाईं ओर भी स्ट्रिंग होना आवश्यक है; int देने पर TypeError आता है।
Q6. How do you check if value 25 exists among the values of dictionary d?
(डिक्शनरी d के मानों (values) में 25 की मौजूदगी कैसे जांचेंगे?)
(A) 25 in d.values()
(B) 25 in d
(C) d.contains(25)
(D) 25 in d.keys()
Correct Answer: (A)
[EN] d.values() exposes a view of the dictionary's values, allowing membership checks on values.
[HI] d.values() डिक्शनरी के सभी मानों का व्यू देता है, जिस पर 'in' द्वारा मान खोजा जा सकता है।
Q7. What is the output of: print('Py' in 'python')?
(print('Py' in 'python') का आउटपुट क्या होगा?)
(A) ValueError
(B) None
(C) True
(D) False
Correct Answer: (D)
[EN] String membership with 'in' is strictly case-sensitive: 'Py' does not match 'py'.
[HI] स्ट्रिंग पर 'in' केस-सेंसिटिव होता है, इसलिए 'Py' और 'py' का मिलान नहीं होता और परिणाम False आता है।
Q8. What happens when you evaluate [1] in {1, 2, 3}?
([1] in {1, 2, 3} चलाने पर क्या होता है?)
(A) Raises ValueError
(B) Raises TypeError: unhashable type: 'list'
(C) Returns False
(D) Returns True
Correct Answer: (B)
[EN] Testing membership in a set requires hashing the searched item, and mutable lists are unhashable.
[HI] सेट में खोजने के लिए तत्व का हैशेबल होना जरूरी है; म्यूटेबल लिस्ट अनहैशेबल होने के कारण TypeError देती है।
Q9. What is the time complexity of checking 999999 in range(1000000) in Python 3?
(पायथन 3 में 999999 in range(1000000) की समय जटिलता क्या है?)
(A) O(1)
(B) O(log n)
(C) O(n)
(D) O(n^2)
Correct Answer: (A)
[EN] Python 3's range.__contains__ uses arithmetic bounds and step divisibility math to verify integer membership in O(1) time.
[HI] पायथन 3 में range गणितीय सीमा और स्टेप सूत्र का उपयोग करके O(1) समय में सदस्यता की जाँच करता है।
Q10. What is the output of: print(('a', 1) in {'a': 1, 'b': 2}.items())?
(print(('a', 1) in {'a': 1, 'b': 2}.items()) का आउटपुट क्या होगा?)
(A) False
(B) TypeError
(C) KeyError
(D) True
Correct Answer: (D)
[EN] d.items() yields (key, value) tuple pairs, and ('a', 1) matches the first pair in O(1) time.
[HI] d.items() कुंजी-मान के टपल जोड़े देता है, और ('a', 1) इसमें मौजूद होने के कारण परिणाम True होगा।
Q11. Which dunder method does Python call first when evaluating 'x in obj'?
('x in obj' का मूल्यांकन करते समय पायथन सबसे पहले किस डंडर (dunder) मेथड को कॉल करता है?)
(A) __contains__
(B) __has__
(C) __member__
(D) __find__
Correct Answer: (A)
[EN] Python invokes obj.__contains__(x) first, falling back to __iter__() and __getitem__() only if __contains__ is absent.
[HI] पायथन सबसे पहले obj.__contains__(x) को कॉल करता है; इसके न होने पर __iter__() या __getitem__() का उपयोग होता है।
Q12. What is the output of: print(1 in [True, False])?
(print(1 in [True, False]) का आउटपुट क्या होगा?)
(A) False
(B) True
(C) TypeError
(D) None
Correct Answer: (B)
[EN] Membership uses equality (==) after identity, and in Python 1 == True evaluates to True.
[HI] सदस्यता जाँच में == का उपयोग होता है, और पायथन में 1 == True सत्य होता है, अतः परिणाम True आएगा।
Q13. What is the result of: 'a' in 'ab' in 'abc'?
('a' in 'ab' in 'abc' का परिणाम क्या होगा?)
(A) False
(B) True
(C) TypeError
(D) SyntaxError
Correct Answer: (B)
[EN] Chained membership parses as ('a' in 'ab') and ('ab' in 'abc'), both of which are True.
[HI] चेन्ड तुलना के कारण यह ('a' in 'ab') and ('ab' in 'abc') के रूप में हल होकर True देता है।
Q14. What is the output of: print(4 not in range(0, 10, 2))?
(print(4 not in range(0, 10, 2)) का आउटपुट क्या होगा?)
(A) None
(B) True
(C) False
(D) ValueError
Correct Answer: (C)
[EN] range(0, 10, 2) represents [0, 2, 4, 6, 8]; since 4 is present, '4 not in ...' is False.
[HI] range(0, 10, 2) में [0, 2, 4, 6, 8] शामिल हैं; चूंकि 4 मौजूद है, इसलिए '4 not in' का उत्तर False होगा।
Q15. Which expression adheres to PEP 8 style guidelines?
(कौन सा व्यंजक PEP 8 स्टाइल दिशानिर्देशों के अनुरूप है?)
(A) if x not in my_list:
(B) if (x in my_list) == False:
(C) if my_list.has(x):
(D) if not x in my_list:
Correct Answer: (A)
[EN] PEP 8 explicitly recommends 'x not in seq' instead of 'not x in seq' for clarity.
[HI] PEP 8 स्पष्ट रूप से पठनीयता के लिए 'not x in seq' के बजाय 'x not in seq' लिखने की सलाह देता है।
Q16. Which of the following statements about Python membership operators is NOT true?
(पायथन के सदस्यता ऑपरेटर्स के बारे में कौन सा कथन सत्य नहीं है?)
(A) 'in' and 'not in' return a boolean True or False
(B) Checking membership in a set is faster on average than in a large list
(C) 'in' on a string checks for contiguous substrings, not unordered characters
(D) 'x in dict' checks whether x exists as either a key or a value in dict
Correct Answer: (D)
[EN] Dictionary membership 'x in d' inspects ONLY keys, never values (unless d.values() is explicitly queried).
[HI] 'x in d' डिक्शनरी में केवल कुंजियों (keys) की जाँच करता है, मानों (values) की नहीं।
Q17. Within Python Membership Operators: in and not in (Sequence Search), which container type directly maps to: "Contiguous substring (case-sensitive)"?
(Python Membership Operators: in and not in (Sequence Search) में कौन सा Container Type सीधे "Contiguous substring (case-sensitive)" से संबंधित है?)
(A) str
(B) set / frozenset
(C) dict
(D) list / tuple
Correct Answer: (A)
[EN] In Python Membership Operators: in and not in (Sequence Search), 'str' handles 'Contiguous substring (case-sensitive)' (O(n * m) worst / fast C scan).
[HI] Python Membership Operators: in and not in (Sequence Search) में 'str' का कार्य/विवरण 'Contiguous substring (case-sensitive)' (O(n * m) worst / fast C scan) है।
Q18. Which option accurately describes 'list / tuple' in Python Membership Operators: in and not in (Sequence Search)?
(Python Membership Operators: in and not in (Sequence Search) के संदर्भ में 'list / tuple' की सही विशेषता कौन सी है?)
(A) Hashed set member
(B) Exact top-level element
(C) Dictionary keys only (not values)
(D) Contiguous substring (case-sensitive)
Correct Answer: (B)
[EN] 'list / tuple' provides 'Exact top-level element' — key point: O(n) linear scan.
[HI] 'list / tuple' का मुख्य विवरण 'Exact top-level element' है (O(n) linear scan)।
Q19. In Python Membership Operators: in and not in (Sequence Search), identify the correct parameter for the specification: "Hashed set member".
(Python Membership Operators: in and not in (Sequence Search) में "Hashed set member" विवरण किस विकल्प के लिए सही है?)
(A) str
(B) dict
(C) list / tuple
(D) set / frozenset
Correct Answer: (D)
[EN] 'set / frozenset' corresponds to 'Hashed set member' (O(1) average hash lookup).
[HI] 'set / frozenset' का संबंध 'Hashed set member' (O(1) average hash lookup) से है।
Q20. Considering Python Membership Operators: in and not in (Sequence Search), what key behavior defines 'dict'?
(Python Membership Operators: in and not in (Sequence Search) में 'dict' की मुख्य भूमिका या मान क्या है?)
(A) Dictionary keys only (not values)
(B) Exact top-level element
(C) Contiguous substring (case-sensitive)
(D) Hashed set member
Correct Answer: (A)
[EN] 'dict' represents 'Dictionary keys only (not values)' (O(1) constant-time key hash).
[HI] 'dict' का अर्थ/मान 'Dictionary keys only (not values)' है (O(1) constant-time key hash)।
Python Membership Operators: in and not in (Sequence Search) — Exam FAQs
Q1: Why is 'x in my_set' so much faster than 'x in my_list'?
'x in my_set' की गति 'x in my_list' की तुलना में इतनी तेज क्यों होती है?
[EN] A list checks elements one by one from start to finish (O(n) linear time), whereas a set computes hash(x) and jumps directly to the bucket in memory (O(1) average time).
[HI] लिस्ट में तत्वों की खोज शुरुआत से अंत तक एक-एक करके (O(n)) होती है, जबकि सेट hash(x) निकालकर सीधे मेमोरी बकेट में O(1) समय में जाँच कर लेता है।
Q2: Why does '1 in [True]' return True?
'1 in [True]' का परिणाम True क्यों आता है?
[EN] Because Python's list membership checks 'elem is target or elem == target', and bool subclasses int where True == 1 is True.
[HI] क्योंकि लिस्ट में सदस्यता जाँच के दौरान == तुलना होती है और पायथन में bool, int की सबक्लास है जहाँ True == 1 सत्य होता है।
Q3: How do I check if ALL items of a sublist exist inside a list in order?
यह कैसे जांचें कि एक छोटी लिस्ट के सभी तत्व क्रम से बड़ी लिस्ट में हैं या नहीं?
[EN] The 'in' operator only checks single elements on lists, so [2, 3] in [1, 2, 3] is False. You can use all(x in big for x in small) for unordered subset checks, or slicing windows for contiguous sublists.
[HI] लिस्ट पर 'in' केवल एकल तत्व खोजता है। बिना क्रम के जाँच के लिए all(x in big for x in small) या लगातार सब-लिस्ट के लिए स्लाइसिंग विंडो का प्रयोग करें।
Post a Comment
Post a Comment