Python Set क्या है? Set Methods & Operations in Hindi | Union, Intersection, Difference | RBSE Class 12

इस लेख में: Python Set क्या है, Set कैसे create करते हैं, duplicate values, add(), update(), remove(), discard(), pop(), clear() जैसे important methods तथा Union, Intersection, Difference और Symmetric Difference जैसे Set Operations को आसान Hindi में examples के साथ समझेंगे।

Python Set क्या है?

Set Python का एक built-in collection data type है जिसका उपयोग unique elements का collection store करने के लिए किया जाता है।

Set की सबसे महत्वपूर्ण विशेषता यह है कि इसमें सामान्यतः duplicate elements नहीं रहते। Set में membership testing और mathematical set operations जैसे union, intersection और difference आसानी से किए जा सकते हैं।

Example

numbers = {10, 20, 30, 40}

print(numbers)

Output:

{10, 20, 30, 40}
Important: Set को curly brackets { } से लिखा जाता है। लेकिन empty set बनाने के लिए {} का उपयोग नहीं किया जाता। Empty set बनाने के लिए set() लिखना होता है।

Python Set की मुख्य विशेषताएँ

  • Set unique elements का collection है।
  • Duplicate values को एक बार ही रखा जाता है।
  • Set indexing और slicing को support नहीं करता।
  • Set में membership testing आसानी से की जा सकती है।
  • Set में elements add और remove किए जा सकते हैं।
  • Set operations जैसे Union, Intersection और Difference उपलब्ध हैं।

Python में Set कैसे बनाते हैं?

fruits = {"Apple", "Banana", "Mango"}

print(fruits)

Output:

{'Apple', 'Banana', 'Mango'}

Set के elements का display order हमेशा उसी क्रम में दिखाई दे, यह मानकर नहीं चलना चाहिए।

Set में Duplicate Values

यदि Set में duplicate values दी जाती हैं तो duplicate values एक ही element के रूप में रहती हैं।

numbers = {10, 20, 10, 30, 20, 40}

print(numbers)

Output का एक संभावित रूप:

{10, 20, 30, 40}
Exam Tip: Set का उपयोग unique values रखने के लिए बहुत उपयोगी है।

Empty Set कैसे बनाते हैं?

Python में empty set बनाने के लिए set() का उपयोग करना चाहिए।

my_set = set()

print(my_set)
print(type(my_set))

Output:

set()
<class 'set'>

Empty Set में {} क्यों नहीं?

my_set = {}

print(type(my_set))

Output:

<class 'dict'>

Python में {} एक empty dictionary को represent करता है, empty set को नहीं।

Set में अलग-अलग Data Types

data = {10, "Python", 5.5, True}

print(data)

Set में hashable objects रखे जा सकते हैं। Practical programs में आमतौर पर numbers, strings आदि का उपयोग किया जाता है।

Set में Indexing क्यों नहीं होती?

Set में elements को index number से access नहीं किया जाता। इसलिए list या tuple की तरह my_set[0] लिखकर element access करना valid तरीका नहीं है।

numbers = {10, 20, 30}

# numbers[0]
Important: List और Tuple में indexing होती है, लेकिन Set में indexing और slicing नहीं होती।

Set पर for Loop

Set के elements को loop की सहायता से process किया जा सकता है।

fruits = {"Apple", "Banana", "Mango"}

for fruit in fruits:
    print(fruit)

Elements का output order निश्चित मानकर नहीं चलना चाहिए।

Set में Element Check करना

किसी element के Set में मौजूद होने की जाँच के लिए in operator का उपयोग किया जा सकता है।

fruits = {"Apple", "Banana", "Mango"}

if "Mango" in fruits:
    print("Mango is present")

Output:

Mango is present

Set में नया Element जोड़ना — add()

add() method से Set में एक element जोड़ा जा सकता है।

fruits = {"Apple", "Banana"}

fruits.add("Mango")

print(fruits)

Output का एक संभावित रूप:

{'Apple', 'Banana', 'Mango'}

Set में Multiple Elements जोड़ना — update()

update() method की सहायता से एक iterable के multiple elements को Set में जोड़ा जा सकता है।

fruits = {"Apple", "Banana"}

fruits.update(["Mango", "Orange"])

print(fruits)

Output का एक संभावित रूप:

{'Apple', 'Banana', 'Mango', 'Orange'}

remove() Method

remove() specified element को Set से हटाता है। यदि specified element मौजूद नहीं है तो error हो सकता है।

fruits = {"Apple", "Banana", "Mango"}

fruits.remove("Banana")

print(fruits)

discard() Method

discard() भी specified element को हटाता है। यदि element मौजूद नहीं है तो discard() सामान्यतः error नहीं देता।

fruits = {"Apple", "Banana", "Mango"}

fruits.discard("Banana")

print(fruits)

remove() और discard() में अंतर

remove() discard()
Specified element remove करता है। Specified element remove करता है।
Element मौजूद न होने पर KeyError हो सकता है। Element मौजूद न होने पर सामान्यतः error नहीं देता।
Exam Question: remove() और discard() में मुख्य अंतर लिखिए।

pop() Method

pop() Set से एक element remove करके उसे return करता है। Set में indexing नहीं होने के कारण pop() में index देकर किसी particular element को remove नहीं किया जाता।

numbers = {10, 20, 30, 40}

item = numbers.pop()

print("Removed:", item)
print(numbers)

कौन-सा element remove होगा, इसे fixed index की तरह assume नहीं करना चाहिए।

clear() Method

clear() Set के सभी elements को remove कर देता है।

numbers = {10, 20, 30}

numbers.clear()

print(numbers)

Output:

set()

Set की Length

Set में कितने elements हैं यह जानने के लिए len() function का उपयोग किया जा सकता है।

numbers = {10, 20, 30, 40}

print(len(numbers))

Output:

4

Set Union

Union दो Sets के सभी unique elements को combine करता है।

union() Method

A = {1, 2, 3}
B = {3, 4, 5}

C = A.union(B)

print(C)

Output का एक संभावित रूप:

{1, 2, 3, 4, 5}

Union Operator |

A = {1, 2, 3}
B = {3, 4, 5}

print(A | B)

Output का एक संभावित रूप:

{1, 2, 3, 4, 5}

Set Intersection

Intersection दो Sets में common elements को प्राप्त करता है।

intersection() Method

A = {1, 2, 3}
B = {2, 3, 4}

print(A.intersection(B))

Output:

{2, 3}

Intersection Operator &

A = {1, 2, 3}
B = {2, 3, 4}

print(A & B)

Output:

{2, 3}

Set Difference

Difference पहले Set में मौजूद लेकिन दूसरे Set में मौजूद नहीं elements को प्राप्त करता है।

A = {1, 2, 3, 4}
B = {3, 4, 5}

print(A.difference(B))

Output:

{1, 2}

Difference Operator -

A = {1, 2, 3, 4}
B = {3, 4, 5}

print(A - B)

Output:

{1, 2}

Symmetric Difference

Symmetric Difference उन elements को देता है जो किसी एक Set में हैं लेकिन दोनों Sets में common नहीं हैं।

A = {1, 2, 3}
B = {3, 4, 5}

print(A.symmetric_difference(B))

Output:

{1, 2, 4, 5}

Symmetric Difference Operator ^

A = {1, 2, 3}
B = {3, 4, 5}

print(A ^ B)

Output:

{1, 2, 4, 5}

Set Operations — Quick Table

Operation Method Operator Meaning
Union union() | दोनों Sets के सभी unique elements
Intersection intersection() & Common elements
Difference difference() - पहले Set में हैं, दूसरे में नहीं
Symmetric Difference symmetric_difference() ^ केवल एक Set में मौजूद elements

Set Subset

यदि एक Set के सभी elements दूसरे Set में मौजूद हों, तो पहले Set को दूसरे Set का subset कहा जा सकता है।

A = {1, 2}
B = {1, 2, 3, 4}

print(A.issubset(B))

Output:

True

issuperset() Method

यदि दूसरे Set के सभी elements पहले Set में मौजूद हों, तो पहला Set दूसरे Set का superset हो सकता है।

A = {1, 2, 3, 4}
B = {1, 2}

print(A.issuperset(B))

Output:

True

isdisjoint() Method

यदि दो Sets में कोई common element नहीं है, तो वे disjoint Sets कहलाते हैं।

A = {1, 2, 3}
B = {4, 5, 6}

print(A.isdisjoint(B))

Output:

True

Set को List में Convert करना

numbers = {10, 20, 30}

my_list = list(numbers)

print(my_list)

Set को list में convert करने के बाद elements का order निश्चित मानना उचित नहीं है।

List को Set में Convert करना

List की duplicate values को हटाने के लिए Set में convert करना उपयोगी हो सकता है।

numbers = [10, 20, 10, 30, 20, 40]

unique_numbers = set(numbers)

print(unique_numbers)

Output का एक संभावित रूप:

{10, 20, 30, 40}
Practical Tip: Duplicate values हटाकर unique values प्राप्त करने के लिए set(list_name) एक आसान तरीका है।

Set और List में अंतर

List Set
Ordered sequence है। Index-based ordered sequence नहीं है।
Duplicate values रख सकती है। Duplicate elements को नहीं रखता।
Indexing उपलब्ध है। Indexing उपलब्ध नहीं है।
Slicing उपलब्ध है। Slicing उपलब्ध नहीं है।
[ ] का उपयोग किया जाता है। { } का उपयोग किया जाता है।

Set और Tuple में अंतर

Tuple Set
Ordered sequence है। Index-based sequence नहीं है।
Duplicate values रख सकता है। Duplicate elements नहीं रखता।
Indexing और slicing उपलब्ध हैं। Indexing और slicing उपलब्ध नहीं हैं।
Immutable है। Set में elements add/remove किए जा सकते हैं।

Python Set का Practical Example

मान लीजिए दो classes के students की IDs दी गई हैं और हमें सभी unique students की IDs प्राप्त करनी हैं।

class_a = {101, 102, 103, 104}
class_b = {103, 104, 105, 106}

all_students = class_a.union(class_b)

print(all_students)

Output का एक संभावित रूप:

{101, 102, 103, 104, 105, 106}

Common Elements निकालना

class_a = {101, 102, 103, 104}
class_b = {103, 104, 105, 106}

common_students = class_a.intersection(class_b)

print(common_students)

Output:

{103, 104}

Set के Important Methods

Method उपयोग
add() एक element जोड़ना
update() Multiple elements जोड़ना
remove() Element remove करना
discard() Element remove करना, missing element पर सामान्यतः error नहीं
pop() एक element remove करके return करना
clear() सभी elements remove करना
union() Union निकालना
intersection() Common elements निकालना
difference() Difference निकालना
symmetric_difference() Symmetric Difference निकालना
issubset() Subset check करना
issuperset() Superset check करना
isdisjoint() Common element न होने की जाँच करना

Python Set Quick Revision

Concept मुख्य बात
Set Unique elements का collection
Duplicate Duplicate elements नहीं रखे जाते
Indexing उपलब्ध नहीं
Slicing उपलब्ध नहीं
add() एक element जोड़ना
update() Multiple elements जोड़ना
remove() Element हटाना
discard() Element हटाना
Union सभी unique elements
Intersection Common elements
Difference पहले Set के unique elements
Symmetric Difference दोनों Sets के non-common elements

RBSE Class 12 Important Questions

  1. Python Set क्या है?
  2. Set की मुख्य विशेषताएँ लिखिए।
  3. Set और List में अंतर लिखिए।
  4. Set और Tuple में अंतर लिखिए।
  5. Python में Empty Set कैसे बनाते हैं?
  6. Set में duplicate elements क्यों नहीं रहते?
  7. Set में indexing क्यों नहीं होती?
  8. add() method का उपयोग बताइए।
  9. update() method का उपयोग बताइए।
  10. remove() और discard() में अंतर लिखिए।
  11. pop() method क्या करता है?
  12. Union क्या है?
  13. Intersection क्या है?
  14. Difference क्या है?
  15. Symmetric Difference क्या है?
  16. Union के लिए कौन-सा operator उपयोग होता है?
  17. Intersection के लिए कौन-सा operator उपयोग होता है?
  18. Difference के लिए कौन-सा operator उपयोग होता है?
  19. Symmetric Difference के लिए कौन-सा operator उपयोग होता है?
  20. issubset() और issuperset() क्या हैं?

Frequently Asked Questions (FAQs)

Python Set क्या है?

Python Set unique elements का collection है। इसमें duplicate elements नहीं रखे जाते और indexing उपलब्ध नहीं होती।

Python में Empty Set कैसे बनाते हैं?

Empty Set बनाने के लिए set() का उपयोग किया जाता है। {} empty dictionary बनाता है।

क्या Set में duplicate values रख सकते हैं?

Set duplicate elements को एक unique element के रूप में रखता है, इसलिए duplicate entries अलग-अलग elements के रूप में नहीं रहतीं।

Set में indexing क्यों नहीं होती?

Set को index-based sequence की तरह access नहीं किया जाता, इसलिए list और tuple की तरह numerical indexing का उपयोग नहीं किया जाता।

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

दोनों specified element को हटाते हैं, लेकिन remove() में element न मिलने पर KeyError हो सकता है जबकि discard() missing element पर सामान्यतः error नहीं देता।

Set Union क्या है?

Union दो या अधिक Sets के सभी unique elements का collection देता है। इसके लिए union() method या | operator का उपयोग किया जा सकता है।

Set Intersection क्या है?

Intersection दो Sets में मौजूद common elements को प्राप्त करता है। इसके लिए intersection() method या & operator का उपयोग किया जा सकता है।

अब अगला Topic पढ़ें

RBSE Python Study Tip: Set को समझने के लिए सबसे पहले unique elements, add(), remove(), discard() और update() समझें। इसके बाद Union, Intersection, Difference और Symmetric Difference को examples के साथ practice करें। ये concepts Python programming में बहुत उपयोगी हैं।
Official Reference:

Python Set और Set operations की विस्तृत जानकारी के लिए Python की official documentation देखें।

Python Data Structures Documentation