Sets - Jupyter Notebook
Sets - Jupyter Notebook
In [12]:
set_1={1,4,2,3,"hello",'vardhaman',2.48}
s3={'college','of','engineering','apple'}
print(type(set_1))
<class 'set'>
In [13]:
set2={1,1.24,'hello',(5,6,7,'list'),("in",'tuple','inside','set'),'last'}
print(set2)
set2.add('another')
set2.discard((5,6,7,'list'))
set2.remove(("in",'tuple','inside','set'))
print(set2.pop())
set2.update(set_1)
print(set2)
{1, 1.24, ('in', 'tuple', 'inside', 'set'), (5, 6, 7, 'list'), 'last', 'hell
o'}
In [14]:
print(set_1)
print(s3)
In [15]:
set_1=set((1,2.3,8,5,4,7))
set_2={1,28,4,"hello","students",2.4}
print(type(set_1))
print(type(set_2))
print(set_1)
print(set_2)
<class 'set'>
<class 'set'>
{1, 2.3, 4, 5, 7, 8}
In [16]:
In [17]:
print(set1)
In [18]:
set2={100,254,90,47,200,478,100,254,47,94,78}
print(set2)
In [19]:
set2={100,254,90,47,200,478}
print(set2)
In [20]:
print(set2)
In [21]:
Days={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}
In [22]:
print(Days)
In [23]:
for i in Days:
print(i)
Saturday
Thursday
Tuesday
Sunday
Friday
Monday
Wednesday
In [24]:
print(set1)
In [25]:
In [26]:
ls2=[1,2,4,5,2,6,8,1,9,4]
print(set(ls2))
{1, 2, 4, 5, 6, 8, 9}
In [27]:
if 'mango' in basket:
print('yes')
else:
print('no')
no
Set Functions
In [28]:
print(help(set))
class set(object)
| __and__(self, value, /)
| Return self&value.
| __contains__(...)
| x.__contains__(y) <==> y in x.
| __eq__(self, value, /)
| Return self==value.
| ge (self value /)
In [29]:
st1={2,8,6,10,45,2,89,6}
print(st1)
In [30]:
In [31]:
st1.add(12)
print(st1)
In [32]:
st1.remove(2)
print(st1)
In [34]:
---------------------------------------------------------------------------
~\AppData\Local\Temp/ipykernel_18352/884427205.py in <module>
2 print(st1)
KeyError: 20
In [35]:
In [36]:
In [37]:
st3={'hello','iam','from','vardhaman','college'}
print(st3)
In [38]:
print(st3.pop())
college
In [39]:
print(st1.clear())
{8, 10, 'string', 45, 77, 47, 'randam', 'hello', 87.98, 89}
None
In [41]:
new_set=st1.copy()
print(st1)
print(new_set)
st1.add(49)
print(st1)
print(new_set)
set()
set()
{49}
set()
In [42]:
del new_set
5. isdisjoint
6. issubset
7. issuperset
In [48]:
set1={3,6,2,1,0,0,6,7}
set2={4,5,6,7,0}
print(set1.symmetric_difference_update(set1.union(set2)))
print(set1)
None
{4, 5}
In [49]:
real = set((0,1,2,3,4,5,6,7,8,9))
odd = set((1,3,5,7,9))
even = set((2,4,6,8))
random=set((5,9,3,7,2))
print(real,odd,even,random)
In [50]:
{2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 5, 7, 9}
None
In [51]:
odd
Out[51]:
{1, 2, 3, 5, 7, 9}
In [52]:
set2.difference_update(set1)
set2
Out[52]:
{0, 6, 7}
In [53]:
set1={3,6,2,1,0,0,6,7}
set2={4,5,6,7,0}
# letters in first but not in second sets
print(set2 - set1 )
# Returns a set containing the difference between two sets
print(set1.difference_update(set2))
{4, 5}
None
In [54]:
set1
Out[54]:
{1, 2, 3}
In [55]:
8 in st1
Out[55]:
False
In [56]:
{2}
None
In [57]:
help(set1.isdisjoint)
In [58]:
set2
Out[58]:
{0, 4, 5, 6, 7}
In [59]:
print(set2.symmetric_difference_update(set1))
{8, 4, 6}
None
In [60]:
set2
Out[60]:
{0, 1, 2, 3, 4, 5, 6, 7}
Python allows us to use the comparison operators i.e., <, >, <=, >= , == with the
sets by using which we can check whether a set is a subset, superset, or equivale
nt to other set.
In [61]:
st1={1,0,2}
st2={2,0,1}
st2.issubset(st1)
Out[61]:
True
In [62]:
odd.isdisjoint(even)
Out[62]:
False
Set Comprehension
In [63]:
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39,
41, 43, 45, 47, 49}
In [64]:
In [65]:
new = set("alacazam")
print(new)
Python frozenset()
The frozenset() function returns an immutable frozenset object initialized with el
ements from the given iterable. While elements of a set can be modified at any tim
e, elements of the frozen set remain the same after creation.
In [69]:
print("Normal Set")
print(normal_set)
# A frozen set
st3 = frozenset(["e", "f", "g",'a'])
print("\nFrozen Set")
print(st3)
Normal Set
Frozen Set
In [67]:
st3.difference(st1)
Out[67]:
print(frozenset.union(normal_set))
---------------------------------------------------------------------------
~\AppData\Local\Temp/ipykernel_18352/1430241181.py in <module>
----> 1 print(frozenset.union(normal_set))
In [72]:
Out[72]:
In [73]:
print(Hash('apple'),Hash('banana'),Hash('cherry'),Hash('kiwi'),Hash('orange'),Hash('mango')
2 0 2 1 0 2
In [74]:
s.add(5)
print('Updated set after add:', s)
s.discard(5)
print('Updated set_after discard:', s)
s.remove(1)
print('Updated set after remove:', s)
s.difference_update(u)
print(f"Difference of set {s} and {u}: ",s)
t.intersection_update(u)
print(f"Intersection of set {t} and {u}: ",t)
u.symmetric_difference_update(s)
print(f"Symmetric Difference of set {u} and {s}: ",u)
s.clear()
print('Updated set after clear:', s)
length of sets S = 6 T = 5 U = 4
Minimum of sets S = 1 T = 1 U = 1
Maximum of sets S = 9 T = 6 U = 4
Sum of sets S = 25 T = 19 U = 10
Popped element 2
Excersices
4: Update the first set with items that don’t exist in the first set
5: Remove items from the set at once
7: Check if two sets have any elements in common. If yes, display the common eleme
nts
9: Remove items from set1 that are not common to both set1 and set2
In [ ]: