SimpleTuts.com

Python Sets

Python sets are unordered collections of unique elements that support efficient membership tests, set operations, and provide methods for adding and removing elements dynamically.


#Python Sets
#Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.

thisset = {"apple", "banana", "cherry"}

#list length
print("Length : ",len(thisset))

#Get items
print("All values : ",thisset)

#add items
thisset.add("orange")
print("After add : ",thisset)

#Remove items
thisset.remove("banana")
print("After remove : ",thisset)

#check item
print("Checking an item in set:")
if "apple" in thisset:
  print("Yes, 'apple' is in the fruits set")

#Loop items
print("Print items using loop:")
for x in thisset:
  print(x)
                
Python Online Compiler