pop() function: This function will remove any random element from the set.
It will return that element which is removed by it.
If set is empty this function will give error (KeyError).
s1={1,2,3,4}
s1.pop()
print(s1) # {2,3,4}
discard() function: This function will remove the specified element from the set.
The return type of this function is None.
s1={1,2,3,4}
s1.discard(2)
print(s1) # {1,3,4}
There is no error come if the specified element is not present in the set or if the set is empty.
remove() function: The return type of this function is None.
It will give error (KeyError) if the specified element is not present in the set or if the set is empty.
s1={1,2,4}
s1.remove(2)
print(s1) # {1,4}
copy() function: This function is used to copy whole data of one set to other set object.
s1={1,3,4}
s2=s1.copy()
print(s2) # {1,3,4}
Note** List cannot be an element of set.
Tuple can be an element of set.
It will return that element which is removed by it.
If set is empty this function will give error (KeyError).
s1={1,2,3,4}
s1.pop()
print(s1) # {2,3,4}
discard() function: This function will remove the specified element from the set.
The return type of this function is None.
s1={1,2,3,4}
s1.discard(2)
print(s1) # {1,3,4}
There is no error come if the specified element is not present in the set or if the set is empty.
remove() function: The return type of this function is None.
It will give error (KeyError) if the specified element is not present in the set or if the set is empty.
s1={1,2,4}
s1.remove(2)
print(s1) # {1,4}
copy() function: This function is used to copy whole data of one set to other set object.
s1={1,3,4}
s2=s1.copy()
print(s2) # {1,3,4}
Note** List cannot be an element of set.
Tuple can be an element of set.
Be First to Post Comment !
Post a Comment