Python me Sets kya hote hain?
Set Python ka ek unordered collection hai jisme duplicate values nahi hoti. Matlab agar aap same value baar baar daalo, woh sirf ek baar store hogi. Set ka use mainly unique items store karne aur mathematical set operations (union, intersection, difference) ke liye hota hai.
Set ki 4 main khasiyatein hain :-
- Unordered — items ka koi fixed order nahi hota
- No Duplicates — same value sirf ek baar store hogi
- Mutable — items add ya remove ho sakte hain
- Not Indexed — index se access nahi kar sakte (s[0] nahi chalega)
Is page me yeh sab sikhenge :-
- 1. Set banana aur basics
- 2. Items add aur remove karna
- 3. Set Operations — union, intersection, difference, symmetric difference
- 4. Set Methods — subset, superset, disjoint
- 5. Set Comprehension
- 6. Frozen Set
1. Set Banana aur Basics
Set ko curly brackets { } me likha jaata hai. Note: Empty set banane ke liye set() likhna padta hai — sirf { } likhne se empty dictionary ban jaati hai!
Example 1: Set banana — duplicates automatic remove ho jaate hain
Example 2: List se duplicates hatane ke liye set use karna
Example 3: Set indexed nahi hota — in operator se check karo
2. Items Add aur Remove Karna
Set me items add() aur update() se add hote hain, aur remove(), discard(), pop() se remove hote hain.
| Method | Kaam |
|---|---|
| add(x) | Ek item add karta hai |
| update(iterable) | Multiple items add karta hai |
| remove(x) | Item remove — nahi mila toh KeyError |
| discard(x) | Item remove — nahi mila toh koi error nahi |
| pop() | Koi bhi ek random item remove karke return |
| clear() | Saare items hata deta hai |
Example 1: add() aur update()
Example 2: remove() vs discard() — fark samjho
Example 3: pop() aur clear()
3. Set Operations
Set ka sabse powerful feature hai mathematical operations. Ye operations operators se bhi ho sakti hain aur methods se bhi.
| Operation | Operator | Method | Matlab |
|---|---|---|---|
| Union | a | b | a.union(b) | Dono sets ke saare items |
| Intersection | a & b | a.intersection(b) | Sirf common items |
| Difference | a - b | a.difference(b) | a me jo b me nahi |
| Symmetric Diff | a ^ b | a.symmetric_difference(b) | Dono me exclusive items |
Example 1: Union (|) aur Intersection (&)
Example 2: Difference (-) aur Symmetric Difference (^)
Example 3: Real-life — do websites ke common visitors
4. Set Methods — Subset, Superset aur In-place Operations
Set ke kuch aur useful methods hain — check karne ke liye ki ek set doosre ka subset ya superset hai, aur in-place update ke liye.
Example 1: issubset(), issuperset(), isdisjoint()
Example 2: intersection_update() aur difference_update()
5. Set Comprehension
List comprehension ki tarah Set Comprehension bhi hoti hai — ek line me set banana with conditions. Automatically duplicates remove ho jaate hain.
Example 1: Squares ka set — duplicates hatenge automatically
Example 2: Sentence se unique words nikalna
6. Frozen Set
frozenset ek immutable set hai — yaani ek baar banane ke baad isme koi change nahi ho sakta. Yeh bilkul waise hai jaise tuple, list ka immutable version hota hai. frozenset ko dictionary key ke roop me bhi use kar sakte hain (normal set nahi).
Example 1: frozenset banana aur use karna
Example 2: frozenset pe set operations bhi chal sakti hain
Quick Summary — Set ek nazar me
| Concept | Kaam | Example |
|---|---|---|
| Banana | { } me items | s = {1, 2, 3} |
| Empty set | set() use karo | s = set() |
| add(x) | Ek item add | s.add(5) |
| update() | Multiple add | s.update([4,5]) |
| remove(x) | Hatao — error if missing | s.remove(3) |
| discard(x) | Hatao — no error | s.discard(3) |
| Union (|) | Saare items | a | b |
| Intersection (&) | Common items | a & b |
| Difference (-) | a me jo b me nahi | a - b |
| Sym. Diff (^) | Exclusive items | a ^ b |
| issubset() | Subset check | a.issubset(b) |
| issuperset() | Superset check | a.issuperset(b) |
| isdisjoint() | Koi common nahi? | a.isdisjoint(b) |
| frozenset | Immutable set | frozenset([1,2,3]) |