-
-
Save IndhumathyChelliah/853f207378a46d2bd3b4622387d9861a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1={1,2,3,4,5} | |
#remove method will remove specified element from set. | |
s1.remove(5) | |
print (s1) #Output:{1,2,3,4} | |
#If item specified is not in set, KeyError is thrown. | |
s1.remove(6) # KeyError: 6 | |
# discard method will also remove the element from set. | |
s1.discard(4) | |
print (s1)#Output:{1,2,3} | |
# if element specified is not set means, it will not raise an error | |
s1.discard(7) | |
print (s1) #Output:{1,2,3} | |
#Since set in unordered, pop will remove any element from set.pop() method will return the removed item. | |
print(s1.pop()) #Output: 1 | |
print (s1) #Output:{2,3} | |
# clear() will empty the set. | |
s1.clear() | |
print (s1) #Output: set() | |
# del keyword is used to delete the set itself. | |
del s1 | |
#s1 is deleted. | |
print (s1) #Output: NameError: name 's1' is not defined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment