Method Description
add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
difference_update()Removes the items in this set that are also included in another, specified set
discard()Remove the specified item
intersection()Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint()Returns whether two sets have a intersection or not
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from this set and another
union()Return a set containing the union of sets
update()Update the set with the union of this set and others

Set Add method:

        Note: Order of inserted element doesn't matter.
        person = {"Anil", "Mohit", "Dinesh"}
        person.add("Rakesh") #will add Rakesh in person set
        print(person)
        person.add("Geeta") #will add Geeta in person set
        print(person)
        
Output:
        {'Rakesh', 'Mohit', 'Dinesh', 'Anil'}
        {'Mohit', 'Anil', 'Dinesh', 'Geeta', 'Rakesh'}
        

Set clear() method:

                 
                person = {"Anil", "Mohit", "Dinesh"}
                person.clear();
                print(person)

        
Output:
        set()
        

Set remove() method:

                 
                person = {"Anil", "Mohit", "Dinesh"}
                person.remove("Mohit");
                print(person)

        
Output:
        {'Dinesh', 'Anil'}
        

Set discard() method:

                 
                person = {"Anil", "Mohit", "Dinesh"}
                person.discard("Mohit");
                print(person)

        
Output:
        {'Dinesh', 'Anil'}
        

Set pop() method:

pop() method of set collection is also used to remove an item of the set. pop method remove any random element from set because pop method removes the last item of the set but last item not possible to know in a set.
                 
            person = {"Anil", "Mohit", "Dinesh","Geeta","Ram","Sonu"}
            person.pop();
            print(person) #Line 1
            
            #person.pop("Mohit"); #TypeError: set.pop() takes no arguments
            print(person.pop()) #Line 2 # will return the delete item

        
Output:
Execute First Time:
        {'Dinesh', 'Mohit', 'Ram', 'Anil', 'Sonu'} #Geeta Remove
        Sonu # Line 2 # is removed when you call pop
        
Execute Second Time:
        {'Sonu', 'Dinesh', 'Ram', 'Mohit', 'Geeta'} #Anil Remove
        
Execute Third Time:
        {'Geeta', 'Sonu', 'Dinesh', 'Anil', 'Mohit'} #Ram Remove
        

Set pop() method: also return removed item too

                 
            person = {"Anil", "Mohit", "Dinesh","Geeta","Ram","Sonu"}
            print(person.pop()) # will return the delete item
            print(person)

        
Output:
        Sonu     # is removed when you call pop
        {'Mohit', 'Geeta', 'Dinesh', 'Ram', 'Anil'}
        

Set union() method:

In Python Set's union() Method is used to combine one or more sets together. All the duplicate items of every set is remove and collectivly create a new set with unique items.
                 
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                print(setNum1.union(setNum2))
                
                #we can also perform same task with | operator :
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                print(setNum1  | setNum2)

        
Output:
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} #Duplicates are removed
        
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} #Duplicates are removed
        

Set union() method with more than one sets

                 
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                setNum3={11,33,55}
                setNum4={22,44}
                print(setNum1.union(setNum2,setNum3,setNum4))
                
                
                #we can also perform same task with | operator :
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                setNum3={11,33,55}
                setNum4={22,44}
                print(setNum1  | setNum2 | setNum3 | setNum4)
        
Output:
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 11, 44, 22, 55} #Duplicates are removed
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 11, 44, 22, 55} #Duplicates are removed
        

Set union() method with more than one sets using chain rule.

                #Set union() method with more than one sets using chain rule.
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                setNum3={11,33,55}
                setNum4={22,44}
                print(setNum1.union(setNum2).union(setNum3).union(setNum4))
                
                
                #we can perform same task with string more than one sets using chain rule.
                A = {'red', 'blue', 'white'}
                B = {'red', 'green', 'black'}
                C = {'pink', 'red', 'white', 'orange'}

                union = A.union(B).union(C)
                print(union)
                
                #we can also perform same task with | operator :
                A = {'red', 'blue', 'white'}
                B = {'red', 'green', 'black'}
                C = {'pink', 'red', 'white', 'orange'}
                print(A|B|C)
        
Output:
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 11, 44, 22, 55} #Duplicates are removed
    {'white', 'black', 'green', 'pink', 'blue', 'red', 'orange'} #Duplicates are removed
    {'green', 'red', 'pink', 'white', 'orange', 'black', 'blue'} #Duplicates are removed
        

Set union() method vs | operator

1. Set Operation With | operator Both operands must be sets.
2. Set Operation With union() method will take any iterable as an input, convert it to a set, and then conduct the union.
Note: union () method input could be list, set, tuple as well as dictionary too but dictionary keys will combine not value.
                #Set and List using union() method
                num = {1, 2, 3} #num is set
                name = ['Anil', 'Mohan', 'Geeta'] #name is List
                print(num.union(name)) #union of List and Set
                
                #Set and List using | operator
                num = {1, 2, 3} #num is set
                name = ['Anil', 'Mohan', 'Geeta'] #name is List
                print(num | name ) #union of List and Set
        
Output:
        {1, 2, 3, 'Anil', 'Mohan', 'Geeta'} #Set and List using union() method
        

Traceback (most recent call last): #Error: Set and List using | operator File "C:/Users/user/Desktop/1.py", line 3, in print(num | name ) #union of List and Set TypeError: unsupported operand type(s) for |: 'set' and 'list'

Set union() method with list, tuple and dictionary

1. Set Operation With | operator Both operands must be sets.
2. Set Operation With union() method will take any iterable as an input, convert it to a set, and then conduct the union.
Note: union () method input could be list, set, tuple as well as dictionary too but dictionary keys will combine not value.
                #Set and List using union() method
                num = {1, 2, 3} #num is set
                name = ['Anil', 'Mohan', 'Geeta'] #name is List
                print(num.union(name)) #union of List and Set
                
                
                #Set and Tuple using union() method
                num = {1, 2, 3,4,5} #num is set
                name = (1,'Anil', 'Mohan',2, 'Geeta') #name is Tuple
                print(num.union(name)) #union of Tuple and Set
                
                
                #Set and Dictionary using union() method
                num = {1, 2, 3} #num is set
                name = ['Anil':11, 'Mohan':12, 'Geeta':13] #name is Dictionary
                print(num.union(name)) #union of Dictionary and Set
        
Output:
{1, 2, 3, 'Anil', 'Mohan', 'Geeta'}  #Set and List using union() method
{1, 2, 3, 4, 5, 'Geeta', 'Anil', 'Mohan'} #Set and tupel using union() method
{1, 2, 3, 'Mohan', 'Geeta', 'Anil'}  #Set and dictionary using union() method [only keys combine with set]
        

Set intersection Method

Set intersection method is used to determined the common items that resides in two or more sets
                #Set intersection() Method
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                print(setNum1.intersection(setNum2)) #will return comman record
                
                #we can also perform same task with & operator :
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                print(setNum1  & setNum2)   #will return comman record
        
Output:
       {3, 6, 7}
       {3, 6, 7}
        

Set intersection() method with more than one sets

                #Set intersection() method with more than one sets
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                setNum3={11,33,55,3}
                setNum4={22,3,44}
                print(setNum1.intersection(setNum2,setNum3,setNum4))


                #we can also perform same task with & operator :
                setNum1={1,3,6,5,7,1,9}
                setNum2={2,4,3,6,8,7,10}
                setNum3={11,33,55,3}
                setNum4={22,3,44}
                print(setNum1  & setNum2 & setNum3 & setNum4)
        
Output:
        {3}
        {3}
        

Set intersection() method with more than one sets using chain rule.

        #Set intersection() method with more than one sets using chain rule.
        setNum1={1,3,6,5,7,1,9}
        setNum2={2,4,3,6,8,7,10}
        setNum3={11,33,3,55}
        setNum4={22,3,44}
        setNum5={77,88}
        print(setNum1.intersection(setNum2).intersection(setNum3).intersection(setNum4))
setNum6=setNum1.intersection(setNum2).intersection(setNum3).intersection(setNum4) print(setNum5.intersection(setNum6)) #we can perform same task with string more than one sets using chain rule. A = {'red', 'blue', 'white'} B = {'red', 'green', 'black'} C = {'pink', 'red', 'white', 'orange'} intersection = A.intersection(B).intersection(C) print(intersection) #we can also perform same task with & operator : A = {'red', 'blue', 'white'} B = {'red', 'green', 'black'} C = {'pink', 'red', 'white', 'orange'} print(A&B&C)
Output:
                {3}
                set()
                {'red'}
                {'red'}
        

Set intersection() method vs & operator

1. Set Operation With & operator Both operands must be sets.
2. Set Operation With intersection() method will take any iterable as an input, convert it to a set, and then conduct the union.
                #Set and List using intersection() method
                num = {1, 2, 3} #num is set
                name = ['Anil', 'Mohan', 'Geeta'] #name is List
                print(num.intersection(name)) #intersection of List and Set
                
                #Set and List using & operator
                num = {1, 2, 3} #num is set
                name = ['Anil', 'Mohan', 'Geeta'] #name is List
                print(num & name ) #intersection of List and Set
        
Output:
        set()
        Traceback (most recent call last):         #Error: Set and List using & operator
        File "C:/Users/user/Desktop/1.py", line 9, in 
        print(num & name ) #intersection of List and Set
        TypeError: unsupported operand type(s) for &: 'set' and 'list'

        

The copy() method :

Make the another copie of the existing set.
                persion = {"Anil", "Mohan", "Geeta","Sonu","Rahul","Jyoti"}
                copiedPerson = persion.copy()
                print(copiedPerson)
        
Output:
                {'Geeta', 'Anil', 'Mohan', 'Sonu', 'Rahul', 'Jyoti'}
        

The difference() method :

difference() method return the difference between the two sets results in a new set that has elements from the first set which aren’t present in the second set
                #difference() function using Number set
                totalRoll={111,112,113,114,115,116,117,118,119,120}
                failRoll={113,116}
                print(totalRoll.difference(failRoll)) #Only Passed Student List
                
                #difference function using String set
                totalStudent = {"Anil", "Mohan", "Geeta","Sonu","Rahul","Jyoti","Rajni"}
                failedStudent ={"Geeta","Rahul","Jyoti"}

                print(totalStudent.difference(failedStudent)) #Only Passed Student Name
                
                
                #Another way to differenciate the another set
                totalRoll={111,112,113,114,115,116,117,118,119,120}
                failRoll={113,116}
                print(totalRoll  - failRoll)

                totalStudent = {"Anil", "Mohan", "Geeta","Sonu","Rahul","Jyoti","Rajni"}
                failedStudent ={"Geeta","Rahul","Jyoti"}

                print(totalStudent - failedStudent)
        
Output:
                {111, 112, 114, 115, 117, 118, 119, 120}    #Failed Student doesn't exist
                {'Anil', 'Rajni', 'Sonu', 'Mohan'}          #Failed Student doesn't exist
                
                 #Output using another way to differenciate set
                {111, 112, 114, 115, 117, 118, 119, 120}    #Failed Student doesn't exist
                {'Mohan', 'Rajni', 'Anil', 'Sonu'}          #Failed Student doesn't exist
        

Set difference() method with more than one sets using chain rule.

                totalRoll={111,112,113,114,115,116,117,118,119,120}
                failRoll={113,116}
                gameStudent={112,118,120};
                print(totalRoll.difference(failRoll).difference(gameStudent))

                totalStudent = {"Anil", "Mohan", "Geeta","Sonu","Rahul","Jyoti","Rajni"}
                fialStudent ={"Geeta","Rahul","Jyoti"}
                gameStudent ={"Mohan","Jyoti","Rajni"}
                print(totalStudent.difference(fialStudent).difference(gameStudent))
        
Output:
                {111, 114, 115, 117, 119}
                {'Sonu', 'Anil'}
        

Set difference() vs - operator

1. Set Operation With - operator Both operands must be sets.
2. Set Operation With difference() method will take any iterable as an input, convert it to a set, and then conduct the union.
                totalRoll={111,112,113,114,115,116,117,118,119,120} #Line 1
                failRoll=[113,116] #Line 2

                print(totalRoll.difference(failRoll)) #Line 3
                #print(totalRoll - failRoll))  #Error #Line 4

                totalStudent = {"Anil", "Mohan", "Geeta","Sonu","Rahul","Jyoti","Rajni"} #Line 5
                fialStudent =["Geeta","Rahul","Jyoti"] #Line 6

                print(totalStudent.difference(fialStudent)) #Line 7
                #print(totalStudent - fialStudent) #Error #Line 8
        
Output:
            {111, 112, 114, 115, 117, 118, 119, 120}    #Line 3
            {'Anil', 'Rajni', 'Mohan', 'Sonu'}          #Line 7
        
        
           Traceback (most recent call last):            #Error: Set and List using - operator #Line 4
           File "./prog.py", line 5, in 
           TypeError: unsupported operand type(s) for -: 'set' and 'list'
       
       
            Traceback (most recent call last):         #Error: Set and List using - operator #Line 8
            File "./prog.py", line 11, in 
            TypeError: unsupported operand type(s) for -: 'set' and 'list'

        
3 of 3