<jupyter_start><jupyter_text>SETS1. unordered2. mutable3. No duplicates<jupyter_code>myset={1,2,3}
print(myset)
myset={1,2,3,1,2}
print(myset)
myset={1,2,3}
print(myset)
myset=set({"Hello"})
print(myset)
myset={}
print(myset)
print(type(myset))

# so to be a set

myset2=set({})
print(myset2)
print(type(myset2))<jupyter_output>{}
<class 'dict'>
set()
<class 'set'><jupyter_text>Add Elements<jupyter_code>myset=set({})

myset.add(1)
myset.add(2)
myset.add(3)

print(myset)<jupyter_output>{1, 2, 3}<jupyter_text>Remove Elements remove()<jupyter_code>myset=set({})

myset.add(1)
myset.add(2)
myset.add(3)
myset.remove(3)

print(myset)<jupyter_output>{1, 2}<jupyter_text>discard()<jupyter_code>myset=set({})

myset.add(1)
myset.add(2)
myset.add(3)
a=myset.discard(4)

print(myset)
myset=set({})

myset.add(1)
myset.add(2)
myset.add(3)
myset.discard(3)

print(myset)
print(myset)
myset=set({})

myset.add(1)
myset.add(2)
myset.add(3)

myset.clear()

print(myset)
myset=set({})

myset.add(1)
myset.add(2)
myset.add(3)

print(myset.pop())
print(myset)<jupyter_output>1
{2, 3}<jupyter_text>TypeError: add() takes exactly one argument (2 given)<jupyter_code>myset=set({})

myset.add(1)
myset.add(2)
myset.add(3)

for i in myset:
  print(i)
if 1 in myset:
  print("Yes")<jupyter_output>Yes<jupyter_text>UNION<jupyter_code>odds={1,3,5,7,9}
evens={0,2,4,6,8}
primes={2,3,5,7}

u=odds.union(evens)
print(u)<jupyter_output>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}<jupyter_text>INTERSECTION<jupyter_code>odds={1,3,5,7,9}
evens={0,2,4,6,8}
primes={2,3,5,7}

u=odds.intersection(evens)
print(u)

v=odds.intersection(primes)
print(v)<jupyter_output>set()
{3, 5, 7}<jupyter_text>DIFFERENCE<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3,10,11,12}

diff=setA.difference(setB)
print(diff)

diff2=setB.difference(setA)
print(diff2)<jupyter_output>{4, 5, 6, 7, 8, 9}
{10, 11, 12}<jupyter_text>SYMMETRIC DIFFERENCE<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3,10,11,12}

symdiff=setA.symmetric_difference(setB)
print(symdiff)

symdiff2=setB.symmetric_difference(setA)
print(symdiff2)<jupyter_output>{4, 5, 6, 7, 8, 9, 10, 11, 12}
{4, 5, 6, 7, 8, 9, 10, 11, 12}<jupyter_text>UPDATE<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3,10,11,12}

setA.update(setB)
print(setA)
print(setB)<jupyter_output>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
{1, 2, 3, 10, 11, 12}<jupyter_text>INTERSECTION UPDATE<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3,10,11,12}

setA.intersection_update(setB)
print(setA)<jupyter_output>{1, 2, 3}<jupyter_text>DIFFERENCE UPDATE<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3,10,11,12}
 
setA.difference_update(setB)
print(setA)<jupyter_output>{4, 5, 6, 7, 8, 9}<jupyter_text>SYMMETRIC DIFFERENCE UPDATE<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3,10,11,12}
 
setA.symmetric_difference_update(setB)
print(setA)<jupyter_output>{4, 5, 6, 7, 8, 9, 10, 11, 12}<jupyter_text>ISSUBSET<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3}
 
print(setA.issubset(setB))
print(setB.issubset(setA))<jupyter_output>False
True<jupyter_text>ISSUPERSETif set A has all the elements of set b then set a is superset of B<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3}
 
print(setA.issuperset(setB))
print(setB.issuperset(setA))<jupyter_output>True
False<jupyter_text>ISDISJOINhave same elements then false<jupyter_code>setA={1,2,3,4,5,6,7,8,9}
setB={1,2,3}
setC={7,8}

print(setA.isdisjoint(setB))
print(setB.isdisjoint(setA))
print(setB.isdisjoint(setC))<jupyter_output>False
False
True<jupyter_text>making copy of set<jupyter_code>setA ={1,2,3,4,5,6}

setB=setA

setB.add(7)
print(setB)
print(setA)<jupyter_output>{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7}<jupyter_text>making copy of the set without changing the original set<jupyter_code>setA ={1,2,3,4,5,6}

setB=setA.copy()

setB.add(7)
print(setB)
print(setA)
setA ={1,2,3,4,5,6}

setB=set(setA)

setB.add(7)
print(setB)
print(setA)<jupyter_output>{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6}<jupyter_text>frozenset() -  cant modify after initiation<jupyter_code>a = frozenset([1,2,3,4])
print(a)
#a.add(1)
#a.remove(2)
print(a)
#AttributeError: 'frozenset' object has no attribute 'add'
#AttributeError: 'frozenset' object has no attribute 'remove'<jupyter_output>frozenset({1, 2, 3, 4})
{'fraction_non_alphanumeric': 0.20325779036827196, 'fraction_numerical': 0.08994334277620397, 'mean_word_length': 9.2590799031477, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '217876', 'license': 'MIT', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2217, 'n_words': 310, 'path': 'Set/Sets.ipynb', 'repo_name': 'avs-abhishek123/Python-Cheat-Sheet', 'size': '4236'}