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

Python Set in Hindi

Lavkush Chakrawarti

परिचय

Set क्या है?

Syntax

उदाहरण

Set की विशेषताएँ

Set बनाने के तरीके

1. Direct Set

2. set() Constructor

3. String से Set

4. Empty Set

Duplicate Values

Set में विभिन्न Data Types

Set की Length

Membership Operator

Set Traversing

Set Mutable क्यों है?

Set Operations

1. Union

2. Intersection

3. Difference

4. Symmetric Difference

Subset

Superset

Disjoint Set

Frozen Set

विशेषताएँ

Built-in Functions

Set Methods

Practical Programs

Program 1 – Duplicate हटाना

Program 2 – Union

Program 3 – Intersection

Program 4 – Difference

Program 5 – Symmetric Difference

Common Errors

TypeError

Best Practices

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

Frequently Asked Questions (FAQs)

प्रश्न 1. Set क्या है?

प्रश्न 2. क्या Set में Duplicate Values हो सकती हैं?

प्रश्न 3. क्या Set में Indexing होती है?

प्रश्न 4. Frozen Set क्या है?

प्रश्न 5. Set का सबसे बड़ा उपयोग क्या है?

Quick Revision

अगला अध्याय

 

 

परिचय

Programming में कई बार हमें ऐसे Data की आवश्यकता होती है जिसमें Duplicate Values न हों। उदाहरण के लिए किसी कक्षा में उपस्थित विद्यार्थियों की सूची, Unique User IDs, Product Codes, Email IDs या किसी परीक्षा में शामिल विद्यार्थियों का Unique Record।

यदि ऐसे Data को List में रखा जाए, तो Duplicate Values आने की संभावना रहती है। इस समस्या का समाधान Set है।

Set Python का एक Built-in Collection Data Type है, जो केवल Unique Elements को Store करता है। Set में Duplicate Values अपने आप हट जाती हैं।

Set गणित (Mathematics) के Set Theory पर आधारित है, इसलिए इसमें Union, Intersection, Difference और Symmetric Difference जैसे Operations भी उपलब्ध हैं।

 

Set क्या है?

Set Python का एक Unordered Collection Data Type है, जिसमें केवल Unique Elements Store किए जाते हैं।

Set को Curly Braces {} के अंदर लिखा जाता है।

Syntax

set_name = {item1, item2, item3}

 

उदाहरण

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

 

print(fruits)

 

Set की विशेषताएँ

  • Unordered Collection है।
  • Duplicate Values स्वतः हट जाती हैं।
  • Mutable होता है।
  • Indexing उपलब्ध नहीं होती।
  • Slicing उपलब्ध नहीं होती।
  • Elements Hashable होने चाहिए।
  • Mathematical Set Operations को Support करता है।

 

Set बनाने के तरीके

  1. Direct Set

numbers = {10, 20, 30, 40}

 

  1. set() Constructor

numbers = set([10, 20, 30])

 

  1. String से Set

letters = set("Python")

 

print(letters)

 

  1. Empty Set

❌ गलत

data = {}

यह Dictionary बनती है।

✔ सही

data = set()

 

Duplicate Values

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

 

print(numbers)

Output

{10,20,30,40}

 

Set में विभिन्न Data Types

data = {

    10,

    20.5,

    "Python",

    True

}

 

Set की Length

numbers = {10,20,30}

 

print(len(numbers))

 

Membership Operator

numbers = {10,20,30}

 

print(20 in numbers)

print(50 not in numbers)

 

Set Traversing

colors = {"Red","Blue","Green"}

 

for color in colors:

    print(color)

 

Set Mutable क्यों है?

Set में नए Elements जोड़े और हटाए जा सकते हैं, लेकिन किसी विशेष Index पर Element को बदला नहीं जा सकता क्योंकि Set में Indexing नहीं होती।

 

Set Operations

Python Set की सबसे महत्वपूर्ण विशेषता Mathematical Operations हैं।

 

  1. Union

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

A = {1,2,3}

B = {3,4,5}

 

print(A | B)

Output

{1,2,3,4,5}

 

  1. Intersection

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

A = {1,2,3}

B = {2,3,4}

 

print(A & B)

Output

{2,3}

 

  1. Difference

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

A = {1,2,3}

B = {2,3,4}

 

print(A - B)

Output

{1}

 

  1. Symmetric Difference

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

A = {1,2,3}

B = {2,3,4}

 

print(A ^ B)

Output

{1,4}

 

Subset

A = {1,2}

 

B = {1,2,3,4}

 

print(A <= B)

 

Superset

print(B >= A)

 

Disjoint Set

A = {1,2}

 

B = {5,6}

 

print(A.isdisjoint(B))

 

Frozen Set

यदि Set को Immutable बनाना हो, तो frozenset() का उपयोग किया जाता है।

numbers = frozenset([10,20,30])

 

print(numbers)

विशेषताएँ

  • Immutable होता है।
  • नए Elements नहीं जोड़ सकते।
  • हटा नहीं सकते।
  • Dictionary Key के रूप में उपयोग किया जा सकता है।

 

Built-in Functions

Function

उपयोग

len()

कुल Elements

max()

सबसे बड़ा

min()

सबसे छोटा

sum()

योग

sorted()

Sorting

any()

कोई True

all()

सभी True

 

Set Methods

Python Set के प्रमुख Methods:

  • add()
  • update()
  • remove()
  • discard()
  • pop()
  • clear()
  • copy()
  • union()
  • intersection()
  • difference()
  • symmetric_difference()
  • issubset()
  • issuperset()
  • isdisjoint()

अगले अध्याय में इन सभी Methods को एक Master Guide में विस्तार से समझेंगे।

 

Practical Programs

Program 1 – Duplicate हटाना

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

 

unique = set(numbers)

 

print(unique)

 

Program 2 – Union

A = {1,2,3}

 

B = {3,4,5}

 

print(A.union(B))

 

Program 3 – Intersection

A = {1,2,3}

 

B = {2,3,5}

 

print(A.intersection(B))

 

Program 4 – Difference

A = {1,2,3}

 

B = {2,3}

 

print(A.difference(B))

 

Program 5 – Symmetric Difference

A = {1,2,3}

 

B = {3,4,5}

 

print(A.symmetric_difference(B))

 

Common Errors

TypeError

numbers = {[1,2],3}

❌ क्योंकि List Hashable नहीं होती।

 

Best Practices

  • Duplicate Data हटाने के लिए Set का उपयोग करें।
  • Membership Test के लिए Set सबसे तेज़ होता है।
  • Mathematical Operations के लिए Set उपयुक्त है।
  • Ordered Data के लिए List का उपयोग करें।
  • Immutable Collection चाहिए तो frozenset() उपयोग करें।

 

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

  • Set में Duplicate Values नहीं होती।
  • Set Unordered Collection है।
  • Set Mutable होता है।
  • Indexing और Slicing उपलब्ध नहीं होती।
  • frozenset() Immutable होता है।
  • Union (|), Intersection (&), Difference (-), Symmetric Difference (^) महत्वपूर्ण Operators हैं।

 

Frequently Asked Questions (FAQs)

प्रश्न 1. Set क्या है?

Set Python का ऐसा Collection Data Type है जिसमें केवल Unique Values Store होती हैं।

प्रश्न 2. क्या Set में Duplicate Values हो सकती हैं?

नहीं।

प्रश्न 3. क्या Set में Indexing होती है?

नहीं।

प्रश्न 4. Frozen Set क्या है?

Immutable Set को Frozen Set कहते हैं।

प्रश्न 5. Set का सबसे बड़ा उपयोग क्या है?

Duplicate हटाने और Mathematical Set Operations करने में।

 

Quick Revision

  • Set = Unique Elements
  • Duplicate = ✘
  • Mutable = ✔
  • Ordered = ✘
  • Indexing = ✘
  • Slicing = ✘
  • frozenset() = Immutable Set
  • | = Union
  • & = Intersection
  • - = Difference
  • ^ = Symmetric Difference

 

अगला अध्याय

Python Set Methods in Hindi – add(), update(), remove(), discard(), pop(), clear(), copy(), union(), intersection(), difference(), symmetric_difference(), issubset(), issuperset() और isdisjoint() सहित सभी Set Methods की Complete Guide

 

निष्कर्ष (Conclusion)

Python Set की पूरी जानकारी हिंदी में। Set क्या है, Set Creation, Set Operations, Frozen Set, Union, Intersection, Difference, Symmetric Difference, Built-in Functions, Practical Programs और Interview Questions सीखें।