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 :-

Is page me yeh sab sikhenge :-



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

Python
In [1]:
1
Output

Example 2: List se duplicates hatane ke liye set use karna

Python
In [1]:
1
Output

Example 3: Set indexed nahi hota — in operator se check karo

Python
In [1]:
1
Output


2. Items Add aur Remove Karna

Set me items add() aur update() se add hote hain, aur remove(), discard(), pop() se remove hote hain.

MethodKaam
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()

Python
In [1]:
1
Output

Example 2: remove() vs discard() — fark samjho

Python
In [1]:
1
Output

Example 3: pop() aur clear()

Python
In [1]:
1
Output


3. Set Operations

Set ka sabse powerful feature hai mathematical operations. Ye operations operators se bhi ho sakti hain aur methods se bhi.

OperationOperatorMethodMatlab
Uniona | ba.union(b)Dono sets ke saare items
Intersectiona & ba.intersection(b)Sirf common items
Differencea - ba.difference(b)a me jo b me nahi
Symmetric Diffa ^ ba.symmetric_difference(b)Dono me exclusive items

Example 1: Union (|) aur Intersection (&)

Python
In [1]:
1
Output

Example 2: Difference (-) aur Symmetric Difference (^)

Python
In [1]:
1
Output

Example 3: Real-life — do websites ke common visitors

Python
In [1]:
1
Output


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()

Python
In [1]:
1
Output

Example 2: intersection_update() aur difference_update()

Python
In [1]:
1
Output


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

Python
In [1]:
1
Output

Example 2: Sentence se unique words nikalna

Python
In [1]:
1
Output


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

Python
In [1]:
1
Output

Example 2: frozenset pe set operations bhi chal sakti hain

Python
In [1]:
1
Output



Quick Summary — Set ek nazar me

ConceptKaamExample
Banana{ } me itemss = {1, 2, 3}
Empty setset() use karos = set()
add(x)Ek item adds.add(5)
update()Multiple adds.update([4,5])
remove(x)Hatao — error if missings.remove(3)
discard(x)Hatao — no errors.discard(3)
Union (|)Saare itemsa | b
Intersection (&)Common itemsa & b
Difference (-)a me jo b me nahia - b
Sym. Diff (^)Exclusive itemsa ^ b
issubset()Subset checka.issubset(b)
issuperset()Superset checka.issuperset(b)
isdisjoint()Koi common nahi?a.isdisjoint(b)
frozensetImmutable setfrozenset([1,2,3])