In Python programming, Sets are an unordered collection of data items. In sets, every element is unique. There are no duplicate elements in a set. Sets are defined by placing elements inside curly braces {}, separated by commas.
Create Empty Set:
emp_set = set()
print(emp_set)
PythonThe Output Of This Code Will Be:
set()
PythonElements In Set
info = {"Priti", 19, True, 5.9}
print(info)
PythonThe Output Of This Code Will Be:
{True, 19, 5.9, 'Priti'}
PythonAccessing The Sets Item:
Sets are unordered collections. It means there are no orders. So that’s why we can’t use index numbers for accessing the set elements. However, you can iterate through the set using a for loop. Let’s see an example:
info = {"Priti", 19, False, 5.9}
for item in info:
print(item)
PythonThe Output Of This Code Will Be:
False
Priti
19
5.9
PythonSet Operations:
1. Add Elements In The Set:
Using`add()` method we can add new elements in the set.
info = {"first element"}
info.add("New Element")
print(info)
PythonThe Output Of This Code Will Be:
{'first element', 'New Element'}
Python2. Remove Elements From The Set:
info = {"first element", "second element", 19}
info.remove(19)
print(info)
PythonThe Output Of This Code Will Be:
{'second element', 'first element'}
Python3. Union
The union operation combines two sets, keeping only unique elements in the resulting set. In other words, it creates a new set that contains all distinct elements from both sets. Let’s see an example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print("Union:", union_set)
PythonThe Output Of This Code Will Be:
Union: {1, 2, 3, 4, 5}
Python4. Intersection:
The intersection operation contains only the elements that are common in both sets and returns a new set. It identifies the elements that exist in both sets. Let’s see an example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print("Intersection:", intersection_set)
PythonThe Output Of This Code Will Be:
Intersection: {3}
Python5. Difference:
The difference operation returns a new set containing the elements that are present in the first set but not in the second set. It identifies the elements unique to the first set. Let’s see an example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print("Difference:", difference_set)
PythonThe Output Of This Code Will Be:
Difference: {1, 2}
Python4. symmetric_difference()
This method calculates the symmetric difference between two sets, meaning it returns the elements that are present in either of the sets, but not in both. It creates a new set containing these unique elements. Let’s perform this operation:
cities = {"Kolkata", "Lucknow", "Chennai", "Delhi"}
cities2 = {"Kolkata", "Seoul", "Bangalore", "Lucknow"}
cities3 = cities.symmetric_difference(cities2)
print(cities3)
PythonThe Output Of This Code Will Be:
{'Seoul', 'Bangalore', 'Chennai', 'Delhi'}
PythonHere, `cities3` contains only cities that are unique to either of the sets.
5. symmetric_difference_update():
This method also calculates the symmetric difference, but it updates the existing set with the result. It does not create any new set or update any existing set. Let’s see an example:
cities = {"Kolkata", "Lucknow", "Chennai", "Delhi"}
cities2 = {"Kolkata", "Seoul", "Bangalore", "Lucknow"}
cities.symmetric_difference_update(cities2)
print(cities)
PythonThe Output Of This Code Will Be:
{'Bangalore', 'Delhi', 'Chennai', 'Seoul'}
PythonHere, cities contain cities that are unique to either of the sets, and the existing set is updated with these changes.
6. `difference()`:
The `difference()` method in Python sets is used to find the set of elements that are present in the original set but not in another set. It returns a new set containing the unique elements that exist only in the original set. Let’s see an example:
cities = {"Kolkata", "Chennai", "Berlin", "Delhi"}
cities2 = {"Seoul", "Siliguri", "Delhi"}
cities3 = cities.difference(cities2)
print(cities3)
PythonThe Output Of This Code Will Be:
{'Kolkata', 'Chennai', 'Berlin'}
PythonHere, `cities3` contains cities that are present in the `cities` set but not in the `cities2` set.
7. `difference_update()`:
The `difference_update()` method is similar, but it updates the existing set with the result instead of creating a new set. It modifies the original set to contain only the elements that are unique to it. Let’s see some examples:
cities = {"Kolkata", "Chennai", "Berlin", "Delhi"}
cities2 = {"Seoul", "Siliguri", "Delhi"}
cities.difference_update(cities2)
print(cities)
PythonThe Output Of This Code Will Be:
{'Kolkata', 'Berlin', 'Chennai'}
PythonHere, `cities` is updated to contain only the cities that are unique to it, and the modification is done in place.
If you don’t know the Recursion in Python” just click on Read More.
I hope you understand “Set & Set Operations In Python” Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀