Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created June 15, 2020 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/853f207378a46d2bd3b4622387d9861a to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/853f207378a46d2bd3b4622387d9861a to your computer and use it in GitHub Desktop.
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