PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Set Methods: add(), update(), remove(), discard(), pop() (सेट मेथड्स: add(), discard() (बिना एरर हटाना) और pop()) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python sets provide in-place mutation methods: add() for a single item, update() for unpacking iterables, remove() and discard() for deleting a specific element, and pop() to remove an arbitrary item. Crucially, discard() ignores missing values silently while remove() raises KeyError.
अध्याय सार: पायथन सेट में इन-प्लेस बदलाव के लिए add() (एकल तत्व जोड़ना), update() (इटरेबल जोड़ना), remove() और discard() (निर्दिष्ट तत्व हटाना) तथा pop() (कोई भी एक तत्व हटाना) मेथड्स होते हैं। इनमें मुख्य अंतर यह है कि तत्व न मिलने पर discard() कोई एरर नहीं देता, जबकि remove() KeyError उत्पन्न करता है।
Python Set Methods: add(), update(), remove(), discard(), pop(): Detailed Notes & Architecture (सेट मेथड्स: add(), discard() (बिना एरर हटाना) और pop())
1. add(elem) vs update(*others) (add(elem) बनाम update(*others))
[EN] s.add(elem) inserts a single hashable object into the set in O(1) average time and returns None; if elem is already present, the set remains unchanged without error, while unhashable arguments like lists raise TypeError. In contrast, s.update(*others) (equivalent to s |= other) accepts one or more iterables (lists, tuples, strings, dicts, sets) and inserts each yielded hashable item individually in place, returning None.
[HI] s.add(elem) सेट में एक एकल हैशेबल तत्व औसत O(1) समय में जोड़ता है और None लौटाता है; तत्व पहले से होने पर कोई त्रुटि नहीं आती, पर लिस्ट जैसे अनहैशेबल मान पर TypeError आता है। वहीं s.update(*others) एक या अधिक इटरेबल (लिस्ट, स्ट्रिंग, टपल, सेट) लेकर उनके सभी तत्वों को एक-एक कर इन-प्लेस जोड़ता है और None लौटाता है।
2. remove(elem) vs discard(elem): Error Handling (remove(elem) बनाम discard(elem): एरर प्रबंधन)
[EN] Both s.remove(elem) and s.discard(elem) delete elem in O(1) average time and return None. Their critical exam distinction lies in missing-element behavior: s.remove(elem) raises KeyError if elem is absent from the set (just like missing dictionary keys, NOT ValueError!), whereas s.discard(elem) safely does nothing and leaves the set intact without raising any exception.
[HI] s.remove(elem) और s.discard(elem) दोनों ही O(1) औसत समय में तत्व को हटाते हैं और None लौटाते हैं। परीक्षा के लिए सबसे मुख्य अंतर तब दिखता है जब तत्व सेट में मौजूद न हो: s.remove(elem) ऐसी स्थिति में KeyError देता है (लिस्ट की तरह ValueError नहीं!), जबकि s.discard(elem) बिना किसी एरर के चुपचाप सेट को ज्यों का त्यों छोड़ देता है।
3. pop() and clear() on Unindexed Sets (अनइंडेक्स्ड सेट्स पर pop() और clear())
[EN] Because sets have no index positions, s.pop() takes zero arguments (passing an index like s.pop(0) raises TypeError) and removes and returns an arbitrary element from the hash table. Attempting s.pop() on an empty set raises KeyError: 'pop from an empty set'. Meanwhile, s.clear() empties all items from the set in place and returns None while preserving id(s).
[HI] सेट में कोई इंडेक्स नहीं होता, इसलिए s.pop() बिना किसी आर्गुमेंट के चलता है (s.pop(0) देने पर TypeError आता है) और हैश टेबल से कोई भी एक तत्व हटाकर उसे लौटाता है। खाली सेट पर pop() चलाने से KeyError आता है। वहीं s.clear() पूरे सेट को इन-प्लेस खाली करके None लौटाता है।
4. In-Place Algebraic Update Methods (इन-प्लेस गणितीय अपडेट मेथड्स)
[EN] Besides update() (|=), Python sets include intersection_update() (&=), difference_update() (-=), and symmetric_difference_update() (^=). Unlike |, &, -, and ^ which allocate new set objects, these update methods mutate the left-hand set directly in memory and return None—ideal for memory-efficient filtering over large streaming collections.
[HI] update() (|=) के अलावा सेट्स में intersection_update() (&=), difference_update() (-=) और symmetric_difference_update() (^=) मेथड्स भी होते हैं। |, &, -, ^ के विपरीत, ये नया सेट बनाने के बजाय मूल सेट को ही मेमोरी में इन-प्लेस बदलते हैं और None लौटाते हैं।
Syntax, Formula & Technical Example (Python Set Methods: add(), update(), remove(), discard(), pop())
s = {10, 20}
s.add(30) # {10, 20, 30}
s.update([30, 40], 'ab') # adds 40, 'a', 'b'
s.discard(999) # no error even though 999 is missing
s.remove(20) # removes 20; KeyError if missing
val = s.pop() # removes & returns one arbitrary element
print(val in {10, 30, 40, 'a', 'b'}, len(s)) # True 4
Python Set Methods: add(), update(), remove(), discard(), pop() Quick Reference & Comparison Table
| Set Method | Behavior When Missing / Argument Rule | Key Exam Fact |
|---|---|---|
| s.add(x) | Inserts single hashable item x; returns None | Duplicates ignored; unhashable x raises TypeError |
| s.update(it) | Unpacks iterable items into s; returns None | Accepts multiple iterables (list, str, set, tuple) |
| s.remove(x) | Raises KeyError if x is absent; returns None | Note KeyError (whereas list.remove raises ValueError) |
| s.discard(x) | Does nothing silently if x absent; returns None | Safe deletion without try/except KeyError |
Python Set Methods: add(), update(), remove(), discard(), pop() Top 20 Bilingual MCQs with Answers
Q1. Which exception does s.remove(99) raise when 99 is not in set s?
(जब सेट s में 99 मौजूद न हो, तो s.remove(99) कौन-सा एक्सेप्शन उत्पन्न करता है?)
(A) KeyError
(B) No error
(C) ValueError
(D) IndexError
Correct Answer: (A)
[EN] Unlike list.remove() which raises ValueError, set.remove() raises KeyError because sets share hash-key semantics.
[HI] list.remove() के विपरीत (जो ValueError देता है), set.remove() अनुपस्थित तत्व पर KeyError देता है।
Q2. What happens when you call s.discard(99) on s = {1, 2, 3}?
(s = {1, 2, 3} पर s.discard(99) चलाने से क्या होता है?)
(A) Deletes the last element
(B) Leaves s unchanged without raising any error and returns None
(C) Raises KeyError
(D) Raises ValueError
Correct Answer: (B)
[EN] discard() safely ignores missing elements without raising exceptions.
[HI] discard() अनुपस्थित तत्व को बिना किसी एरर के अनदेखा करता है और None लौटाता है।
Q3. What happens if you pass an index to set.pop(), e.g., {10, 20}.pop(0)?
({10, 20}.pop(0) की तरह सेट के pop() में इंडेक्स पास करने पर क्या होता है?)
(A) Raises TypeError
(B) Raises KeyError
(C) Removes 10
(D) Raises IndexError
Correct Answer: (A)
[EN] Sets are unindexed, so set.pop() takes zero arguments and passing 0 raises TypeError: set.pop() takes no arguments.
[HI] सेट अनइंडेक्स्ड होते हैं, अतः set.pop() कोई आर्गुमेंट नहीं लेता; 0 देने पर TypeError आता है।
Q4. What happens when calling set().pop() on an empty set?
(खाली सेट पर set().pop() चलाने से क्या होता है?)
(A) Returns None
(B) Raises ValueError
(C) Raises IndexError
(D) Raises KeyError
Correct Answer: (D)
[EN] Popping from an empty set raises KeyError: 'pop from an empty set'.
[HI] खाली सेट से pop() करने पर KeyError: 'pop from an empty set' त्रुटि आती है।
Q5. What is the output of s = {1, 2}; s.update('hi'); print(len(s))?
(s = {1, 2}; s.update('hi'); print(len(s)) का आउटपुट क्या होगा?)
(A) 2
(B) 4
(C) TypeError
(D) 3
Correct Answer: (B)
[EN] update() iterates over string 'hi' and adds 'h' and 'i' separately, making the set {1, 2, 'h', 'i'} of length 4.
[HI] update() स्ट्रिंग 'hi' के अक्षर 'h' और 'i' को अलग-अलग जोड़ता है, जिससे लंबाई 4 हो जाती है।
Q6. What is the output of s = {1, 2}; s.add('hi'); print(len(s))?
(s = {1, 2}; s.add('hi'); print(len(s)) का आउटपुट क्या होगा?)
(A) 2
(B) 3
(C) TypeError
(D) 4
Correct Answer: (B)
[EN] add('hi') inserts the whole immutable string 'hi' as a single hashable element, giving length 3.
[HI] add('hi') पूरी स्ट्रिंग 'hi' को एक एकल तत्व के रूप में जोड़ता है, जिससे लंबाई 3 होती है।
Q7. What does x = {1, 2}.add(3); print(x) output?
(x = {1, 2}.add(3); print(x) का आउटपुट क्या होगा?)
(A) 3
(B) {1, 2, 3}
(C) None
(D) True
Correct Answer: (C)
[EN] add() mutates the set in place and always returns None.
[HI] add() सेट को इन-प्लेस बदलता है और हमेशा None लौटाता है।
Q8. Which error occurs when running {1, 2}.add([3, 4])?
({1, 2}.add([3, 4]) चलाने पर कौन-सी त्रुटि आती है?)
(A) KeyError
(B) TypeError
(C) ValueError
(D) AttributeError
Correct Answer: (B)
[EN] Lists are unhashable, so passing a list to add() raises TypeError: unhashable type: 'list'.
[HI] लिस्ट अनहैशेबल होती है, इसलिए add() में लिस्ट देने पर TypeError आता है।
Q9. What happens when you run {1, 2}.update([3, 4])?
({1, 2}.update([3, 4]) चलाने पर क्या होता है?)
(A) Adds [3, 4] as a single nested list
(B) Adds 3 and 4 into the set (length becomes 4)
(C) Returns {1, 2, 3, 4} leaving original unchanged
(D) Raises TypeError: unhashable type: 'list'
Correct Answer: (B)
[EN] update() iterates over the list and inserts hashable elements 3 and 4 individually in place.
[HI] update() लिस्ट के भीतर के हैशेबल तत्वों 3 और 4 को एक-एक कर सेट में जोड़ देता है।
Q10. Which method empties all elements of a set in place while preserving its object id?
(कौन-सा मेथड सेट के ऑब्जेक्ट id को बदले बिना सभी तत्वों को इन-प्लेस खाली कर देता है?)
(A) discard()
(B) pop()
(C) remove()
(D) clear()
Correct Answer: (D)
[EN] s.clear() removes all items in place and keeps id(s) unchanged, unlike reassigning s = set().
[HI] s.clear() मूल सेट की id सुरक्षित रखते हुए सभी तत्व हटा देता है, जबकि s = set() नया ऑब्जेक्ट बनाता है।
Q11. What does s = {1, 2, 3}; s.difference_update({2, 4}); print(s) output?
(s = {1, 2, 3}; s.difference_update({2, 4}); print(s) का आउटपुट क्या होगा?)
(A) None
(B) {1, 2, 3}
(C) {1, 3}
(D) {4}
Correct Answer: (C)
[EN] difference_update() removes elements found in {2, 4} from s in place (equivalent to s -= {2, 4}), leaving {1, 3}.
[HI] difference_update() सेट s से {2, 4} के तत्व इन-प्लेस हटा देता है (s -= {2, 4}), जिससे {1, 3} बचता है।
Q12. What is the result of s = {1, 2, 3}; s.intersection_update([2, 3, 5]); print(s)?
(s = {1, 2, 3}; s.intersection_update([2, 3, 5]); print(s) का परिणाम क्या होगा?)
(A) {2, 3}
(B) {1, 2, 3, 5}
(C) TypeError
(D) {1}
Correct Answer: (A)
[EN] intersection_update() keeps only elements common with the given iterable in place, updating s to {2, 3}.
[HI] intersection_update() सेट s में केवल दोनों के उभयनिष्ठ तत्व {2, 3} को ही रखता है।
Q13. Which statement accurately compares remove(), discard(), and pop()?
(remove(), discard() और pop() की सही तुलना कौन-सी है?)
(A) remove() returns the removed item; pop() returns None
(B) pop() returns the removed item; remove() and discard() return None
(C) discard() raises KeyError on missing values
(D) All three return the removed item
Correct Answer: (B)
[EN] Only pop() returns the element it removed; remove() and discard() always return None.
[HI] केवल pop() हटाए गए तत्व को वापस लौटाता है; remove() और discard() हमेशा None लौटाते हैं।
Q14. What happens if you pass a non-iterable integer to update(), e.g., {1, 2}.update(3)?
({1, 2}.update(3) में नॉन-इटरेबल पूर्णांक देने पर क्या होता है?)
(A) Raises ValueError
(B) Adds 3 to the set
(C) Raises TypeError
(D) Does nothing
Correct Answer: (C)
[EN] update() expects one or more iterables and raises TypeError: 'int' object is not iterable when passed 3.
[HI] update() केवल इटरेबल स्वीकार करता है; पूर्णांक 3 देने पर TypeError आता है।
Q15. What does s = {1, 2}; s.update((3,), [4, 5]); print(len(s)) output?
(s = {1, 2}; s.update((3,), [4, 5]); print(len(s)) का आउटपुट क्या होगा?)
(A) 4
(B) TypeError
(C) 5
(D) 3
Correct Answer: (C)
[EN] update() accepts multiple iterable arguments at once, adding 3, 4, and 5 to {1, 2} for a total of 5 elements.
[HI] update() एक साथ कई इटरेबल ले सकता है, जिससे 3, 4 और 5 जुड़कर कुल 5 तत्व हो जाते हैं।
Q16. Which statement is NOT true about Python set mutation methods?
(पायथन सेट म्यूटेशन मेथड्स के बारे में कौन-सा कथन सत्य नहीं है?)
(A) s.discard(x) does not raise an error when x is absent
(B) s.add(x) adds a single hashable item
(C) s.remove(x) raises ValueError when x is absent from the set
(D) s.pop() takes no arguments
Correct Answer: (C)
[EN] s.remove(x) raises KeyError (not ValueError) when x is missing from a set; ValueError is raised by list.remove(x).
[HI] सेट में तत्व न होने पर s.remove(x) ValueError नहीं बल्कि KeyError देता है; ValueError लिस्ट में आता है।
Q17. Within Python Set Methods: add(), update(), remove(), discard(), pop(), which set method directly maps to: "Inserts single hashable item x; returns None"?
(Python Set Methods: add(), update(), remove(), discard(), pop() में कौन सा Set Method सीधे "Inserts single hashable item x; returns None" से संबंधित है?)
(A) s.add(x)
(B) s.discard(x)
(C) s.remove(x)
(D) s.update(it)
Correct Answer: (A)
[EN] In Python Set Methods: add(), update(), remove(), discard(), pop(), 's.add(x)' handles 'Inserts single hashable item x; returns None' (Duplicates ignored; unhashable x raises TypeError).
[HI] Python Set Methods: add(), update(), remove(), discard(), pop() में 's.add(x)' का कार्य/विवरण 'Inserts single hashable item x; returns None' (Duplicates ignored; unhashable x raises TypeError) है।
Q18. Which option accurately describes 's.update(it)' in Python Set Methods: add(), update(), remove(), discard(), pop()?
(Python Set Methods: add(), update(), remove(), discard(), pop() के संदर्भ में 's.update(it)' की सही विशेषता कौन सी है?)
(A) Raises KeyError if x is absent; returns None
(B) Unpacks iterable items into s; returns None
(C) Inserts single hashable item x; returns None
(D) Does nothing silently if x absent; returns None
Correct Answer: (B)
[EN] 's.update(it)' provides 'Unpacks iterable items into s; returns None' — key point: Accepts multiple iterables (list, str, set, tuple).
[HI] 's.update(it)' का मुख्य विवरण 'Unpacks iterable items into s; returns None' है (Accepts multiple iterables (list, str, set, tuple))।
Q19. In Python Set Methods: add(), update(), remove(), discard(), pop(), identify the correct parameter for the specification: "Raises KeyError if x is absent; returns None".
(Python Set Methods: add(), update(), remove(), discard(), pop() में "Raises KeyError if x is absent; returns None" विवरण किस विकल्प के लिए सही है?)
(A) s.remove(x)
(B) s.discard(x)
(C) s.update(it)
(D) s.add(x)
Correct Answer: (A)
[EN] 's.remove(x)' corresponds to 'Raises KeyError if x is absent; returns None' (Note KeyError (whereas list.remove raises ValueError)).
[HI] 's.remove(x)' का संबंध 'Raises KeyError if x is absent; returns None' (Note KeyError (whereas list.remove raises ValueError)) से है।
Q20. Considering Python Set Methods: add(), update(), remove(), discard(), pop(), what key behavior defines 's.discard(x)'?
(Python Set Methods: add(), update(), remove(), discard(), pop() में 's.discard(x)' की मुख्य भूमिका या मान क्या है?)
(A) Inserts single hashable item x; returns None
(B) Unpacks iterable items into s; returns None
(C) Does nothing silently if x absent; returns None
(D) Raises KeyError if x is absent; returns None
Correct Answer: (C)
[EN] 's.discard(x)' represents 'Does nothing silently if x absent; returns None' (Safe deletion without try/except KeyError).
[HI] 's.discard(x)' का अर्थ/मान 'Does nothing silently if x absent; returns None' है (Safe deletion without try/except KeyError)।
Python Set Methods: add(), update(), remove(), discard(), pop() — Exam FAQs
Q1: Why does list.remove() raise ValueError while set.remove() raises KeyError?
list.remove() ValueError क्यों देता है जबकि set.remove() KeyError क्यों देता है?
[EN] A set is implemented internally as a hash table of unique keys (essentially a dict without values). Just as looking up or deleting a missing key in a dict raises KeyError, removing a missing item from a set raises KeyError.
[HI] सेट आंतरिक रूप से अद्वितीय कीज़ (keys) की हैश टेबल के रूप में बना होता है (बिना वैल्यू वाली डिक्शनरी की तरह)। इसलिए अनुपस्थित तत्व हटाने पर डिक्शनरी की तरह ही KeyError आता है।
Q2: Does set.pop() always remove the smallest or first element?
क्या set.pop() हमेशा सबसे छोटा या पहला तत्व ही हटाता है?
[EN] No. set.pop() pops whatever element occupies the next occupied hash table bucket, which appears arbitrary and depends on hash values rather than numeric order or insertion order.
[HI] नहीं। set.pop() हैश टेबल के अगले भरे हुए बकेट से तत्व निकालता है, जो संख्यात्मक या डालने के क्रम के बजाय हैश मान पर निर्भर करता है।
Q3: Should I use s.add((1, 2)) or s.update((1, 2)) to add a coordinate tuple as one element?
निर्देशांक टपल (1, 2) को एक एकल तत्व के रूप में जोड़ने के लिए s.add((1, 2)) उपयोग करें या s.update((1, 2))?
[EN] Use s.add((1, 2)) so the tuple itself is stored as a single item {(1, 2)}. Calling s.update((1, 2)) unpacks the tuple and inserts 1 and 2 separately.
[HI] s.add((1, 2)) उपयोग करें ताकि पूरा टपल एक तत्व {(1, 2)} के रूप में जुड़े। s.update((1, 2)) करने से 1 और 2 अलग-अलग जुड़ जाएंगे।
Post a Comment
Post a Comment