This is part 12 of a series of python coding I am having fun with and want to show others interested or stuck.
Code Example:
#Creating and Working with sets
#Finding the number of items in a set
#Add and delete items using the add(), remove(), & update() method
#Create the sets
fruit_set = {"apple", "banana", "cherry"}
tropical_set = {"pineapple", "mango", "papaya"}
#print the number of items in the fruit_set
print(fruit_set)
print(len(fruit_set))
#add items to the set by using the .add() method
fruit_set.add("orange")
print(fruit_set)
print(len(fruit_set))
#Add items from two sets together using the update() method
fruit_set.update(tropical_set)
print(fruit_set)
print(len(fruit_set))
#Say you accidentally added a non-fruit item to the set
fruit_set.add('pen')
print(fruit_set)
#You can remove the item from the fruit set by using the
#.remove() method
fruit_set.remove('pen')
print(fruit_set)
#python #pythonprogramming #pythontutorial #pythonforbeginners #shortsfeed
Music: Paid Subscription with Wondershare Filmora