In python, a set is another type of unordered collection of unique and immutable objects. We cannot access elements using index value like we do in lists. Sets are typically used for mathematical operations like union, intersection, difference, etc.
Creating a set
There are two ways to define a set in python,
A set can be created by placing all the elements inside curly braces{}, and the elements are separated by comma.
Example:
my_set = {"Python", "HTML", "Java", "JavaScript", "CSS"}
print(my_set)
print(type(my_set))
Output:
{'Python', 'Java', 'JavaScript', 'HTML', 'CSS'}
<class 'set'>
The another way to creating a set is by using built-in set() function. This set() function is also used to create a empty set.
Example:
my_set = set(["Python", "HTML", "Java", "JavaScript", "CSS"])
print(my_set)
print(type(my_set))
Output:
{'JavaScript', 'CSS', 'Java', 'Python', 'HTML'}
<class 'set'>
Access Elements in Set
sets are unordered collection that’s we cannot access elements in a set by using it’s index value. We can access all the elements together by using for loop.
Example:
my_set = {"Python", "HTML", "Java", "JavaScript", "CSS"}
for i in my_set:
print(i)
Output:
HTML
JavaScript
CSS
Python
Java
Add elements in a set
Note: Once a set is created we cannot change its elements.
We can add elements to set by using two add() and update() method.
add() method
To add single element we use add() method.
Example:
my_set = {"Python", "HTML", "Java", "JavaScript", "CSS"}
print(my_set)
my_set.add("PHP")
print(my_set)
Output:
{'JavaScript', 'Python', 'HTML', 'Java', 'CSS'}
{'JavaScript', 'PHP', 'Python', 'HTML', 'Java', 'CSS'}
update() method:
To add multiple items to a set use update() method.
Example:
my_set = {"Python", "HTML", "Java", "JavaScript", "CSS"}
print(my_set)
my_set.update("R", "C")
print(my_set)
Output:
{'Python', 'CSS', 'Java', 'JavaScript', 'HTML'}
{'Python', 'CSS', 'Java', 'C', 'JavaScript', 'HTML', 'R'}
Remove element in set
Python provides two ways to remove an item in a set, remove() and discard() method.
remove() method
remove() method will remove the item only if the item present in the set. If the item does not exist in the set remove() method will raise an error.
Example:
my_set = {"Python", "HTML", "Java", "JavaScript", "CSS"}
my_set.remove("HTML")
print(my_set)
Output:
{'Python', 'Java', 'JavaScript', 'CSS'}
discard() method
discard() method will remove the item if the item present in the set, if the item does not exist in the set discard() method will not raise an error.
Example:
my_set = {"Python", "HTML", "Java", "JavaScript", "CSS"}
my_set.discard("HTML")
print(my_set)
Output:
{'Python', 'JavaScript', 'Java', 'CSS'}
To delete entire set use del() keyword.
Example:
my_set = {"Python", "HTML", "Java", "JavaScript", "CSS"}
del my_set
print(my_set)
Output:
Traceback (most recent call last):
File "mytup.py", line 3, in <module>
print(my_set)
NameError: name 'my_set' is not defined
Frozen sets
frozenset is a new class that has the characteristics of a set. frozenset are immutable sets that is once a frozenset is assigned we cannot modified(add, update, remove, etc.) the elements in frozenset.
Example:
my_set = frozenset(["Python", "Java","JavaScript", "PHP"])
print(my_set)
print(type(my_set))
Output:
frozenset({'PHP', 'Java', 'JavaScript', 'Python'})
<class 'frozenset'>
Python set Operations
Set can be performed mathematical operations like union, intersection, difference and symmetric difference.
sets Union
union() function is used to merge two sets and return a new set containing all items. It is performed using | operator.
Example:
my_set1 = {"Python", "Java", "JavaScript", "PHP"}
my_set2 = {"CSS", "HTML", "C"}
my_set3 = my_set1.union(my_set2)
print(my_set3)
Output:
{'Python', 'C', 'CSS', 'PHP', 'HTML', 'Java', 'JavaScript'}
Sets Intersection
Intersection() function will return the elements that are present in both sets. It is performed using & operator.
Example:
my_set1 = {"Python", "Java","JavaScript", "HTML", "PHP"}
my_set2 = {"CSS", "HTML", "JavaScript", "C", "Java"}
my_set3 = my_set1.intersection(my_set2)
print(my_set3)
Output:
{'JavaScript', 'Java', 'HTML'}
Sets difference
difference() function will return only the elements from the first set and none from the second set.
Example:
my_set1 = {"Python", "Java","JavaScript", "HTML", "PHP"}
my_set2 = {"CSS", "HTML", "JavaScript", "C", "Java"}
my_set3 = my_set1.difference(my_set2)
print(my_set3)
Output:
{'PHP', 'Python'}
Sets symmetric difference
symmetric_difference() function will remove element which is present in both sets.
Example:
my_set1 = {"Python", "Java","JavaScript", "HTML", "PHP"}
my_set2 = {"CSS", "HTML", "JavaScript", "C", "Java"}
my_set3 = my_set1.symmetric_difference(my_set2)
print(my_set3)
Output:
{'C', 'CSS', 'PHP', 'Python'}
Leave a Comment