शुभ दीपावली! NotesMedia की ओर से आपको और आपके परिवार को ढेरों शुभकामनाएं ✨ शुभ दीपावली! आपको और आपके परिवार को शुभकामनाएं ✨ शुभ दीपावली! ✨
BCA / B.Tech 6 min read

Set Operations in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Python Notes in Hindi | पायथन नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Set Operations in Hindi - General Topics
  • • अवलोकन: Quick Answer: Python में Set एक unordered, mutable और unindexed collection है जो केवल unique elements को store करता है। Set Operations गणितीय Set Theory...
  • • मुख्य अवधारणाएं: Python set operations, Union intersection difference symmetric Hindi, Issubset operations,Python set operations, Union intersection difference symmetric, Issubset operations
Quick Answer: Python में Set एक unordered, mutable और unindexed collection है जो केवल unique elements को store करता है। Set Operations गणितीय Set Theory (Venn Diagram) के सिद्धांतों पर आधारित होते हैं। Python में चार मुख्य set operations हैं: 1. Union (| या .union()): दोनों sets के सभी elements; 2. Intersection (& या .intersection()): दोनों sets में common elements; 3. Difference (- या .difference()): पहले set के वे elements जो दूसरे में नहीं हैं; और 4. Symmetric Difference (^ या .symmetric_difference()): दोनों sets के वे elements जो common नहीं हैं।

Python में Set क्या है और इसे कैसे बनाएं?

Python में List और Tuple की तुलना में Set बहुत तेज lookup speed (O(1) average time complexity, Dictionary की तरह) देता है क्योंकि यह आंतरिक रूप से Hash Table का use करता है। Python के अन्य data types को समझने के लिए data types in Python in Hindi पढ़ें।

Set को curly braces { } या set() constructor से बनाया जाता है:

# 1. Non-empty set
fruits = {"apple", "banana", "cherry", "apple"}
print(fruits)  # Output: {'apple', 'banana', 'cherry'} (Duplicates removed; order अलग हो सकता है)

# 2. Empty set (सबसे जरूरी नियम)
empty_set = set()      # सही तरीका: empty set
empty_dict = {}        # गलत तरीका: यह empty dictionary बनाता है!
Advertisement

Python के 4 मुख्य Set Operations (गणितीय संक्रियाएं)

Python में set operations को आप Operators (प्रतीकों) या Built-in Methods दोनों से कर सकते हैं। built-in methods in Python में इनके उपयोगी functions दिए गए हैं।

1. Union (संघ - | या union())

Union operation दो या अधिक sets के सभी unique elements को मिलाकर एक नया set लौटाता है।

set_a = {1, 2, 3}
set_b = {3, 4, 5}

# Operator से
print(set_a | set_b)          # Output: {1, 2, 3, 4, 5}

# Method से
print(set_a.union(set_b))     # Output: {1, 2, 3, 4, 5}

2. Intersection (उभयनिष्ठ - & या intersection())

Intersection केवल उन elements को चुनता है जो दोनों sets में common (मौजूद) होते हैं।

set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

print(set_a & set_b)                  # Output: {3, 4}
print(set_a.intersection(set_b))     # Output: {3, 4}

3. Difference (अंतर - - या difference())

Difference operation पहले set के उन elements को लौटाता है जो दूसरे set में मौजूद नहीं हैं। ध्यान रहे कि A - B और B - A का result अलग होता है:

set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

print(set_a - set_b)                  # Output: {1, 2} (A में हैं, B में नहीं)
print(set_b - set_a)                  # Output: {5, 6} (B में हैं, A में नहीं)
print(set_a.difference(set_b))        # Output: {1, 2}

4. Symmetric Difference (सममित अंतर - ^ या symmetric_difference())

यह उन सभी elements को लौटाता है जो या तो पहले set में हैं या दूसरे में हैं, लेकिन दोनों में common नहीं हैं (यानी Union minus Intersection):

set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

print(set_a ^ set_b)                          # Output: {1, 2, 5, 6}
print(set_a.symmetric_difference(set_b))     # Output: {1, 2, 5, 6}

गणितीय Set Operations की तुलनात्मक तालिका (Comparison Table)

Exam में quick revision के लिए चारों मुख्य operations का सारांश नीचे दिया गया है:

Operation Name Operator Method Syntax Venn Diagram अर्थ Code उदाहरण (A={1,2,3}, B={3,4}) Result Output
Union | A.union(B) A और B के सभी तत्व। A | B {1, 2, 3, 4}
Intersection & A.intersection(B) A और B दोनों में उभयनिष्ठ (Common) तत्व। A & B {3}
Difference - A.difference(B) केवल A के तत्व, B वाले हटाकर। A - B {1, 2}
Symmetric Difference ^ A.symmetric_difference(B) वे तत्व जो केवल एक में हैं (Common छोड़कर)। A ^ B {1, 2, 4}

Python Collections की तुलना: List, Tuple, Set, Dictionary

Python में विभिन्न data संग्रह प्रकारों में अंतर समझना exam के लिए बहुत महत्वपूर्ण है। List और Tuple के विवरण के लिए List and Tuples in Python पढ़ें:

Collection Type Order सुरक्षित? Mutable (परिवर्तनीय)? Duplicates अनुमति? Indexing & Slicing? आदर्श Use Case
List हाँ (Ordered) हाँ (Mutable) हाँ (Duplicates Allowed) हाँ (list[0]) सीक्वेंस data जहाँ क्रम महत्वपूर्ण हो।
Tuple हाँ (Ordered) नहीं (Immutable) हाँ (Duplicates Allowed) हाँ (tuple[0]) Read-only data और secure configuration.
Set नहीं (Unordered) हाँ (Mutable) नहीं (Unique Elements Only) नहीं (No Indexing) Duplicates हटाना और गणितीय तुलना।
Dictionary हाँ (Python 3.7+) हाँ (Mutable) Keys unique, Values duplicate हो सकती हैं। Key से (dict[key]) Key-Value pairs (जैसे छात्र का रोल नंबर व नाम)।

Relational & Membership Operations (Subset, Superset, Disjoint)

दो sets के बीच संबंध जांचने के लिए Python boolean (True/False) methods देता है:

  • issubset() (या <=): यदि Set A के सभी तत्व Set B में मौजूद हों, तो True देता है।
    {1, 2}.issubset({1, 2, 3}) → True
  • issuperset() (या >=): यदि Set A में Set B के सभी तत्व मौजूद हों (extra items होना जरूरी नहीं)।
    {1, 2, 3}.issuperset({1, 2}) → True
  • isdisjoint(): यदि दोनों sets में कोई भी तत्व common न हो (Intersection खाली हो), तो True देता है।
    {1, 2}.isdisjoint({3, 4}) → True

Complete Worked Program: कॉलेज स्टूडेंट कोर्स data का विश्लेषण

यह एक सम्पूर्ण और tested Python program है जो दो courses में enrolled students के data पर set operations लागू करता है:

# Real-World Scenario: College Course Enrollment Analysis

# 1. Students enrolled in Python Course
python_students = {"Rahul", "Priya", "Amit", "Sneha", "Karan"}

# 2. Students enrolled in Data Science Course
datascience_students = {"Amit", "Sneha", "Vikas", "Pooja", "Anjali"}

print("=== College Enrollment Analysis ===")
print("Python Students:", python_students)
print("Data Science Students:", datascience_students)

# Operation 1: All unique students across both courses (Union)
all_students = python_students | datascience_students
print("\n1. Total Unique Students Enrolled (Union):")
print(all_students)
print("Total Count:", len(all_students))

# Operation 2: Students enrolled in BOTH courses (Intersection)
both_courses = python_students & datascience_students
print("\n2. Students Enrolled in BOTH Courses (Intersection):")
print(both_courses)

# Operation 3: Students enrolled ONLY in Python (Difference)
only_python = python_students - datascience_students
print("\n3. Students Enrolled ONLY in Python (Difference):")
print(only_python)

# Operation 4: Students enrolled in EXACTLY ONE course (Symmetric Difference)
single_course_only = python_students ^ datascience_students
print("\n4. Students in EXACTLY ONE Course (Symmetric Difference):")
print(single_course_only)

# Operation 5: Checking Subsets
scholarship_group = {"Amit", "Sneha"}
is_subset = scholarship_group.issubset(python_students)
print("\n5. Is Scholarship Group entirely in Python Course?", is_subset)
Advertisement

Program Output:

=== College Enrollment Analysis ===
Python Students: {'Rahul', 'Sneha', 'Amit', 'Priya', 'Karan'}
Data Science Students: {'Vikas', 'Sneha', 'Pooja', 'Anjali', 'Amit'}

1. Total Unique Students Enrolled (Union):
{'Rahul', 'Vikas', 'Sneha', 'Pooja', 'Anjali', 'Amit', 'Priya', 'Karan'}
Total Count: 8

2. Students Enrolled in BOTH Courses (Intersection):
{'Sneha', 'Amit'}

3. Students Enrolled ONLY in Python (Difference):
{'Rahul', 'Priya', 'Karan'}

4. Students in EXACTLY ONE Course (Symmetric Difference):
{'Rahul', 'Vikas', 'Pooja', 'Anjali', 'Priya', 'Karan'}

5. Is Scholarship Group entirely in Python Course? True

Note: Set unordered होता है, इसलिए आपके computer पर names का order ऊपर से अलग दिख सकता है (string hashing हर run में बदल सकती है)। Elements और Count वही रहेंगे।

विद्यार्थी अक्सर क्या गलतियां करते हैं? (Common Student Mistakes)

  • खाली सेट बनाने में गलती: सबसे सामान्य गलती s = {} लिखना है। Python इसे खाली Dictionary (dict) मानता है, Set नहीं। खाली सेट बनाने के लिए हमेशा s = set() लिखना चाहिए।
  • Set में Indexing का प्रयास करना: Students my_set[0] लिखकर पहला तत्व निकालने की कोशिश करते हैं। चूंकि Set पूरी तरह unindexed और unordered होता है, इसलिए Python TypeError: 'set' object is not subscriptable error फेंकता है। elements को access करने के लिए for loop या in operator का use करें।
  • Mutable Objects को Set में डालना: Set केवल Hashable (Immutable) data को ही स्वीकार करता है (जैसे int, float, string, tuple)। यदि आप set के अंदर List या Dictionary डालने का प्रयास करते हैं (जैसे {[1, 2], 3}), तो Python TypeError: unhashable type: 'list' error देता है।
  • Difference में क्रम का ध्यान न रखना: A - B और B - A दोनों अलग परिणाम देते हैं। A - B का अर्थ है A के वे तत्व जो B में नहीं हैं; जबकि B - A का अर्थ है B के वे तत्व जो A में नहीं हैं।

Exam के लिए Important Questions

Short Questions (2 से 5 अंक)

  1. Python में Set क्या है? इसकी definition लिखिए।
  2. Python में खाली Set और खाली Dictionary बनाने के syntax में क्या अंतर है?
  3. Intersection और Symmetric Difference में क्या मुख्य अंतर होता है?
  4. Set में List को element के रूप में क्यों नहीं जोड़ा जा सकता?
  5. isdisjoint() method क्या काम करता है? एक छोटा उदाहरण दीजिए।

Long Questions (10 से 20 अंक)

  1. Python में Set Operations (Union, Intersection, Difference, Symmetric Difference) की detail व्याख्या कीजिए। प्रत्येक के लिए Operator और Method syntax उदाहरण के साथ समझाइए।
  2. Python के विभिन्न Data Collections (List, Tuple, Set, Dictionary) का तुलनात्मक अध्ययन प्रस्तुत कीजिए। Mutability, indexing और use case के आधार पर अंतर स्पष्ट कीजिए।
  3. एक complete Python program लिखिए जो दो अलग-अलग lists से data पढ़कर common और unique elements का विश्लेषण set operations से करता हो। अधिक प्रश्नों के लिए BCA Python MDSU Exam Paper देखें।

FAQs

Q1: क्या Set में Duplicate वैल्यूज डालने पर कोई error आती है?

नहीं, Python कोई error नहीं देता बल्कि बिना किसी चेतावनी के डुप्लीकेट वैल्यूज को अपने आप discard कर देता है और केवल एक कॉपी रखता है।

Q2: Python में Frozenset क्या होता है?

frozenset एक Immutable (अपरिवर्तनीय) सेट होता है। एक बार बनने के बाद इसमें न तो नए तत्व जोड़े जा सकते हैं और न ही हटाए जा सकते हैं। फ्रोजनसेट hashable होता है, इसलिए इसे Dictionary की Key या दूसरे Set के अंदर element के रूप में use किया जा सकता है।

Q3: remove() और discard() में क्या अंतर है?

दोनों सेट से element हटाने का काम करते हैं। अंतर यह है कि यदि element सेट में मौजूद नहीं है, तो remove() एक KeyError error फेंकता है, जबकि discard() चुपचाप बिना कोई error दिए आगे बढ़ जाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Python में Set एक unordered, mutable और unindexed collection है जो केवल unique elements को store करता है।
Python Notes in Hindi | पायथन नोट्स हिंदी में के पाठ्यक्रम में Set Operations in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Set Operations in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement