Part 12 | Python Programming | Creating and working with sets

Published: 08 January 2023
on channel: oOSylarTechOo
24
1

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


On this page of the site you can watch the video online Part 12 | Python Programming | Creating and working with sets with a duration of hours minute second in good quality, which was uploaded by the user oOSylarTechOo 08 January 2023, share the link with friends and acquaintances, this video has already been watched 24 times on youtube and it was liked by 1 viewers. Enjoy your viewing!