Chapter 8 Python
Chapter 8 Python
Eg:
1. s={}
2. print(s)
3. print(type(s))
4.
5. Output
6. {}
7. Eg:
1. s=set()
2. print(s)
3. print(type(s))
4.
5. Output
6. set()
………………………………………………………………………………………..
*********Important functions of set:**********:
……………………………………………………………………………………..
1. add(x):-
Adds item x to the set
Eg:
1. s={10,20,30}
2. s.add(40);
3. print(s) #{40, 10, 20, 30}
……………………………………………………………………………………
2. update(x,y,z):-
To add multiple items to the set.
Arguments are not individual elements and these are
Iterable objects like List,range etc. All elements present
in the given Iterable objects will be added to the set.
Eg:
1. s={10,20,30}
2. l=[40,50,60,10]
3. s.update(l,range(5))
4. print(s)
6. Output
7. {0, 1, 2, 3, 4, 40, 10, 50, 20, 60, 30}
……………………………………………………………………………………….
Q. What is the difference between add() and update()
functions in set?
:-We can use add() to add individual item to the
Set,where as we can use update() function to add
multiple items to Set.
add() function can take only one argument where as
update() function can take any number of arguments but
all arguments should be iterable objects.
……………………………………………………………………………………
Q. Which of the following are valid for set s?
1. s.add(10)
2. s.add(10,20,30) TypeError: add() takes exactly one
argument (3 given)
3. s.update(10) TypeError: 'int' object is not iterable
4. s.update(range(1,10,2),range(0,10,2))
.............................................
3. copy(): -Returns copy of the set.
It is cloned object.
s={10,20,30}
s1=s.copy()
print(s1)
…………………………………………………..
4. pop():
It removes and returns some random element from
the set.
Eg:
1. s={40,10,30,20}
2. print(s)
3. print(s.pop())
4. print(s)
5.
6. Output
7. {40, 10, 20, 30}
8. 40
9. {10, 20, 30}
…………………………………………………………….
5. remove(x):-
It removes specified element from the set.
If the specified element not present in the Set then we
will get KeyError.
s={40,10,30,20}
s.remove(30)
print(s) # {40, 10, 20}
s.remove(50) ==>KeyError: 50
…………………………….
6. discard(x):
It removes the specified element from the set.
If the specified element not present in the set then we
won't get any error.
s={10,20,30}
s.discard(10)
print(s) ===>{20, 30}
s.discard(50)
print(s) ==>{20, 30}
………………………………………………………………………………………
Q. What is the difference between remove() and
discard() functions in Set?
Q. Explain differences between pop(),remove() and
discard() functionsin Set?
………………………………………………………………
7.clear():-
To remove all elements from the Set.
1. s={10,20,30}
2. print(s)
3. s.clear()
4. print(s)
5.
6. Output
7. {10, 20, 30}
8. set()
…………………………………………………………………………
********Mathematical operations on the Set:****:-
………………………………………………………………….
1.union():-
x.union(y) ==>We can use this function to return all
elements present in both sets x.union(y) or x|y
…………………
Eg:
x={10,20,30,40}
y={30,40,50,60}
print(x.union(y)) #{10, 20, 30, 40, 50, 60}
print(x|y) #{10, 20, 30, 40, 50, 60}
………………………………….
2. intersection():-
x.intersection(y) or x&y