- Python Set, unordered item का collection होता है।
- Set, duplicate वैल्यू को remove करता है।
- Set , Mutable होता है अर्थात हम set में change (add और remove) कर सकते है।
- Set , indexing और slicing को support नहीं करता।
- Set , Hashing के concept को follow करता है।
Create a Set
set1 ={"tech","computer",25,96,25.63}
print(set1)
print(type(set1))
OUTPUT
{'computer', 96, 'tech', 25, 25.63}
<class 'set'>
Create a set Using set() method
set1 =set(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])
print(set1)
print(type(set1))
OUTPUT
{'Friday', 'Wednesday', 'Thursday', 'Sunday', 'Saturday', 'Tuesday', 'Monday'}
<class 'set'>
Empty Set
a= set()
print(a)
print(type(a))
OUTPUT
set()
<class 'set'>
Creating a set which have immutable elements
set1 = {1,2,3, "JavaTpoint", 20.5, 14}
print(set1)
print(type(set1))
set ={1,2,3, 9,("JavaTpoint",86,25.6), 20.5, 14}
print(set)
print(type(set))
OUTPUT
{1, 2, 3, 14, 'JavaTpoint', 20.5}
<class 'set'>
{1, 2, 3, 9, ('JavaTpoint', 86, 25.6), 14, 20.5}
<class 'set'>
Creating a set which have mutable element
set2 = {1,2,3,["Javatpoint",4]}
print(type(set2))
OUTPUT
TypeError Traceback (most recent call last)
<ipython-input-21-d78e1c57fb3e> in <module>()
---------> 1 set2 = {1,2,3,["Javatpoint",4]}
2 print(type(set2))
TypeError: unhashable type: 'list'
note : अर्थात set अपने अंदर mutable element को स्टोर नहीं करता।
Duplicates element Not Allowed
Set, duplicate वैल्यू को remove करता है।
set1={"tech",29,36,"tech",89,29,78,32}
print(set1)
OUTPUT
{32, 36, 'tech', 78, 89, 29}
Set Methods
add() | set के अंदर नया element add करना। |
clear() | set के सारे element को remove करता है। |
copy() | set की copy को return करता है। |
difference() | दो या दो से अधिक सेटों के बीच के difference वाले set को return करता है। |
difference_update() | set से उस item को रिमूव करता है जो item दोनों set में हो और साथ ही उस सेट को update करता है। |
discard() | यह method सेट से किसी specific item को remove करता है। |
intersection() | दो सेट में स्टोर एक जैसे आइटम को return करता है। |
intersection_update() | intersection_update() method उन item को रिमूव करती है जो आइटम दोनों sets में मौजूद नहीं हैं। और set को update करता है। |
isdisjoint() | यदि दोनों सेटों में से एक जैसा कोई भी item मौजूद नहीं है, तो isdisjoint () method true return करता है, अन्यथा false |
issubset() | Returns whether another set contains this set or not |
issuperset() | Returns whether this set contains another set or not |
pop() | Removes an element from the set |
remove() | Removes the specified element |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from this set and another |
union() | यह एक नया set return करता है जिसमे original सेट के सारे item होते है। |
update() | Update the set with the union of this set and others |
add( ) method
set के अंदर नया element add करना।
veg= {"carrot","corn","onion","potato"}
veg.add("pumpkin")
print(veg)
OUTPUT
{'pumpkin', 'onion', 'potato', 'carrot', 'corn'}
clear() method
set के सारे element को remove करता है।
veg= {"carrot","corn","onion","potato"}
veg.clear()
print(veg)
OUTPUT
set()
आगे पढ़े - python set methods