Sets
Sets Python का एक Build-in-Data Type है जिसका उपयोग Unique element को collect करने के लिए किया जाता है sets में Duplicate value तथा unordered (क्रमबद्ध) नहीं होता है और इसमें indexing तथा Slicing भी नहीं होती है |
Characteristics of Sets
- Unordered – Sets में Elements का कोई निश्चित क्रम (order) नहीं होता हैं।
- Unique Elements – Sets में Duplicate values अपने आप हट जाती हैं।
- Mutable – Set में elements को add और remove किया जा सकता है।
- No Indexing – Sets में Elements को index (
set[0]) से access नहीं किया जा सकता। - No Slicing – Set पर slicing (
set[1:3]) नहीं की जा सकती हैं। - Immutable Elements – Set के elements immutable (जैसे
int,float,str,tuple) होने चाहिए। - Curly Braces
{}याset()function का उपयोग करके Set बनाया जाता है।
Types of sets
1. Set (Mutable Set)
- Mutable Sets एक Normal sets होता है |
- इसमें element को add, remove और update किया जा सकता है अर्थात यह एक mutable set होता है |
- Mutable Sets value को unordered में रखता है और Duplicate value को remove करता है |
- Sets के Element को curly brackets ({}) के अंदर लिखा जाता है और हर एक element को comma(,) से separate किया जाता है |
Source Code :
set1 = {"H", "e", "l", "l", "o"}
#elements in curly braces
set2 = set("Hello")
#passing string to set function
set3 = set(["H", "e", "l", "l", "o"])
#passing list to set function
set4 = set(("H", "e", "l", "l", "o"))
#passing tuple to set function
set5 = set()
#empty set
2. Frozen sets (Immutable set)
- Frozen Set एक Immutable sets होता है |
- इसे बनाने के बाद इसके element को add, remove या update नहीं किया जा सकता है |
- इसमें duplicates Value नहीं होती है और सभी value एक unordered form में होता है |
- प्रोजेक्ट को भोजन फंक्शन के द्वारा क्रिएट किया जाता हैइस डिक्शनरी कीकी या किसी अन्य सेट की एलिमेंट के रूप में उसे किया जा सकता है
Source Code :
# Frozen Set Example
numbers = frozenset([10, 20, 30, 40])
print("Frozen Set :", numbers)
3. Mixed Data Types Set
Sets mutable items को support नहीं करता है sets में [1,2] यह मुटेबल आइटम है |
Source Code :
set = {1, "H", 2.6, [1,2]}
#Output
# set = {1, "H", 2.6, [1,2]}
#TypeError: unhashable type: 'list'
sets में tuple का इस्तेमाल किया जा सकता है क्योंकि sets और double यह immutable होता है |
Source Code :
set = {1, "H", 2.6, (1,2)}
4. Empty sets
Sets के element को curly brackets ({}) में लिखा जाता है लेकिन अगर empty curly braces ({}) का इस्तेमाल किया जाता है तो वह ‘dictionary type’ हो जाता है | empty set के लिए empty set() function को देना पड़ता है |
Source Code :
set1 = {}
print(type(set1))
#Output : <class 'dict'>
set2 = set()
print(type(set2))
#Output : <class 'set'>
Removing Duplicate Element
sets duplicate elements को remove कर देता है |
Source code :
set = {"H", "e", "l", "l", "o"}
print(set)
#Output : {'o', 'l', 'H', 'e'}
Try to changing set’s element
set में element की value index से change नहीं की जा सकती है | set यह indexing को support नहीं करता है |
Source Code :
set = {1, 2, 5, 3, 4}
set[1] = 6
#Output :
# set[1] = 6
#TypeError: 'set' object does not support item assignment
Adding element using add() method to set
sets में अगर एक elements को add करना हो तो add() method का use किया जाता है |
Source Code :
set = {1, 2, 5, 3, 4}
set.add(6)
print(set)
Removing Element using remove() method from set
sets में अगर एक elements को remove करना हो तो remove() method का use किया जाता है |
Source Code :
set = {1, 2, 5, 3, 4}
set.remove(5)
print(set)
Removing all elements using ‘clear()’ method from set
sets में से सभी elements को remove करना हो तो clear() method का use किया जाता है |
Source Code :
set = {1, 2, 5, 3, 4}
set.clear()
print(set)
Iterating over a set
हर element को iterate through print करने के लिए ‘for_in’ loop का use किया जाता है |
Source Code :
set = {1, 2, 5, 3, 4}
for i in set:
print(i)
- इसे भी पड़े : Python Lists in Hindi
- इसे भी पड़े : python Tuple in Hindi
- इसे भी पड़े : Python Dictionary in Hindi
Set Function in Python
| Function / Method | Description | Example |
|---|---|---|
add() | एक element जोड़ता है | s.add(10) |
update() | एक साथ कई elements जोड़ता है | s.update([1,2,3]) |
remove() | Element हटाता है, नहीं मिलने पर Error देता है | s.remove(5) |
discard() | Element हटाता है, नहीं मिलने पर Error नहीं देता | s.discard(5) |
pop() | कोई भी random element हटाकर return करता है | s.pop() |
clear() | पूरा set खाली कर देता है | s.clear() |
copy() | Set की copy बनाता है | s2=s.copy() |
union() | दो sets को जोड़ता है | a.union(b) |
intersection() | Common elements देता है | a.intersection(b) |
difference() | पहले set के अलग elements देता है | a.difference(b) |
symmetric_difference() | दोनों sets के अलग-अलग elements देता है | a.symmetric_difference(b) |
intersection_update() | Common elements से original set update करता है | a.intersection_update(b) |
difference_update() | Difference से original set update करता है | a.difference_update(b) |
symmetric_difference_update() | Symmetric Difference से original set update करता है | a.symmetric_difference_update(b) |
issubset() | Check करता है कि subset है या नहीं | a.issubset(b) |
issuperset() | Check करता है कि superset है या नहीं | a.issuperset(b) |
isdisjoint() | Check करता है कि दोनों sets में common element नहीं है | a.isdisjoint(b) |
Sets method in Python
| Method | Syntax | working | Return Value |
|---|---|---|---|
add() | set.add(x) | Set में एक नया Element जोड़ता है। | None |
update() | set.update(iterable) | एक या अधिक Elements जोड़ता है। | None |
remove() | set.remove(x) | Element हटाता है, न मिलने पर KeyError देता है। | None |
discard() | set.discard(x) | Element हटाता है, न मिलने पर Error नहीं देता। | None |
pop() | set.pop() | किसी एक (अनिश्चित) Element को हटाकर Return करता है। | Removed Element |
clear() | set.clear() | पूरे Set को खाली कर देता है। | None |
copy() | set.copy() | Set की Copy बनाता है। | New Set |
union() | set1.union(set2) | दोनों Sets के सभी Unique Elements देता है। | New Set |
intersection() | set1.intersection(set2) | दोनों Sets के Common Elements देता है। | New Set |
difference() | set1.difference(set2) | पहले Set के वे Elements देता है जो दूसरे Set में नहीं हैं। | New Set |
symmetric_difference() | set1.symmetric_difference(set2) | दोनों Sets के अलग-अलग (Non-common) Elements देता है। | New Set |
intersection_update() | set1.intersection_update(set2) | Original Set को केवल Common Elements से Update करता है। | None |
difference_update() | set1.difference_update(set2) | Original Set से Common Elements हटा देता है। | None |
symmetric_difference_update() | set1.symmetric_difference_update(set2) | Original Set को Symmetric Difference से Update करता है। | None |
issubset() | set1.issubset(set2) | जाँचता है कि पहला Set दूसरे Set का Subset है या नहीं। | True / False |
issuperset() | set1.issuperset(set2) | जाँचता है कि पहला Set दूसरे Set का Superset है या नहीं। | True / False |
isdisjoint() | set1.isdisjoint(set2) | जाँचता है कि दोनों Sets में कोई Common Element नहीं है। | True / False |
1. add() Method क्या है?
Answer : add() Method का उपयोग Set में एक नया Element जोड़ने के लिए किया जाता है। यदि Element पहले से मौजूद है, तो Set में कोई परिवर्तन नहीं होता है।
2. update() Method क्या है?
Answer : update() Method का उपयोग Set में एक साथ कई Elements जोड़ने के लिए किया जाता है। यह List, Tuple, Set आदि से भी Elements जोड़ सकता है।
