# Example
# Check if "banana" is present in the set:
thisset = {"apple", "banana", "cherry"}
print("cherry" in thisset)
print("kiwi" in thisset)
print("apple" in thisset,"banana" in thisset)
Output:
True
False
True True
# Change Items
# Once a set is created, you cannot change its items,
# but you can add new items.
# Add Items
# To add one item to a set use the add() method.
# To add more than one item to a set use the update() method.
# Example
# Add an item to a set, using the add() method:
thisset = {"apple", "banana", "cherry","banana","cherry"}
thisset.add("kiwi")
thisset.add("chiku")
thisset.add("apple")
print(thisset)
Output:
{'chiku', 'apple', 'banana', 'kiwi', 'cherry'}
# Example
# Add multiple items to a set, using the update() method:
thisset = {"apple", "banana", "cherry"}
print(thisset)
thisset.update(["apple", "mango", "grapes","chiku"])
#thisset.update({"orange", "mango", "grapes"})
#thisset.update(("orange", "mango", "grapes"))
print(thisset)
Output:
{'banana', 'apple', 'cherry'}
{'apple', 'chiku', 'grapes', 'cherry', 'banana', 'mango'}
# Get the Length of a Set
# To determine how many items a set has, use the len() method.
# Example
# Get the number of items in a set:
thisset = {"apple", "banana", "cherry","apple"}
print(len(thisset))
Output:
3
# Remove Item
# To remove an item in a set, use the remove(),
# or the discard() method.
# Example
# Remove "banana" by using the remove() method:
#Note: If the item to remove does not exist, remove() will raise an error.
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
#thisset.remove("kiwi")
print(thisset)
Output:
{'apple', 'cherry'}
# Remove Item
# To remove an item in a set, use the remove(),
# or the discard() method.
# Example
# Remove "banana" by using the remove() method:
#Note: If the item to remove does not exist, remove() will raise an error.
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
thisset.remove("kiwi")
print(thisset)
Output:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/python_set/8.py", line 9, in <module>
thisset.remove("kiwi")
KeyError: 'kiwi'
# Example
# Remove "banana" by using the discard() method:
#Note: If the item to remove does not exist, discard() will NOT raise an error.
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
thisset.discard("kiwi")
print(thisset)
Output:
{'apple', 'cherry'}
# You can also use the pop(), method to remove an item,
# but this method will remove the last item.
# Remember that sets are unordered,
# so you will not know what item that gets removed.
# The return value of the pop() method is the removed item.
# Example
# Remove the last item by using the pop() method:
#Note: Sets are unordered, so when using the pop() method,
# you will not know which item that gets removed.
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
x = thisset.pop()
print(x)
print(thisset)
x = thisset.pop()
print(x)
print(thisset)
Output:
apple
{'banana', 'cherry'}
banana
{'cherry'}
cherry
set()
# Example
# The clear() method empties the set:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
Output:
set()
# Example
# The del keyword will delete the set completely:
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
Output:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/python_set/12.py", line 6, in <module>
print(thisset)
NameError: name 'thisset' is not defined
# Example
# The del keyword will delete the set completely:
thisset = {"apple", "banana", "cherry"}
del thisset
#print(thisset)
Output:
# Join Two Sets
# There are several ways to join two or more sets in Python.
# You can use the union() method that returns a new set containing all items from both sets,
# Example
# The union() method returns a new set with all items from both sets:
# + operator can not be used for set to join two sets
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3,"c"}
set5={10, 20, 30}
set4=set1.union(set2)
print(set4)
set3=set1.union(set2,set5,set1)
print(set3)
# set3=set2.union(set1)
# print(set3)
#print(set1)
# set4=set1+set2
# print(set4)
Output:
{1, 2, 'c', 3, 'a', 'b'}
{1, 2, 3, 'a', 'b', 10, 'c', 20, 30}
# Example
# The update() method inserts the items in set2 into set1:
# Note: Both union() and update() will exclude any duplicate items.
# The update() method that inserts all the items from one set into another:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
print(set2)
# set2.update(set1)
# print(set2)
Output:
{'a', 1, 2, 3, 'b', 'c'}
{1, 2, 3}
# The set() Constructor
# It is also possible to use the set() constructor to make a set.
# Example
# Using the set() constructor to make a set:
thisset = set(("apple", "banana", "cherry"))
print(thisset)
thisset = set(["apple", "banana", "cherry"])
print(thisset)
thisset = set({"apple", "banana", "cherry"})
print(thisset)
Output:
{'cherry', 'banana', 'apple'}
{'cherry', 'banana', 'apple'}
{'cherry', 'banana', 'apple'}
Please explore following set methods:
# Set Methods
# Python has a set of built-in methods that you can use on sets.
# Method Description
# add() Adds an element to the set
# clear() Removes all the elements from the set
# copy() Returns a copy of the set
# difference() Returns a set containing the difference between two or more sets
# difference_update() Removes the items in this set that are also included in another, specified set
# discard() Remove the specified item
# intersection() Returns a set, that is the intersection of two other sets
# intersection_update() Removes the items in this set that are not present in other, specified set(s)
# isdisjoint() Returns whether two sets have a intersection or not
# issubset() Returns whether another set contains this set or not
# issuperset() Returns whether this set contains another set or not
# pop() Removes an element from the set
# remove() Removes the specified element
# symmetric_difference() Returns a set with the symmetric differences of two sets
# symmetric_difference_update() inserts the symmetric differences from this set and another
# union() Return a set containing the union of sets
# update() Update the set with the union of this set and others
# Dictionary
# A dictionary is a collection which is unordered, changeable and indexed.
# In Python dictionaries are written with curly brackets, and they have keys and values.
# Example
# Create and print a dictionary:
vehicle = {
"brand": "Maruti",
"model": "K10",
"cc": 1000
}
print(vehicle)
Output:
{'brand': 'Maruti', 'model': 'K10', 'cc': 1000}
Google Meet Recorded Session with OBS: