Set_python
Set_python
Note: When you run this code, you might get output in a different
order. This is because the set has no particular order.
companies.update(tech_companies)
print(companies)
print('Initial Set:',languages)
removedValue = languages.discard('Java')
Here, we have used the discard() method to remove 'Java' from the
languages set.
Built-in Functions with Set
Here are some of the popular built-in functions that allow us to perform
different operations on a set.
# second set
B = {0, 2, 4}
Set Intersection
The intersection of two sets A and B include the common elements
between set A and B.
In Python, we use the & operator or the intersection() method to
perform the set intersection operation. For example,
# first set
A = {1, 3, 5}
# second set
B = {1, 2, 3}
# perform intersection operation using &
print('Intersection using &:', A & B)
# perform intersection operation using intersection()
print('Intersection using intersection():', A.intersection(B))
Note: A&B and intersection() is equivalent to A ⋂ B set operation.