Set1 - Jupyter Notebook - 091348
Set1 - Jupyter Notebook - 091348
Set1 - Jupyter Notebook - 091348
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???
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]}
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])
---------------------------------------------------------------------------
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])
Out[30]: False
Out[30]: False
Out[30]: True
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}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[91], line 2
1 #restriction on update method....
----> 2 s.update(1)
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
---------------------------------------------------------------------------
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)