Data Type Of Python
In this section we will see new data type which is Sets.
3) Sets:- Set object is an unordered collection of distinct unhashable object. The common user include membership test removing duplicate from a sequence and computing mathematical operation such as intersection, union, differences etc.
Being an unordered collection set do not record element position or order of insertion. Accordingly set do not support indexing, slicing, stepping or other sequence like behavior.
We declare a set by using curly braces {}.
Note:- Set can only store immutable data.
- Empty set:-
S = set() - Set with multi element:-
S = {10, 'Hi', (10, 20)} - Membership:-
print(10 in S) => True
print(20 in S) => False - Iteration:-
for i in S:
print(i)
=> 10
'Hi'
(10,20) - Type casting:-
S = "Programming"
S2 = set(S)
print(S2) => {'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'}
In the above example we store our string Programming in variable S. And in variable S2 we convert this String into Set. While converting to the Sets it removes duplicate characters. In Programming there is g, m, r characters are comes multiple time but when it goes to the Sets it accepts only one character from it and remaining characters will be remove.
- add(element):- It adds an element to a set. This method has no effect if the element is already present in a set.
For example:-
S = {'p', 'r', 'g' }
S.add('q') => { 'p', 'r', 'q', 'g' }
S.add('p') => { 'p', 'r', 'q', 'g' } - clear():- It removes all elements of a set and make it empty.
print(S.clear()) => set() - difference(-):- It returns the difference of two or more set as a new set.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S2.difference(S3)) => {'p', 'w', 'o'}
In the above example it does not accept common element from two sets. It accepts only unique elements from the sets.
You can also write,
print(S2-S3) => { 'p', 'w', 'o'}
It also returns the same result. - difference_update(-=):- It removes all elements of another set from current set.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S2.difference_update(S3)) => S2 = { 'p', 'w', 'o' }
This example returns same result of difference method. But one change is in difference_update method the current set will be change or update with output. And difference method returns new set. - discard(element):- It removes an element from a set if it is a member of set. If the element is not the member it does nothing.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
S3.discard('y') => S3 = { 'x', 10}
S2.discard('A') => (It does nothing so you did not get any result) - remove(element):- It remove element from a set if it is a member of set. If the element is not a member it raises KeyError.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
S1.remove('p') => { 'r', 'g' }
S1.remove('q') => KeyError - Intersection(&):- It returns the intersection of a two sets as a new set. That means it returns common element from both sets.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S1.intersection(S2)) => { 'p' }
Here p is common element in both the sets.
print(S2&S3) => { 'x', 'y' } - intersection_update(&=):- It update current set with a intersection of itself and another.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S1.intersection_update(S2)) => S1={ 'p' }
Here set S1 is update with the output of above expression. - isdisjoint(set):- It returns true if two sets have null intersection.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S1.isdisjoint(S3)) => True
Here in S1 and S3 set does not have any common element that means it have null intersection so it return true. Now we see another example.
print(S1.isdisjoint(S2)) => False
Here in S1 and S2 set p is a common element so it not have null intersection, so it return false. - issubset(set):- It return true if another set contains current set otherwise false.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
S4 = { 'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n' }
print(S1.issubset(S4)) => True.
Here all elements of S1 set are present in the S4 set so S1 is the subset of S4.
print(S1.issubset(S2)) => False.
Here not all elements of set S1 present in the set S2 so it return false. - issuperset(set):- It return true whether current set contain another set.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
S4 = { 'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n' }
print(S3.issuperset(S4)) => False.
Here set S3 does not have all elements in S4 so it return False.
print(S4.issuperset(S1)) => True.
Here set S4 have the elements which are also in set S1. - pop():- It remove and return arbitary set element, Raises key error if set is empty.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S3.pop()) =>10
print(S3.pop()) => 'x'
print(S3.pop()) => 'y'
print(S3.pop()) => KeyError
Here it remove any element. Set not have index number so it remove any element of any position. - symmetric_difference(set):- It return the symmetric difference of two set as a new set. It remove same elements from both set and return the new set with the remaining all elements of both the sets.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
print(S1.symmetric_difference(S2)) => {'o', 'x', 'g', 'y', 'r', 'w'} - symmetric_difference_update(set):- It return the symmetric difference of two set with the update of current set.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
print(S1.symmetric_difference_update(S2)) => S1 = {'o', 'x', 'g', 'y', 'r', 'w'}
Here set S1 update with the result of symmetric difference. It is similar to the symmetric_difference but symmetric_difference_update change current set. - union(set):- It return the union of set as a new set. That means it returns new set containing all elements of both sets.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S1.union(S3)) => { 'p', 'r', 'g', 'x', 'y', '10' } - update(set):- It update a current set with union of itself with other.
For example:-
S1 = { 'p', 'r', 'g' }
S2 = { 'p', 'w', 'x', 'y', 'o' }
S3 = { 'x', 'y', '10' }
print(S1.union(S3)) => S1 = { 'p', 'r', 'g', 'x', 'y', '10' }
Here S1 is update with union of set S1 and S3.
Comments
Post a Comment