Sets
SET Data class is similar to
that of Sets we use in Arithmetic. It is an in built Data class in python
unlike in other Language. The Elements are enclosed within { and } and are
unordered. The Elements can be anything which are immutable, so it can be
Integer, Float, Complex , Tuple and String but not List, set or Dictionary.
Though the elements of the sets should be of type immutable , individually the
Set Data class is mutable. ie., we can Add or Remove elements . Indexing is not
possible. That means only the whole set is referred and not the individual
elements. The Set elements are unique means no repetitions of values.
Operators
Union, Intersection,
Difference and Symmetric Difference are the operations that can be performed
like in normal Arithmetic sets.
Examples
#set of integers
s={1,2,3,4,5}
print(s)
print(type(s))
#Set of Strings
s1={'a','e','i','o','u'}
print(s1)
print(type(s1))
s2={(1,2,3),4,(1,2)}
print(s2)
print(type(s2))
s3={(1,2,3),[4,5]}
print(s3)
print(type(s3))
Output
>>>
set([1, 2, 3,
4, 5])
<type
'set'>
set(['a', 'i',
'e', 'u', 'o'])
<type
'set'>
set([(1, 2),
4, (1, 2, 3)])
<type
'set'>
It is possible to create an
Empty Set. But {} does not generate an empty set it creates empty Dictionary
only. Instead use set() statement to create an Empty set..
Add or Remove
Elements
Add Method
Since it is unordered ,
indexing does not have any meaning (will generate Error). So use 'add' method
to add an element.
s4={1,3}
print(s4)
s4.add(2)
print("After
addition,the set is",s4)
s5={'a','u'}
print(s5)
s5.add('i')
print("After
addition, the set is",s5)
Output
set([1, 3])
('After
addition,the set is', set([1, 2, 3]))
set(['a',
'u'])
('After
addition, the set is', set(['a', 'i', 'u']))
Update Method
Update method can be used to
insert multiple values at the same time.
>>> s4.update((2,3,4,5))
>>>
print("After update method, the set S4 is",s4)
('After update
method, the set S4 is', set([1, 2, 3, 4, 5]))
>>>
s4.update((4,5,6),(7,8,9,1))
>>>
print("After update method, the set S4 is",s4)
('After update
method, the set S4 is', set([1, 2, 3, 4, 5, 6, 7, 8, 9]))
Remove Method
To remove an element use
Discard or Remove method. The difference between the two is while Remove method
throws an error if the given element is not existing in the Set, the Discard
method will not throw an error.
Example
>>>
s4.discard(3)
>>>
print(s4)
set([1, 2, 4,
5, 6, 7, 8, 9])
>>>
s4.remove(9)
>>>
print(s4)
set([1, 2, 4,
5, 6, 7, 8])
>>>
s4.discard(0)
>>>
print(s4)
set([1, 2, 4,
5, 6, 7, 8])
>>>
s4.remove(0)
Traceback (most
recent call last):
File "<pyshell#17>", line 1,
in <module>
s4.remove(0)
KeyError: 0
>>>
print(s4)
set([1, 2, 4,
5, 6, 7, 8])
The difference between
Discard and Remove method is, while Discard will not throw an error if the
element is not available, Remove method will throw error.
Operators on Set
Union
We can either use Union
operator '|' or union() method for this. This operation combines 2 sets and
removes duplicates on union.
Example
a={1,3,5,7,9}
b={2,3,5,7,11}
print(a|b)
Output
set([1, 2, 3,
5, 7, 9, 11])
Intersection
We can either use
Intersection operator '&' or intersection() method for this. This operation
combines 2 sets and gives only the common elements of the two.
a={1,3,5,7,9}
b={2,3,5,7,11}
print(a&b)
Output
set([3, 5, 7])
Difference
We can either use Difference
operator '-' or difference() method for this. This operation returns a Set
after removing the elements of B from A.
a={1,3,5,7,9}
b={2,3,5,7,11}
print(a-b)
Output
set([1, 9])
Symmetric
Difference
We can either use Symmetric
Difference operator '^' or symmetric_difference() method for this. This
operation returns a Set combining the elements of A and B after removing the
common elements of A and B.
a={1,3,5,7,9}
b={2,3,5,7,11}
print(a^b)
Output
set([1, 2, 9,
11])
Other Methods
of Set
|
clear() |
Removes all the elements
of a set |
|
copy() |
Returns a shallow copy of
a set. Creates a new Set with references to the elements in the old list |
|
difference_update() |
Removes all the elements
of b in a , and is returned as a new object |
|
intersection_update() |
Returns the common
elements of set A and B as a new object |
|
pop |
Another method to remove
an element will be selected for removal until removed. If the Set is empty
then will throw error. |
No comments:
Post a Comment