Python Sets

Common Set operations

Set operations allow you to manipulate and combine sets in Python to create new sets based on specific criteria. Here's a breakdown of the four common set operations:


1. Union: combining elements

The union of two sets (denoted by |) combines all the unique elements from both sets into a new set. There are no duplicates in the resulting set.You can perform the union using the | operator or the union() method.

Example:

set1 = {"apple", "banana", "cherry"}
set2 = {"mango", "banana", "grape"}

union_set = set1 | set2
# OR
union_set = set1.union(set2)

print(union_set)  # Output: {'apple', 'banana', 'cherry', 'grape', 'mango'}


2. Intersection: common elements

The intersection of two sets (denoted by &) contains only the elements that are common to both sets. It identifies the elements that exist in both sets.
You can perform the intersection using the & operator or the intersection() method.

Example:

set1 = {"apple", "banana", "cherry"}
set2 = {"mango", "banana", "grape"}

intersection_set = set1 & set2
# OR
intersection_set = set1.intersection(set2)

print(intersection_set)  # Output: {'banana'}


3. Difference: elements in one set:

The difference of two sets (denoted by -) contains elements that are present in the first set but not in the second set. It excludes elements common to both sets.
You can perform the difference using the - operator or the difference() method.

Example:

set1 = {"apple", "banana", "cherry"}
set2 = {"mango", "banana", "grape"}

difference_set = set1 - set2
# OR
difference_set = set1.difference(set2)

print(difference_set)  # Output: {'apple', 'cherry'}


4. Symmetric difference: elements in either set:

The symmetric difference of two sets (denoted by ^) contains elements that are present in either set but not in both sets. It combines elements unique to each set.
You can perform the symmetric difference using the ^ operator or the symmetric_difference() method.

Example:

set1 = {"apple", "banana", "cherry"}
set2 = {"mango", "banana", "grape"}

symmetric_difference = set1 ^ set2
# OR
symmetric_difference = set1.symmetric_difference(set2)

print(symmetric_difference)  # Output: {'apple', 'cherry', 'grape', 'mango'}


Remember that these operations create new sets and don't modify the original sets. They provide a powerful way to analyze and manipulate collections of unique elements.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What is the result of the expression "set1 | set2" where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?

{"mango", "grape"}
{"apple", "banana", "cherry"}
{"banana", "grape"}
{"apple", "banana", "cherry", "mango", "grape"}
Check Answer

What does the expression "set1 & set2" return where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?

{"banana"}
{"apple", "cherry"}
{"mango", "grape"}
{"apple", "banana", "cherry", "mango", "grape"}
Check Answer

What is the result of "set1 - set2" where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?

{"apple", "banana", "cherry"}
{"banana", "grape"}
{"apple", "cherry"}
{"mango"}
Check Answer

What does the expression "set1 ^ set2" return where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?

{"apple", "cherry", "mango"}
{"banana", "grape"}
{"apple", "grape"}
{"apple", "cherry", "grape", "mango"}
Check Answer

What do set operations in Python return?

They return a list of elements.
They only return True or False.
They modify the original sets.
They return a new set without affecting the original sets.
Check Answer