[go: up one dir, main page]

0% found this document useful (0 votes)
48 views3 pages

Set2.py - Jupyter Notebook

The document discusses various methods available for sets in Python like add(), remove(), discard(), pop(), clear(), union(), intersection(), difference(), symmetric_difference(), len(), copy(), constructor and subset checking. It contains examples and explanations of using each method on sets and how sets behave in Python.

Uploaded by

tabish.khan.da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views3 pages

Set2.py - Jupyter Notebook

The document discusses various methods available for sets in Python like add(), remove(), discard(), pop(), clear(), union(), intersection(), difference(), symmetric_difference(), len(), copy(), constructor and subset checking. It contains examples and explanations of using each method on sets and how sets behave in Python.

Uploaded by

tabish.khan.da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In [2]:  from IPython.core.

interactiveshell import InteractiveShell


InteractiveShell.ast_node_interactivity = "all"

In [7]:  #lets try removing iteams from set.....


s={1,2,3,4,5,6,7,8}
s.remove(1)
s
#here we have to be careful because if iteam is not in set it will genrate error...
s.remove(1)# remove function takes only one argument....???

Out[7]: {2, 3, 4, 5, 6, 7, 8}

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[7], line 6
4 s
5 #here we have to be careful because if iteam is not in set it will genrate error...
----> 6 s.remove(1)

KeyError: 1

In [25]:  #we can use discard method to overcome this.......


s={1,2,3,4,5,6,7,8}
s.discard(1)
s
s.discard({2,3})
#we can also use pop method to mimicy stack.....not implement stack becuase sets are unordred
s
s.pop()#it takes no argument.....and throws error on empty set
s
x=s.pop()
print(type(x))
print(x)

Out[25]: {2, 3, 4, 5, 6, 7, 8}

Out[25]: {2, 3, 4, 5, 6, 7, 8}

Out[25]: 2

Out[25]: {3, 4, 5, 6, 7, 8}

<class 'int'>
3

In [28]:  #we can use clear method to delete all elemts from sets.....
s.clear()
print("s=======>>>>",s)

s=======>>>> set()

In [31]:  # we can use operators too....like diffrences.....


#now lets take look at how other methods work....
s1={1,2,3,4,5}
s2={6,7,8,9,10,2,3}
#union
s3=s1|s2 # unioun s3 is equal to elements which belongs to s1 or s2
print("opreator===>",s3)
s4=s1.union(s2)
print("union method====>",s4)

opreator===> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


union method====> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
In [37]:  #intersection
s1={1,2,3,4,5}
s2={6,7,8,9,10,2,3}
#opreator &
s3=s1&s2 # it returns items which are s1 and s2.
print("opreator=====>",s3)
#lets do it using method.......
s4=s1.intersection(s2)
print("method=====>>>",s4)
s1
s2

opreator=====> {2, 3}
method=====>>> {2, 3}

Out[37]: {1, 2, 3, 4, 5}

Out[37]: {2, 3, 6, 7, 8, 9, 10}

In [47]:  #difference
s1={1,2,3,4,5}
s2={6,7,8,9,10,2,3}
s3=s1-s2 #returns elements which are in first set and are not in second set....
s4=s2-s1
s3
s4
#lets use method
s5=s1.difference(s2)
print(s5)
#now we can use this to remove elements from set.....
set_names={"jhon","kim","ash","cash"}
set_names-={"jhon","cash"}
print("set_names=======>",set_names)
set_names.clear()
set_names.discard("cat")
#no error if set is empty.....
set_names=set_names.difference("to")
set_names-={"to"}

Out[47]: {1, 4, 5}

Out[47]: {6, 7, 8, 9, 10}

{1, 4, 5}
set_names=======> {'ash', 'kim'}

In [51]:  #symmetric diffrences......which are present in either set but nor in both.....
s1={1,2,3,4,5}
s2={6,7,8,9,10,2,3}
s3=s1^s2
s3
s4=s2^s1
s4
######lets do method......
s5=s1.symmetric_difference(s2)
s5

Out[51]: {1, 4, 5, 6, 7, 8, 9, 10}

Out[51]: {1, 4, 5, 6, 7, 8, 9, 10}

Out[51]: {1, 4, 5, 6, 7, 8, 9, 10}

In [54]:  # how can we know how many elements are there in set?????
s={1,2,3,4,5,6,7,8,9}
len(s)

Out[54]: 9
In [61]:  # this concept will be cleared in dymanim typying.....
s={1,2,3,4}
z=s
s.add(10)
print(z)
print(s)
#how can we get around it?????
x=s.copy() # creats shallow copy and now both are independent....
x.clear()
print(s)
d=set(s) # using constructor....
d.clear()
s

{1, 2, 3, 4, 10}
{1, 2, 3, 4, 10}
{1, 2, 3, 4, 10}

Out[61]: {1, 2, 3, 4, 10}

In [63]:  s={1,2,3}
x=s
y=s
y.clear()
x
s

Out[63]: set()

Out[63]: set()

In [71]:  #### subset


s1={1,2,3,4,5}
s2={6,7,8,9,10,2,3}
s1<=s2 #<= subset opreator
s3={1,2,3}
x=s1<=s3
print("x===",x)
type(x)
s3<=s1

Out[71]: False

x=== False

Out[71]: bool

Out[71]: True

In [ ]:  s1={1,2,3,4,5}
s2={6,7,8,9,10,2,3}

You might also like