Python Set Operations: Union (|), Intersection (&), Difference (-) Notes in Hindi & English : सेट ऑपरेशन्स: यूनियन (|), इंटरसेक्शन (&) और सिमेट्रिक डिफरेंस (^) और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Set Operations: Union (|), Intersection (&), Difference (-) (सेट ऑपरेशन्स: यूनियन (|), इंटरसेक्शन (&) और सिमेट्रिक डिफरेंस (^)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Python sets support mathematical operations including union (|), intersection (&), difference (-), and symmetric difference (^), each returning a new set without mutating operands. While binary operators require set or frozenset operands, named methods like .union() and .intersection() accept any iterable.

अध्याय सार: पायथन सेट्स गणितीय ऑपरेशन्स जैसे यूनियन (|), इंटरसेक्शन (&), डिफरेंस (-) और सिमेट्रिक डिफरेंस (^) का समर्थन करते हैं, जो मूल सेट बदले बिना नया सेट लौटाते हैं। बाइनरी ऑपरेटर्स के लिए दोनों ओर सेट होना जरूरी है, जबकि मेथड रूप (.union(), .intersection()) किसी भी इटरेबल पर काम करते हैं।

Python Set Operations: Union (|), Intersection (&), Difference (-): Detailed Notes & Architecture (सेट ऑपरेशन्स: यूनियन (|), इंटरसेक्शन (&) और सिमेट्रिक डिफरेंस (^))

1. Union (|) and Intersection (&) (यूनियन (|) और इंटरसेक्शन (&))

[EN] Union (A | B or A.union(B)) combines all distinct elements present in A, B, or both, executing in O(len(A) + len(B)) average time. Intersection (A & B or A.intersection(B)) returns only elements common to both A and B. CPython optimizes intersection by iterating over the smaller set and probing the larger hash table, achieving O(min(len(A), len(B))) average time complexity.

[HI] यूनियन (A | B या A.union(B)) दोनों सेट्स के सभी अद्वितीय तत्वों को मिलाकर O(len(A) + len(B)) समय में नया सेट देता है। इंटरसेक्शन (A & B या A.intersection(B)) केवल दोनों सेट्स में उभयनिष्ठ (common) तत्व लौटाता है। CPython इंटरसेक्शन में छोटे सेट पर लूप चलाकर बड़े हैश टेबल में जांच करता है, जिससे औसत समय जटिलता O(min(len(A), len(B))) रहती है।

2. Difference (-) and Asymmetric Behavior (डिफरेंस (-) और असममित व्यवहार)

[EN] Set difference (A - B or A.difference(B)) returns elements present in A that do NOT appear in B, running in O(len(A)) average time. Unlike union, intersection, and symmetric difference—which are commutative—set difference is asymmetric: A - B is generally not equal to B - A unless A == B. Note that calling A + B on sets raises TypeError because + is unsupported on sets.

[HI] सेट डिफरेंस (A - B या A.difference(B)) वे तत्व लौटाता है जो A में हैं लेकिन B में नहीं हैं (O(len(A)) समय)। यूनियन, इंटरसेक्शन और सिमेट्रिक डिफरेंस के विपरीत, डिफरेंस क्रम-विनिमेय (commutative) नहीं है—अर्थात सामान्यतः A - B और B - A अलग होते हैं। ध्यान रहे, सेट्स पर + ऑपरेटर (A + B) समर्थित नहीं है और TypeError देता है।

3. Symmetric Difference (^) (सिमेट्रिक डिफरेंस (^))

[EN] Symmetric difference (A ^ B or A.symmetric_difference(B)) yields elements that belong to either A or B, but NOT to both simultaneously—mathematically equivalent to (A | B) - (A & B) or (A - B) | (B - A). It is commutative (A ^ B == B ^ A) and runs in O(len(A) + len(B)) average time. Think of it as set-level exclusive OR (XOR).

[HI] सिमेट्रिक डिफरेंस (A ^ B या A.symmetric_difference(B)) उन तत्वों को लौटाता है जो या तो A में हैं या B में, परंतु दोनों में उभयनिष्ठ नहीं हैं—यह गणितीय रूप से (A | B) - (A & B) के बराबर है। यह क्रम-विनिमेय (A ^ B == B ^ A) है और सेट स्तर पर एक्सक्लूसिव OR (XOR) जैसा काम करता है।

4. Operators vs Methods and Subset/Disjoint Relations (ऑपरेटर्स बनाम मेथड्स तथा सबसेट/डिसजॉइंट संबंध)

[EN] A subtle Python rule distinguishes operators from methods: symbolic operators (|, &, -, ^) require BOTH operands to be set or frozenset objects (e.g., {1, 2} | [2, 3] raises TypeError), whereas named methods ({1, 2}.union([2, 3])) accept any iterable such as lists, tuples, or strings! Python also supports relational checks: A <= B (issubset), A < B (proper subset), A >= B (issuperset), and A.isdisjoint(B) (True when A & B is empty).

[HI] ऑपरेटर्स और मेथड्स में एक महत्वपूर्ण अंतर है: सिंबॉलिक ऑपरेटर्स (|, &, -, ^) में दोनों तरफ set या frozenset होना जरूरी है (जैसे {1, 2} | [2, 3] पर TypeError आता है), जबकि मेथड्स (.union([2, 3])) लिस्ट, टपल या स्ट्रिंग जैसे किसी भी इटरेबल को स्वीकार कर लेते हैं। इसके अलावा A <= B (सबसेट), A < B (प्रॉपर सबसेट), A >= B (सुपरसेट) और A.isdisjoint(B) भी उपयोगी हैं।

Syntax, Formula & Technical Example (Python Set Operations: Union (|), Intersection (&), Difference (-))

A = {1, 2, 3, 4}
B = {3, 4, 5}
print(A | B)                # {1, 2, 3, 4, 5}
print(A & B)                # {3, 4}
print(A - B, B - A)         # {1, 2} {5}
print(A ^ B)                # {1, 2, 5}
print(A.union([5, 6]))      # {1, 2, 3, 4, 5, 6} (method accepts list)
print({1, 2}.issubset(A))   # True

Python Set Operations: Union (|), Intersection (&), Difference (-) Quick Reference & Comparison Table

Set Operation (A={1,2,3}, B={3,4})Result SetKey Exam Fact
A | B (union){1, 2, 3, 4}All unique elements in A or B; O(len(A)+len(B))
A & B (intersection){3}Elements common to both; O(min(len(A), len(B)))
A - B (difference){1, 2}Elements in A but not B; asymmetric (A-B != B-A)
A ^ B (symmetric_difference){1, 2, 4}Elements in A or B but not both; (A|B)-(A&B)

Python Set Operations: Union (|), Intersection (&), Difference (-) Top 20 Bilingual MCQs with Answers

Q1. For A = {1, 2, 3} and B = {2, 3, 4}, what is A ^ B?

(A = {1, 2, 3} और B = {2, 3, 4} के लिए A ^ B क्या होगा?)

(A) {1}

(B) {1, 2, 3, 4}

(C) {1, 4}

(D) {2, 3}

Correct Answer: (C)

[EN] ^ computes symmetric difference: items in A or B excluding common elements {2, 3}, giving {1, 4}.

[HI] ^ सिमेट्रिक डिफरेंस निकालता है: A या B में मौजूद तत्व जिनमें उभयनिष्ठ {2, 3} शामिल न हों, अतः {1, 4} मिलता है।

Q2. What happens when you execute {1, 2} | [2, 3] in Python?

(पायथन में {1, 2} | [2, 3] चलाने पर क्या होता है?)

(A) {1, 2, 3}

(B) Raises TypeError

(C) [1, 2, 3]

(D) {1}

Correct Answer: (B)

[EN] The binary | operator requires both operands to be sets (or frozensets), unlike .union() which accepts any iterable.

[HI] बाइनरी | ऑपरेटर दोनों तरफ सेट मांगता है, इसलिए लिस्ट देने पर TypeError आता है (जबकि .union() लिस्ट स्वीकार कर लेता है)।

Q3. What does {1, 2, 3}.intersection([2, 3, 4], (3, 4, 5)) return?

({1, 2, 3}.intersection([2, 3, 4], (3, 4, 5)) क्या लौटाता है?)

(A) {3, 4}

(B) TypeError

(C) {2, 3}

(D) {3}

Correct Answer: (D)

[EN] Named set methods accept multiple iterables of any type (list, tuple, etc.) and intersect all of them, yielding {3}.

[HI] नेम्ड सेट मेथड्स एक साथ कई लिस्ट या टपल जैसे इटरेबल स्वीकार कर सकते हैं और सभी का इंटरसेक्शन {3} लौटाते हैं।

Q4. What happens when running {1, 2} + {3, 4} in Python?

(पायथन में {1, 2} + {3, 4} चलाने पर क्या होता है?)

(A) {1, 2, 3, 4}

(B) SyntaxError

(C) Raises TypeError

(D) {1, 2}

Correct Answer: (C)

[EN] The + operator is not defined for set objects; union requires the | operator or .union().

[HI] सेट्स के लिए + ऑपरेटर परिभाषित नहीं है और TypeError देता है; जोड़ने के लिए | या .union() उपयोग करें।

Q5. For A = {10, 20, 30} and B = {20, 30, 40}, what is (A - B) | (B - A)?

(A = {10, 20, 30} और B = {20, 30, 40} के लिए (A - B) | (B - A) का मान क्या होगा?)

(A) {10, 20, 30, 40}

(B) {10}

(C) {10, 40}

(D) {20, 30}

Correct Answer: (C)

[EN] A - B is {10} and B - A is {40}; their union equals symmetric difference A ^ B = {10, 40}.

[HI] A - B = {10} और B - A = {40} का यूनियन सिमेट्रिक डिफरेंस A ^ B = {10, 40} के बराबर होता है।

Q6. What is the average time complexity of A & B (set intersection) in CPython?

(CPython में A & B (सेट इंटरसेक्शन) की औसत समय जटिलता क्या होती है?)

(A) O(n log n)

(B) O(min(len(A), len(B)))

(C) O(len(A) * len(B))

(D) O(1)

Correct Answer: (B)

[EN] CPython iterates through the smaller set and performs O(1) hash probes against the larger set.

[HI] CPython छोटे सेट पर इटरेट करके बड़े सेट में O(1) हैश जांच करता है, अतः जटिलता O(min(len(A), len(B))) होती है।

Q7. What does {1, 2}.isdisjoint({3, 4}) return?

({1, 2}.isdisjoint({3, 4}) क्या लौटाता है?)

(A) False

(B) None

(C) set()

(D) True

Correct Answer: (D)

[EN] isdisjoint() returns True when the two sets have zero elements in common (their intersection is empty).

[HI] जब दो सेट्स में कोई भी उभयनिष्ठ तत्व न हो (इंटरसेक्शन खाली हो), तब isdisjoint() True लौटाता है।

Q8. Which expression checks if A is a PROPER subset of B (A is inside B and A != B)?

(यह जांचने का सही एक्सप्रेशन कौन-सा है कि A, B का प्रॉपर सबसेट है (A != B)?)

(A) A == B

(B) A <= B

(C) A.issubset(B)

(D) A < B

Correct Answer: (D)

[EN] A < B returns True only if all elements of A are in B and len(A) < len(B); <= and issubset() return True even when A == B.

[HI] A < B तभी True होता है जब A, B का सबसेट हो और दोनों बराबर न हों; <= और issubset() बराबर होने पर भी True देते हैं।

Q9. If A = {1, 2} and B = {1, 2}, what is A - B?

(यदि A = {1, 2} और B = {1, 2} हो, तो A - B क्या होगा?)

(A) {}

(B) {0}

(C) None

(D) set()

Correct Answer: (D)

[EN] Removing all elements of B from identical set A yields an empty set, displayed as set() in Python.

[HI] समान सेट घटाने पर खाली सेट बनता है, जिसे पायथन में set() लिखा जाता है (न कि {})।

Q10. Which of the following set operations is NOT commutative (order of A and B matters)?

(निम्न में से कौन-सा सेट ऑपरेशन क्रम-विनिमेय (commutative) नहीं है?)

(A) Symmetric Difference (^)

(B) Union (|)

(C) Difference (-)

(D) Intersection (&)

Correct Answer: (C)

[EN] A - B returns items only in A, while B - A returns items only in B, so order changes the outcome.

[HI] डिफरेंस (-) क्रम पर निर्भर करता है: A - B केवल A के विशिष्ट तत्व देता है जबकि B - A केवल B के।

Q11. What is the result of {1, 2, 3} & frozenset([2, 3, 4])?

({1, 2, 3} & frozenset([2, 3, 4]) का परिणाम क्या होगा?)

(A) TypeError

(B) {1, 4}

(C) frozenset({2, 3})

(D) {2, 3}

Correct Answer: (D)

[EN] Set operators work seamlessly between set and frozenset; the returned type matches the left operand (here, set).

[HI] set और frozenset के बीच सेट ऑपरेटर काम करते हैं और परिणाम बाएं ऑपरेंड (यहाँ set) के प्रकार का होता है।

Q12. What does A = {1, 2}; B = {2, 3}; C = A | B leave inside A?

(A = {1, 2}; B = {2, 3}; C = A | B के बाद A में क्या रहेगा?)

(A) {1, 2, 3}

(B) {1, 2}

(C) None

(D) {2}

Correct Answer: (B)

[EN] The | operator returns a new set C without mutating A or B.

[HI] | ऑपरेटर नया सेट C लौटाता है; यह मूल सेट A या B में कोई बदलाव नहीं करता।

Q13. What is the output of print({1, 2, 3} - {3, 4} - {1})?

(print({1, 2, 3} - {3, 4} - {1}) का आउटपुट क्या होगा?)

(A) {2}

(B) {2, 4}

(C) set()

(D) {1, 2}

Correct Answer: (A)

[EN] Difference evaluates left-to-right: ({1, 2, 3} - {3, 4}) is {1, 2}, then {1, 2} - {1} leaves {2}.

[HI] बाएं से दाएं गणना करने पर पहले {1, 2} बनता है और फिर {1} घटने पर {2} बचता है।

Q14. Which identity always holds true for any two sets A and B?

(किन्हीं भी दो सेट्स A और B के लिए कौन-सी सर्वसमिका हमेशा सत्य होती है?)

(A) len(A ^ B) == len(A) + len(B)

(B) len(A - B) == len(B - A)

(C) len(A | B) == len(A) + len(B)

(D) len(A | B) == len(A) + len(B) - len(A & B)

Correct Answer: (D)

[EN] By the inclusion-exclusion principle, subtracting the intersection count avoids double-counting shared items.

[HI] समावेशन-अपवर्जन सिद्धांत (inclusion-exclusion) के अनुसार यूनियन के आकार से उभयनिष्ठ तत्वों की संख्या घटानी होती है।

Q15. What does A.issuperset(B) check?

(A.issuperset(B) क्या जांचता है?)

(A) Whether A and B have no common elements

(B) Whether every element of A is contained in B

(C) Whether every element of B is contained in A

(D) Whether len(A) > len(B) strictly

Correct Answer: (C)

[EN] A is a superset of B (A >= B) if all elements in B also belong to A.

[HI] यदि B के सभी तत्व A के भीतर मौजूद हों, तो A, B का सुपरसेट कहलाता है (A >= B)।

Q16. Which statement is NOT true about Python set operations?

(पायथन सेट ऑपरेशन्स के बारे में कौन-सा कथन सत्य नहीं है?)

(A) A.union([3, 4]) accepts a list argument

(B) A ^ B finds elements in A or B but not both

(C) A & B returns a new set of common elements

(D) The | operator can directly combine a set with a list without converting the list

Correct Answer: (D)

[EN] Symbolic operators like | strictly require set/frozenset operands on both sides; only methods like .union() accept lists directly.

[HI] | ऑपरेटर सीधे सेट और लिस्ट को नहीं जोड़ सकता (TypeError आएगा); केवल .union() मेथड ही लिस्ट स्वीकार करता है।

Q17. Within Python Set Operations: Union (|), Intersection (&), Difference (-), which set operation (a={1,2,3}, b={3,4}) directly maps to: "{1, 2, 3, 4}"?

(Python Set Operations: Union (|), Intersection (&), Difference (-) में कौन सा Set Operation (A={1,2,3}, B={3,4}) सीधे "{1, 2, 3, 4}" से संबंधित है?)

(A) A - B (difference)

(B) A | B (union)

(C) A & B (intersection)

(D) A ^ B (symmetric_difference)

Correct Answer: (B)

[EN] In Python Set Operations: Union (|), Intersection (&), Difference (-), 'A | B (union)' handles '{1, 2, 3, 4}' (All unique elements in A or B; O(len(A)+len(B))).

[HI] Python Set Operations: Union (|), Intersection (&), Difference (-) में 'A | B (union)' का कार्य/विवरण '{1, 2, 3, 4}' (All unique elements in A or B; O(len(A)+len(B))) है।

Q18. Which option accurately describes 'A & B (intersection)' in Python Set Operations: Union (|), Intersection (&), Difference (-)?

(Python Set Operations: Union (|), Intersection (&), Difference (-) के संदर्भ में 'A & B (intersection)' की सही विशेषता कौन सी है?)

(A) {1, 2, 4}

(B) {3}

(C) {1, 2, 3, 4}

(D) {1, 2}

Correct Answer: (B)

[EN] 'A & B (intersection)' provides '{3}' — key point: Elements common to both; O(min(len(A), len(B))).

[HI] 'A & B (intersection)' का मुख्य विवरण '{3}' है (Elements common to both; O(min(len(A), len(B))))।

Q19. In Python Set Operations: Union (|), Intersection (&), Difference (-), identify the correct parameter for the specification: "{1, 2}".

(Python Set Operations: Union (|), Intersection (&), Difference (-) में "{1, 2}" विवरण किस विकल्प के लिए सही है?)

(A) A - B (difference)

(B) A ^ B (symmetric_difference)

(C) A & B (intersection)

(D) A | B (union)

Correct Answer: (A)

[EN] 'A - B (difference)' corresponds to '{1, 2}' (Elements in A but not B; asymmetric (A-B != B-A)).

[HI] 'A - B (difference)' का संबंध '{1, 2}' (Elements in A but not B; asymmetric (A-B != B-A)) से है।

Q20. Considering Python Set Operations: Union (|), Intersection (&), Difference (-), what key behavior defines 'A ^ B (symmetric_difference)'?

(Python Set Operations: Union (|), Intersection (&), Difference (-) में 'A ^ B (symmetric_difference)' की मुख्य भूमिका या मान क्या है?)

(A) {1, 2}

(B) {1, 2, 3, 4}

(C) {1, 2, 4}

(D) {3}

Correct Answer: (C)

[EN] 'A ^ B (symmetric_difference)' represents '{1, 2, 4}' (Elements in A or B but not both; (A|B)-(A&B)).

[HI] 'A ^ B (symmetric_difference)' का अर्थ/मान '{1, 2, 4}' है (Elements in A or B but not both; (A|B)-(A&B))।

Python Set Operations: Union (|), Intersection (&), Difference (-) — Exam FAQs

Q1: When should I use A.union(B) instead of A | B?

मुझे A | B के बजाय A.union(B) का उपयोग कब करना चाहिए?

[EN] Use A.union(other) when other is a list, tuple, generator, or dictionary keys view, because the method converts any iterable automatically whereas | raises TypeError unless both operands are sets.

[HI] जब दूसरा डेटा सेट न होकर लिस्ट, टपल या जनरेटर हो तब A.union(other) का उपयोग करें, क्योंकि यह किसी भी इटरेबल को स्वीकार करता है जबकि | केवल सेट ही लेता है।

Q2: How does symmetric difference (^) differ from regular difference (-)?

सिमेट्रिक डिफरेंस (^) सामान्य डिफरेंस (-) से कैसे भिन्न है?

[EN] Regular difference A - B keeps items unique to A only (dropping B-only items). Symmetric difference A ^ B keeps items unique to A AND items unique to B, dropping only the overlap.

[HI] सामान्य डिफरेंस A - B केवल A के विशिष्ट तत्व रखता है। सिमेट्रिक डिफरेंस A ^ B दोनों सेट्स के विशिष्ट तत्व रखता है और केवल उभयनिष्ठ (common) तत्वों को हटाता है।

Q3: Do set operations like |, &, -, and ^ modify the original sets?

क्या |, &, -, और ^ जैसे सेट ऑपरेटर मूल सेट्स को बदलते हैं?

[EN] No. All four operators create and return a brand-new set. To mutate set A in place, use |= (update), &= (intersection_update), -= (difference_update), or ^= (symmetric_difference_update).

[HI] नहीं। ये चारों ऑपरेटर हमेशा एक नया सेट लौटाते हैं। मूल सेट को इन-प्लेस बदलने के लिए |=, &=, -= या ^= उपयोग करें।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)