Set
is used to represent group of immutable elements into a single entity. Set
objects are mutable objects. Insertion order is not preserved. Duplicate
elements are not allowed. On the top of the set objects, we can perform the
mathematical set operations like union, intersection, symmetric difference and
so on.
Example –
x=set()
print x
print type(x)
y=set([10,20,30])
print y
z={100,200,300,400,100,100}
print z
print x
print type(x)
y=set([10,20,30])
print y
z={100,200,300,400,100,100}
print z
Output –
set([])
<type
'set'>
set([10,
20, 30])
set([200,
300, 400, 100])
Note – Set objects
does not support indexing. Set allows heterogeneous elements.
Example –
x={10,20,30} print x print x[1:2] print x[0] del x[1]
Output –
set([10,
20, 30])
Traceback
(most recent call last):
File
"C:/Users/om/PycharmProjects/Pythonn/if3.py", line 3, in
<module>
print x[1:2]
TypeError:
'set' object has no attribute '__getitem__'
Example –
x={10,1.1,True,'rakesh',(20,30,40)} print x for p in x: print p
Output –
set([True,
10, 'rakesh', 1.1, (20, 30, 40)])
True
10
rakesh
1.1
(20,
30, 40)
Example –
x={10,20,30,40} print x x.add(50) print x x.update([30,60,70]) print x x.update([80,50],[15,25,35,40]) print x x.discard(30) print x x.remove(25) print x
Output –
set([40,
10, 20, 30])
set([40,
10, 20, 50, 30])
set([70,
40, 10, 50, 20, 60, 30])
set([35,
70, 40, 10, 15, 80, 50, 20, 25, 60, 30])
set([35,
70, 40, 10, 15, 80, 50, 20, 25, 60])
set([35,
70, 40, 10, 15, 80, 50, 20, 60])
Note – In discard
method, if the specified element is not present in Set then it doesn’t gives an
error whereas in remove method it gives an error.
Example –
x=set('rakesh') print x print x.pop() print x x.clear() print x print len(x)
Output –
set(['a', 'e', 'h', 'k', 's', 'r'])
a
set(['e', 'h', 'k', 's', 'r'])
set([])
0
SET MATHEMATICAL OPERATIONS
Example –
A={1,2,3,4,5} print A B={4,5,6,7,8} print B print A|B print A.union(B) print A&B print A.intersection(B) print A-B print A.difference(B) print B-A print B.difference(A) print A^B print A.symmetric_difference(B)
Output –
set([1, 2, 3, 4, 5])
set([8, 4, 5, 6, 7])
set([1, 2, 3, 4, 5, 6, 7, 8])
set([1, 2, 3, 4, 5, 6, 7, 8])
set([4, 5])
set([4, 5])
set([1, 2, 3])
set([1, 2, 3])
set([8, 6, 7])
set([8, 6, 7])
set([1, 2, 3, 6, 7, 8])
set([1, 2, 3, 6, 7, 8])
Example –
x=set('rakesh') print x print 'e' in x print 'm' in x for letter in set('apple'): print letter for p in set(range(1,5)): print p
Output –
set(['a', 'e', 'h', 'k', 's', 'r'])
True
False
a
p
e
l
1
2
3
4
Example –
s={x for x in range(10)} print s t={y**2 for y in range(10)} print t
Output –
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
set([0, 1, 4, 81, 64, 9, 16, 49, 25, 36])
PROGRAM TO ELIMINATE THE DUPLICATE ELEMENTS FROM
LIST
x=[10,20,30,10,30,40] print x print set(x)
Output –
[10, 20, 30, 10, 30, 40]
set([40, 10, 20, 30])
OR
values=[5,5,1,1,2,3,4,4,5] print values output=[] elim=set() for value in values: if value not in elim: output.append(value) elim.add(value) print output
Output –
[5, 5, 1, 1, 2, 3, 4, 4, 5]
[5, 1, 2, 3, 4]
No comments:
Post a Comment