0% found this document useful (0 votes)
4 views

Chapter 8 Set

Uploaded by

jhavidya563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 8 Set

Uploaded by

jhavidya563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Matrix Computers (Page 1 of 5)

10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan


9414752750, 9414930104, 9414244351, 0141-2399633

Chapter 8 Set and Frozenset


Set is an unordered collection of unique immutable elements within the curly braces. The set can
be created by enclosing the comma separated items within curly braces. Python also provides the set
method which can be used to create the set by the passed sequence.
s=set([1,2,3]) #{1,2,3} set from list
s=set((1,2,3)) #{1,2,3} set from tuple
s=set("abc") #{'a','b','c'} set from str
s={1,"aaa",(1,2,3),5.4,True} #number, string, bool are allowed
s={[10,20],{1,2},{1:"aaa",2:"bbb"}} #error list,set,dict are mutable

Empty set:-
s=set()
s={} it is not set. it is dict

Collection of unique elements:-


s={1,2,1,2,1} #{1,2}

It does not support indexing:-


print(s[0]) #error

It does not support slicing:-


print(s[0:]) #error

It is hetrogeneous:-
{1,5.4,"abc"}

It is iterable:-
for k in {1,2,3}:
print(k)

It is mutable:-
s1.add(4)
s1.remove(4)

It does not support ordering:-


{1,2,3}=={3,1,2} is True

Remove duplicate numbers from a list:-


l=[1,1,2,3,2]
l=list(set(l))
Operators not allowed on set:-
s1+s2 not allowed
s1*s2 not allowed
s1/s2 not allowed
Matrix Computers (Page 2 of 5)
10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan
9414752750, 9414930104, 9414244351, 0141-2399633

Union and intersection:-


s1 | s2
s1 & s2

Difference of two sets:-


The difference of two sets can be calculated by using the subtraction (-) operator. The resulting
set will be obtained by removing all the elements from set 1 that are present in set2
s1-s2 allowed it will remove common elements

Membership operator:-
10 in s1
10 not in s1

Set comparisons:-
Python allows us to use the comparison operators i.e., <, >, <=, >= , == with the sets by using which
we can check whether a set is subset, superset, or equivalent to other set. The boolean true or false is
returned depending upon the items present inside the sets.
{1,2,3}=={3,1,2} #True
Matrix Computers (Page 3 of 5)
10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan
9414752750, 9414930104, 9414244351, 0141-2399633

{1,2,3,4}>{1,2,3} #True
{1,2,3,4}>={1,2,3} #True
{1,2,3}>{1,2,3} #False
{1,2,3}>={1,2,3} #True

Identity operator:-
s1=set()
s2=set()
s1 is s2 #False
s1=set([1,2,3])
s2=set([1,2,3])
s1 is s2 #False

Built-in Set Functions:-


Sr.No. Function Description
1 len(set) Gives the total length of the set.
2 max(set) Returns item from the set with max value.
3 min(set) Returns item from the set with min value.
4 sum(set) Returns sum.
5 sorted(seq,
[reverse=True/False]) Returns a sorted list.

Python Built-in set methods:-


SN Method Description
1 add(item) It adds an item to the set. It has no effect if the item is already
present.
s1.add(10)
s1.add(5.4)

2 pop() It will remove any element randomly


s1.pop()

3 discard(item) It removes the specified item from the set.


s1.discard(3) It will remove 3 No error if not found

4 remove(item) Remove an element from a set; it must be a member. If the


element is not a member, raise a KeyError. KeyError if the set is
empty.
s1.remove(3) It will remove 3 Error if not found

5 clear() It deletes all the items from the set.


s1.clear()

del s1 It will also destroy set object

6 copy() It returns a shallow copy of the set.


Matrix Computers (Page 4 of 5)
10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan
9414752750, 9414930104, 9414244351, 0141-2399633

Reference copy
s1={1,2,3}
s2=s1
s1 is s2 #True

True/Shallow Copy
s2=s1.copy()
s1 is s2 #False

7 difference(set) will return extra elements of calling set which are not in
argument set. It will not update calling set
s1.difference(s2) #will return extra elements of s1 which are not in s2.
#It will not update s1

8 difference_update(set) will return extra elements of calling set which are not in
argument set. It will update calling set
s1.difference_update(s2) It will update s1

9 intersection() It returns a new set that contains only the common elements of
both the sets.
10 intersection_update(set)It removes the items from the original set that are not present in
both the sets
s1.intersection(s2) #return common elements but not update s1
s1.intersection_update(s2) #return common elements and update s1

11 symmetric_difference(set)
12 symmetric_difference_update(set)
Update a set with the symmetric difference of itself and another.
s1.symmetric_difference(s2) #return non common elements but will not update s1
s1.symmetric_difference_update(s2) #return non common elements but will update s1

13 union(set) Return the union of sets as a new set.


14 update() Update a set with the union of itself and others.
s1.union(s2) #show elements of both but not update s1
s1.update(s2) #show elements of both but will update s1

15 Isdisjoint(set) Return True if two sets have a null intersection.


16 Issubset(set) Report whether another set contains this set.
17 Issuperset(set) Report whether this set contains another set.

s1.isdisjoint(s2) #True if no elements are common


s1.issubset(s2) #Return True if s1 is subset of s2
s2.issuperset(s1) #Return True if s2 is superset of s1

* set comprehension technique


Matrix Computers (Page 5 of 5)
10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan
9414752750, 9414930104, 9414244351, 0141-2399633

s1={i for i in range(1,11) if i %2==0}


s1={i for i in range(1,11) if i %2!=0}

FrozenSets:-
The frozen sets are the immutable form of the normal sets, i.e., the items of the frozen set can
not be changed and therefore it can be used as a key in dictionary. The elements of the frozen set can not
be changed after the creation. We cannot change or append the content of the frozen sets by using the
methods like add() or remove(). The frozenset() method is used to create the frozenset object. The iterable
sequence is passed into this method which is converted into the frozen set as a return type of the method.

fs=frozenset() #empty frozenset


fs=frozenset(25) #error
fs=frozenset([25]) #single element
fs=frozenset("Hello")
fs=frozenset([1,2,3])
fs=frozenset((1,2,3))
fs=frozenset({1:"abc",2:"xyz",3:"aaa"}) #copy only keys

1 copy() It returns a shallow copy of the set.


2 difference(set) will return extra elements of calling set which are not in argument
set. It will not update calling set
3 intersection() It returns a new set that contains only the common elements of
both the sets.
4 symmetric_difference(set)
5 union(set) Return the union of sets as a new set.

6 Isdisjoint(set) Return True if two sets have a null intersection.


7 Issubset(set) Report whether another set contains this set.
8 Issuperset(set) Report whether this set contains another set.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy