मुख्य कंटेंट पर जाएं
Python Set Methods in Hindi
PYTHON PROGRAMMING 11 मिनट पढ़ें 2 बार पढ़ा गया

Python Set Methods in Hindi

Lavkush Chakrawarti

परिचय

Set Methods क्या हैं?

Syntax

Python Set Methods की सूची

1. add()

Syntax

उदाहरण

2. update()

3. remove()

4. discard()

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

5. pop()

6. clear()

7. copy()

8. union()

9. intersection()

10. difference()

11. symmetric_difference()

12. issubset()

13. issuperset()

14. isdisjoint()

सभी Methods का सारांश

Practical Programs

Program 1 – Duplicate हटाना

Program 2 – Union

Program 3 – Intersection

Program 4 – Difference

Program 5 – Membership Test

Common Errors

KeyError

Best Practices

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

Frequently Asked Questions (FAQs)

प्रश्न 1. add() और update() में क्या अंतर है?

प्रश्न 2. remove() और discard() में क्या अंतर है?

प्रश्न 3. pop() कौन-सा Element हटाता है?

प्रश्न 4. union() और intersection() में क्या अंतर है?

प्रश्न 5. Set का सबसे बड़ा लाभ क्या है?

Quick Revision

अगला अध्याय

 

परिचय

Python में Set एक ऐसा Collection Data Type है जिसमें केवल Unique Elements Store किए जाते हैं। Set बनने के बाद उसमें नए Elements जोड़ना, पुराने Elements हटाना, दो Sets को जोड़ना, Common Elements निकालना या उनके बीच अंतर ज्ञात करना जैसे कार्य बार-बार करने पड़ते हैं।

इन कार्यों को आसान बनाने के लिए Python कई Built-in Set Methods प्रदान करता है।

इस अध्याय में Set के सभी महत्वपूर्ण Methods को Syntax, Examples, Output, Practical Programs और परीक्षा की दृष्टि से महत्वपूर्ण तथ्यों सहित विस्तार से समझेंगे।

 

Set Methods क्या हैं?

Set Methods वे Built-in Functions हैं जो केवल Set Object पर कार्य करते हैं।

Syntax

set_name.method_name(arguments)

 

Python Set Methods की सूची

क्रम

Method

उपयोग

1

add()

एक नया Element जोड़ना

2

update()

कई Elements जोड़ना

3

remove()

Element हटाना

4

discard()

सुरक्षित रूप से Element हटाना

5

pop()

कोई एक Element हटाना

6

clear()

पूरा Set खाली करना

7

copy()

Set की Copy बनाना

8

union()

दो Sets को जोड़ना

9

intersection()

Common Elements प्राप्त करना

10

difference()

Difference प्राप्त करना

11

symmetric_difference()

Symmetric Difference प्राप्त करना

12

issubset()

Subset जाँचना

13

issuperset()

Superset जाँचना

14

isdisjoint()

Common Element है या नहीं

 

  1. add()

Set में एक नया Element जोड़ने के लिए।

Syntax

set.add(element)

उदाहरण

numbers = {10,20,30}

 

numbers.add(40)

 

print(numbers)

Output

{10,20,30,40}

यदि Element पहले से मौजूद है तो Duplicate नहीं बनेगा।

 

  1. update()

एक या अधिक Elements जोड़ने के लिए।

numbers = {10,20}

 

numbers.update([30,40,50])

 

print(numbers)

Output

{10,20,30,40,50}

 

  1. remove()

किसी विशेष Element को हटाने के लिए।

numbers = {10,20,30}

 

numbers.remove(20)

 

print(numbers)

Output

{10,30}

यदि Element मौजूद नहीं है तो KeyError आएगा।

 

  1. discard()

Element हटाता है, लेकिन यदि Element मौजूद न हो तो Error नहीं देता।

numbers = {10,20,30}

 

numbers.discard(50)

 

print(numbers)

 

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

remove()

discard()

Element न मिलने पर Error देता है

Error नहीं देता

KeyError आता है

सुरक्षित Method

 

  1. pop()

Set से कोई एक Random Element हटाकर Return करता है।

numbers = {10,20,30}

 

item = numbers.pop()

 

print(item)

print(numbers)

ध्यान दें: Set Unordered होता है, इसलिए pop() कौन-सा Element हटाएगा, यह निश्चित नहीं होता।

 

  1. clear()

पूरे Set को खाली कर देता है।

numbers = {10,20,30}

 

numbers.clear()

 

print(numbers)

Output

set()

 

  1. copy()

Set की अलग Copy बनाता है।

A = {1,2,3}

 

B = A.copy()

 

print(B)

 

  1. union()

दो Sets के सभी Unique Elements प्राप्त करने के लिए।

A = {1,2,3}

 

B = {3,4,5}

 

print(A.union(B))

Output

{1,2,3,4,5}

 

  1. intersection()

दो Sets के Common Elements प्राप्त करने के लिए।

A = {1,2,3}

 

B = {2,3,4}

 

print(A.intersection(B))

Output

{2,3}

 

  1. difference()

पहले Set में मौजूद लेकिन दूसरे में नहीं।

A = {1,2,3}

 

B = {2,3,4}

 

print(A.difference(B))

Output

{1}

 

  1. symmetric_difference()

जो Elements केवल किसी एक Set में हों।

A = {1,2,3}

 

B = {2,3,4}

 

print(A.symmetric_difference(B))

Output

{1,4}

 

  1. issubset()

जाँचता है कि एक Set दूसरे का Subset है या नहीं।

A = {1,2}

 

B = {1,2,3}

 

print(A.issubset(B))

Output

True

 

  1. issuperset()

जाँचता है कि एक Set दूसरे का Superset है या नहीं।

A = {1,2,3}

 

B = {1,2}

 

print(A.issuperset(B))

Output

True

 

  1. isdisjoint()

यदि दोनों Sets में कोई Common Element नहीं है तो True Return करता है।

A = {1,2}

 

B = {5,6}

 

print(A.isdisjoint(B))

Output

True

 

सभी Methods का सारांश

Method

Return Value

Original Set बदलता है?

add()

None

update()

None

remove()

None

discard()

None

pop()

हटाया गया Element

clear()

None

copy()

नया Set

union()

नया Set

intersection()

नया Set

difference()

नया Set

symmetric_difference()

नया Set

issubset()

Boolean

issuperset()

Boolean

isdisjoint()

Boolean

 

Practical Programs

Program 1 – Duplicate हटाना

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

 

print(set(numbers))

 

Program 2 – Union

A = {1,2,3}

 

B = {3,4,5}

 

print(A.union(B))

 

Program 3 – Intersection

A = {10,20,30}

 

B = {20,30,40}

 

print(A.intersection(B))

 

Program 4 – Difference

A = {10,20,30}

 

B = {20}

 

print(A.difference(B))

 

Program 5 – Membership Test

numbers = {10,20,30}

 

if 20 in numbers:

    print("Found")

 

Common Errors

KeyError

numbers = {10,20}

 

numbers.remove(50)

❌ क्योंकि 50 Set में मौजूद नहीं है।

 

Best Practices

  • Duplicate हटाने के लिए Set का उपयोग करें।
  • Unknown Element हटाने के लिए discard() का उपयोग करें।
  • Membership Testing के लिए Set सबसे तेज़ होता है।
  • Mathematical Operations के लिए Set Methods का उपयोग करें।
  • Original Data सुरक्षित रखना हो तो copy() का उपयोग करें।

 

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

  • add() एक Element जोड़ता है।
  • update() कई Elements जोड़ता है।
  • remove() Error दे सकता है।
  • discard() Error नहीं देता।
  • pop() Random Element हटाता है।
  • union() सभी Unique Elements देता है।
  • intersection() Common Elements देता है।
  • difference() Difference देता है।
  • symmetric_difference() केवल अलग-अलग Elements देता है।

 

Frequently Asked Questions (FAQs)

प्रश्न 1. add() और update() में क्या अंतर है?

add() एक Element जोड़ता है, जबकि update() कई Elements जोड़ सकता है।

प्रश्न 2. remove() और discard() में क्या अंतर है?

remove() Element न मिलने पर KeyError देता है, जबकि discard() Error नहीं देता।

प्रश्न 3. pop() कौन-सा Element हटाता है?

कोई भी Random Element, क्योंकि Set Unordered होता है।

प्रश्न 4. union() और intersection() में क्या अंतर है?

union() सभी Unique Elements देता है, जबकि intersection() केवल Common Elements देता है।

प्रश्न 5. Set का सबसे बड़ा लाभ क्या है?

Duplicate Values हटाना और तेज़ Membership Testing।

 

Quick Revision

  • add() → एक Element जोड़ना
  • update() → कई Elements जोड़ना
  • remove() → Element हटाना
  • discard() → बिना Error हटाना
  • pop() → Random Element हटाना
  • clear() → Set खाली
  • copy() → नया Set
  • union() → सभी Unique Elements
  • intersection() → Common Elements
  • difference() → अंतर
  • symmetric_difference() → केवल अलग Elements
  • issubset() → Subset Check
  • issuperset() → Superset Check
  • isdisjoint() → Common Element Check

 

अगला अध्याय

Python Functions in Hindi – Function क्या है, User Defined Functions, Built-in Functions, Arguments, Parameters, Return Statement, Lambda Function, Recursion, Scope, *args, **kwargs, Anonymous Functions और Practical Programs। यह Python का सबसे महत्वपूर्ण अध्याय है और इसे एक Book Quality Master Guide के रूप में कवर किया जाएगा।

 

निष्कर्ष (Conclusion)

Python Set Methods add(), update(), remove(), discard(), pop(), clear(), copy(), union(), intersection(), difference(), symmetric_difference(), issubset(), issuperset(), isdisjoint() सहित सभी Set Methods उदाहरण और Practical Programs के साथ सीखें।