1+ set1 = {'Ram' , 'Shyam' , 'Jenny' }
2+ set2 = {'Jenny' , 'Jiya' , 'Aakash' }
3+ set3 = {'Ankur' , 'Pradeep' }
4+
5+ print (set1 .union (set2 , set3 ))
6+ # ✅ union() → combines all sets, removes duplicates
7+ # Output: {'Ram','Shyam','Jenny','Jiya','Aakash','Ankur','Pradeep'}
8+
9+ print (set1 | set2 | set3 )
10+ # ✅ Same as union, but using | operator (all operands must be sets)
11+
12+ print (set1 .union (('Mohan' ,'Jenny' )))
13+ # ✅ union() can work with other iterables (like tuple/list), duplicates ignored
14+
15+ set1 .update (set2 )
16+ print (set1 )
17+ # ✅ update() → adds all elements of set2 into set1
18+
19+ set1 .update (['Jenny' ,'Mohan' ])
20+ print (set1 )
21+ # ✅ update() → can add elements from list/tuple also
22+
23+ set1 |= set2
24+ print (set1 )
25+ # ✅ |= operator works same as update()
26+
27+ print (set1 .intersection (set2 , set3 ))
28+ # ✅ intersection() → common elements among all sets
29+ # Here no common element → Output: set()
30+
31+ print (set1 & set2 )
32+ # ✅ & operator → intersection of two sets
33+ # Output: {'Jenny'}
34+
35+ print (set1 .intersection (['Mohan' ,'Shiva' ]))
36+ # ✅ intersection() works with other iterables too
37+ # Output: {'Mohan'} if present in set1
38+
39+ print (set1 & set2 & set3 )
40+ # ✅ intersection of all three sets
41+ # Output: set() (no common element)
42+
43+ set2 .intersection_update (set1 )
44+ print (set2 )
45+ print (set1 )
46+ # ✅ intersection_update() → modifies set2, keeps only common elements with set1
47+ # set2 becomes {'Jenny'}
48+
49+ set2 .intersection_update (['Mohan' ,'Shiva' ])
50+ print (set2 )
51+ # ✅ intersection_update() with iterable → keeps only common elements
52+ # 'Jenny' not in ['Mohan','Shiva'] → set2 becomes empty set()
0 commit comments