Ptyhion Machine Learning Code
Ptyhion Machine Learning Code
July 3, 2024
[1]: s = {}
[2]: type(s) #Please check Dictionary is a another data structure in Python and␣
↪declaryion of both set and dcit are same but usage is different
[2]: dict
[3]: s1 = {1,2,3,4,5}
[5]: type(s1)
[5]: set
[7]: s2 = {1,1,12,3,3,3,4,5,5,5,55,523,34,3,45,6,67}
[11]: l
1
[12]: set(l)
[13]: s4 = {1,2,3,4,[1,2,3,4]} #sets are mutable but not the elements of set that␣
↪means a mutable data strcuture cant be stored are element in sets
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-c88b0db15002> in <cell line: 1>()
----> 1 s4 = {1,2,3,4,[1,2,3,4]}
[14]: s5 = {1,2,3,4, (1,2,3,4)} #As tuples are immutable, it can be stored as element␣
↪of set
[15]: s5
[16]: s6 = {"sumen" , "Sumen" , 2,3,4,5} #we have to take care of case sensitivity␣
↪while storing in sets
[17]: s6
[19]: s7
[20]: s7[0] #unlike strings, list and tuples sets are not indexable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-d0678b25f3b2> in <cell line: 1>()
----> 1 s7[0] #unlike strings, list and tuples sets are not indexable
[21]: s7[::-1]
2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-d62874945df9> in <cell line: 1>()
----> 1 s7[::-1]
[22]: s7
[24]: for i in s7 : #if you want to acess the elemets of sets then it is the way
print(i)
2
3
4
5
sumen
[25]: s7
[41]: s7
[42]: s7.add(2)
[43]: s7
[44]: len(s7)
[44]: 6
[45]: s7.pop() #pop will delete smallest element of set keep in mind
[45]: 2
[46]: s7
3
[46]: {3, 34, 4, 5, 'sumen'}
[47]: s7.pop() #pop will delete smallest element of set keep in mind
[47]: 3
[48]: s7
[49]: s7.pop()
[49]: 4
[50]: s7.pop()
[50]: 5
[51]: s7
[52]: s7.clear()
[53]: s7
[53]: set()
[54]: s8 = {1,2,3,4}
s9 = {1,2,3,5}
[55]: s8.difference(s9)
[55]: {4}
[56]: s9.difference(s8)
[56]: {5}
[57]: s9.union(s8)
[57]: {1, 2, 3, 4, 5}
[59]: s9.intersection(s8) #all set operaation that we studied in you school, all are␣
↪possbile
[59]: {1, 2, 3}
4
[ ]: