Set1 - Jupyter Notebook - 091348

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

In [66]:  #lets learn about sets........

from IPython.core.interactiveshell import InteractiveShell


InteractiveShell.ast_node_interactivity = "all"

In [2]:  s={1,2,3,4,5,6}
s
type(s)

Out[2]: set

In [5]:  s=([1,2,3,4])
type(s)

Out[5]: list

In [8]:  s={1,2,3,3.9,"apple"}
print("s====>",s)
#set can contain diffrent object types....but can it???

s====> {1, 2, 3.9, 3, 'apple'}

In [11]:  s={1,2,[3,4,5]}
#even set it self is mutable but it can only contain immutable datatypes...
#mutable types like list,dic,set and bytearry cannot be element of set.

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[11], line 1
----> 1 s={1,2,[3,4,5]}

TypeError: unhashable type: 'list'

In [16]:  #sets are un-ordred collection.....that means we cannot control the order of elements
#since sets are unordred it does not support indexing or slicing....
#we will learn about indexing and slicing whene we learn strings and list...
#here is just an example....
sent="this is an string"
print("first charachter of string is==>",sent[0])
#now lets do samme with sets
s={1,2,3,4,6}
print("first element of set is==>",s[0])

first charachter of string is==> t

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[16], line 9
7 #now lets do samme with sets
8 s={1,2,3,4,6}
----> 9 print("first element of set is==>",s[0])

TypeError: 'set' object is not subscriptable


In [30]:  #how can we prove sets are unordred????
#lets do it with stings and list.....
s="abc"
s2="bac"
print("#######################are two strings equal#########")
s==s2
l=[1,2,3,4]
l2=[1,2,4,3]
print("#####################are two list equal##############")
l==l2
#here even if two strings or list have same elements there are not equal...lets do it with sets now
s={1,2,3,4}
s1={1,3,2,4}
print("#################are two sets equal###########")
s==s1

#######################are two strings equal#########

Out[30]: False

#####################are two list equal##############

Out[30]: False

#################are two sets equal###########

Out[30]: True

In [90]:  #now lets add the elements to set.....


s={1,2,3,4}
print("set====>>>",s)
#by using methods....
s.add(12)
print("after adding 12 to set using add method====>",s)
#lets say we have to add multiple elements.....
s.update([5,6,7,8])
print("set after update method======>",s)
#but there is a restriction on this method.....it only takes an iterable iteam...

set====>>> {1, 2, 3, 4}
after adding 12 to set using add method====> {1, 2, 3, 4, 12}
set after update method======> {1, 2, 3, 4, 5, 6, 7, 8, 12}

In [91]:  #restriction on update method....


s.update(1)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[91], line 2
1 #restriction on update method....
----> 2 s.update(1)

TypeError: 'int' object is not iterable

In [92]:  #we can also add iteams to set using unioun | operator....
print("set======>",s)
s|={1,2,4,5,5,5,5,5,5,5,5,1000}
print("set after unioun=====>",s)
#surprise here is another restriction on sets...because in math sets u can not have duplicate items

set======> {1, 2, 3, 4, 5, 6, 7, 8, 12}


set after unioun=====> {1, 2, 3, 4, 5, 6, 7, 8, 1000, 12}
In [107]:  #now we can also use unioun method....
us={1,2,3,"zzzzz"}
us
us.union([1,2,3,4,6,1000,"abc"])
us # why?????? because it returns a set but does not change set itself....
us=s1.union("abc")# takes each element of iterable and adds it.....
us.union(1)# same rules as update method....

Out[107]: {1, 2, 3, 'zzzzz'}

Out[107]: {1, 1000, 2, 3, 4, 6, 'abc', 'zzzzz'}

Out[107]: {1, 2, 3, 'zzzzz'}

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[107], line 7
5 us # why?????? because it returns a set but does not change set itself....
6 us=s1.union("abc")# takes each element of iterable and adds it.....
----> 7 us.union(1)

TypeError: 'int' object is not iterable

You might also like