Python Sets
A set is an unordered collection of items. Every element is unique (no duplicates) and must be immutable (which cannot be changed).
However, the set itself is mutable. We can add or remove items from it. Sets can be used to perform mathematical set operations like union, intersection, symmetric difference etc.
A set is created by placing all the items (elements) inside curly braces {}, separated by comma or by using the built-in function set(). Its element cannot be indexed.
Program on sets
m={10,20,40,70}
n={5,"vision",45,(2,3,4)};
print(m);
print(n);
Program on sets with duplicates
n={5,"vision",45,5,(2,3,4,5)}; #sets removes the duplicates
print(n);
print(n[2]) ; # sets cannot accept indexing it will generate errors
for k in n:
print(k);
Program to create a set using set function
n=set([1,2,3,4,5]);
print(n);
m=set([22,33,44,(5,6,7)]);
print(m);
program to create an empty set
a=set();
print(type(a));
Set Methods
Method |
Description |
Add an element to a set |
|
Remove all elements form a set |
|
Return a shallow copy of a set |
|
Return the difference of two or more sets as a new set |
|
Remove all elements of another set from this set |
|
Remove an element from set if it is a member. (Do nothing if the element is not in set) |
|
Return the intersection of two sets as a new set |
|
Update the set with the intersection of itself and another |
|
Return True if two sets have a null intersection |
|
Return True if another set contains this set |
|
Return True if this set contains another set |
|
Remove and return an arbitary set element. Raise KeyError if the set is empty |
|
Remove an element from a set. If the element is not a member, raise a KeyError |
|
Return the union of sets in a new set |
|
Update a set with the union of itself and others |
Program on add and update methods in sets
a={2,5};
print(a);
a.add(3); # add to set in ascending order
print(a);
a.add(4); # add to set in order
print(a);
a.update([1,6]); #update the set in order
print(a);
a.update([1,3,7,9]) #update the set with order eliminates duplicates
print(a);
program remove element from set using discard and remove methods.
a={"prasad","vision","rajesh","saradhi","gokul",2,5};
print(a);
a.discard("rajesh");
print(a);
a.remove(5);
print(a);
#a.remove(8); #if item is not found in set it will throw an error
a.discard(8); #if item not found it will not throw any error.
print(a);
Program on pop and clear methods
a={"prasad","vision","rajesh","saradhi","gokul",2,5};
print(a);
x=a.pop(); # randomly it will remove an item from set
print(x);
a.clear();
print(a);
Set Operations
Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetric difference.
Union: Union is performed using | operator. Same can be accomplished using the method union().
Program on union
a={1,2,3,4,5};
b={3,4,5,6,7};
print(a);
print(b);
c=a|b; # union
print(c);
d=a.union(b); #we can use union keyword
print(d);
Intersection
Intersection is performed using & operator. Same can be accomplished using the method intersection().
Program on intersection
a={1,2,3,4,5};
b={3,4,5,6,7};
print(a);
print(b);
c=a&b; # intersection
print(c);
d=a.intersection(b); #we can use intersection keyword
print(d);
Difference
Difference is performed using - operator. Same can be accomplished using the method difference().
Difference of A and B (A - B) is a set of elements that are only in A but not in B. Similarly, B - A is a set of element in B but not in A.
Program on difference
a={1,2,3,4,5};
b={3,4,5,6,7};
print(a);
print(b);
print(a-b);
print(b-a);
print(a.difference(b));
Symmetric Difference
Symmetric difference is performed using ^ operator. Same can be accomplished using the method symmetric_difference().
Symmetric Difference of A and B is a set of elements in both A and B except those that are common in both.
Program on symmetric difference
a={1,2,3,4,5};
b={3,4,5,6,7};
print(a^b); # eliminates common value in both sets
print(a.symmetric_difference(b));
program on membershipt test
a={10,30,40,50,60,70};
b={4,5,6};
print(20 in a);
print(8 not in b);
program on set functions
a={10,30,40,50,60,70};
print(len(a));
print(max(a));
print(min(a));
print(sorted(a));
print(sum(a));
for k in sorted(set("prasad")):
print(k);
Frozenset
Frozenset is a new class that has the characteristics of a set, but its elements cannot be changed once assigned. While tuples are immutable lists, frozensets are immutable sets.
Frozensets can be created using the function frozenset().
This datatype supports methods like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union(). it does not support add and remove methods.
Program on frozen set
a=frozenset([3,4,5,6]);
b=frozenset([4,5,6,7,8]);
print(a);
print(b);
#a.add(54); # not suorted add
print(a.difference(b));
print(a.union(b));
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743