Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Python Sets

Sets are an unordered collection of unique and immutable elements. There are two main types of sets - mutable sets and immutable frozensets. Sets can perform operations like union, intersection, difference and symmetric difference on other sets. Common set methods include add(), remove(), pop(), clear(), issubset(), and update().

Uploaded by

shreya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Python Sets

Sets are an unordered collection of unique and immutable elements. There are two main types of sets - mutable sets and immutable frozensets. Sets can perform operations like union, intersection, difference and symmetric difference on other sets. Common set methods include add(), remove(), pop(), clear(), issubset(), and update().

Uploaded by

shreya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Sets=>

It is an unordered collection of unique data i.e. No duplicate elements. Sets can store only
immutable type of data. Common uses of sets are removing duplicates from the sequence,
performing mathematical operations like union, intersection etc, performing membership.

Types of sets:

Sets

Mutable Immutable

-set -frozenset

Basic concepts of sets:

1)Empty set=>

#Mutable set

s=set()

#Immutable set

f=frozenset()

2)Set with multiple elements=>

#Mutable set

s={10,11.8,'Hello',('Hi','Welcome','Bye')}#Sets can store only immutable type of data

#s1={1,2,[2,3]}#TypeError: unhashable type: 'list'

#Immutable set

f=frozenset({1,2,3,'Hi','Bye'})

3)Iteration on set(for loop)=>

s={10,11.8,'Hello',('Hi','Welcome','Bye')}

for i in s:

print(i)

#10

#11.8
#'Hello'

#('Hi','Welcome','Bye')

f=frozenset ({1,2,3,'Hi','Bye'})

for i in f:

print (i)

#1

#2

#3

#'Hi'

#'Bye'

4) Membership(in, not)=>True/False

s={1,2,3,'Hi','Bye'}

print(1 in s)#T

print(11 in s)#F

print(21 not in s)#T

print ('Hi' not in s)#F

5) Removing duplicate elements from the sequence using set=>

t=(10,20,10,30,21,10,50,40,10,40,20,30,20,10)

s1=set(t)

print (s1)#{40, 10, 50, 20, 21, 30}

w='PythonProgramming'

s2=set(w)

print (s2)#{'a', 'g', 'o', 'h', 't', 'n', 'r', 'y', 'm', 'i', 'P'}

Note=> Because of unordered collection, sets does not support Indexing, hashing, slicing, striding

concatenation, replication
Methods of sets=>

1) add(element)#set
Add an element to a set.
This has no effect if the element is already present.

Note: data type of 'element' should be 'immutable'

s={1,2,3,4}

s.add(5)
print(s)#{1,2,3,4,5}
s.add(5)
print(s)#{1,2,3,4,5}

s.add('Hello')#string
s.add((1,2,3))#tuple
print(s)#{1,2,3,4,5,'Hello',(1,2,3)}

s.add([1,2,3])#Error: we cann't add mutable data

2)clear()#set

Remove all elements from this set.


s={1,2,3,4}
s.clear()
print(s)#set()

3) difference(set 2)(-)#frozenset and set

Return the difference of two or more sets as a new set.

(i.e. all elements that are in this set but not the others.)

s={1,2,3,4}
s1={2,4,5}

print(s.difference(s1))#{1,3}--->temporary result
print(s)#{1,2,3,4}

print(s1-s)#{5}

4) difference_update(set2)(-=)#set

Remove all elements of another set from the current set.


s={1,2,3,4}
s1={2,4,5}

s.difference_update(s1) #s-=s1 ---> this will update current with difference two set
print(s)#{1,3}

a=10
a-=4
print(a)#6

5)discard(element)#set

Remove an element from a set if it is a member.

If the element is not a member, do nothing.

s={1,2,3,4}
s.discard(3)
print(s)#{1,2,4}

s.discard(5)#Ignores
print(s)#{1,2,4}

6)intersection(set2)(&)#set,frozenset

Return the intersection of two sets as a new set.

(i.e. all elements that are in both sets.)

s={1,2,3,4}
s1={2,4,5}

print(s.intersection(s1))#{2,4}------>temporary result
print(s)#{1,2,3,4}
print(s1&s)#{2,4}

7)intersection_update(set2)(&=)#set

Update a set with the intersection of itself and another.

s={1,2,3,4}
s1={2,4,5}

s.intersection_update(s1)
print(s)#{2,4}

8)isdisjoint(set2)#set,frozenset
Return True if two sets have a null intersection.

s={1,2,3,4}
s1={2,4,5}
s2={'Hi','Bye'}

print(s1.isdisjoint(s2))#T
print(s1.isdisjoint(s))#F

9)issubset(set2)(<)##set,frozenset

Report whether another set contains this set.

s={1,2,3,4}
s1={2,4,5}
s2={1,2,3,4,5,6}

print(s1.issubset(s2))#T
print(s1.issubset(s))#F
print(s.issubset(s2))#T

10)issuperset(set2)(>)

Report whether this set contains another set.

s={1,2,3,4}
s1={2,4,5}
s2={1,2,3,4,5,6}

print(s2.issuperset(s1))#T
print(s2.issuperset(s))#T
print(s.issuperset(s1))#F

11)pop()#set

Remove and return an arbitrary(Unknown) set element.


Raises KeyError if the set is empty.

s={1,2,3,4}
print(s.pop())#4
print(s.pop())#3
print(s.pop())#2
print(s.pop())#1

print(s)#set()#Empty set

print(s.pop())#KeyError

12)remove(element)#set
Remove an element from a set; it must be a member.

If the element is not a member, raise a KeyError.

s={1,2,3,4}
s.remove(3)
print(s)#{1,2,4}

s.remove(5)#KeyError
13)symmetric_difference(set2)(^)#Frozenset & set

Return the symmetric difference of two sets as a new set.

(i.e. all elements that are in exactly one of the sets.)

s={1,2,3,4}
s1={2,4,5}

f=frozenset({1,2,3,4})
f1=frozenset({2,4,5})

print(s.symmetric_difference(s1))#{1,3,5}#==========>Temporary result
print(f.symmetric_difference(f1))#frozenset({1,3,5})

14)symmetric_difference_update(set2)(^=)#Set

Update a set with the symmetric difference of itself and another.

s={1,2,3,4}
s1={2,4,5}

f=frozenset({1,2,3,4})
f1=frozenset({2,4,5})

s.symmetric_difference_update(s1)
#s^=s1
print(s)#{1,3,5}

f.symmetric_difference_update(f1)#Error: frozensets are immutable

15)union(set2)(|)#set,frozenset

Return the union of sets as a new set.

(i.e. all elements that are in either set.)


s={1,2,3,4}
s1={2,4,5}

print(s.union(s1))#{1,2,3,4,5}
print(s)#{1,2,3,4}

16)update (set2)(|=)#set

Update a set with the union of itself and others.

s={1,2,3,4}
s1={2,4,5}

s.update(s1)

print(s)#{1,2,3,4,5}

You might also like