Set Operations

Python SELF EN
Level 9 , Lesson 8
Available

8.1 Operators and Methods Table

Python supports operator overloading, which means you can operate on objects not only using methods but also using operators like: +, -, *, /, |, and so on.

The set class has overloaded all possible operators to make working with sets easy and very similar to how it's done in math.

Here's the table of such operators:

Operator Method Description
| union() Returns the union of two sets.
& intersection() Returns the intersection of sets (only common elements).
- difference() Returns the difference of sets (elements present in the first set only).
^ symmetric_difference() Returns the symmetric difference of sets (elements present in one of the sets, but not both).
<= issubset() Checks if one set is a subset of another.
< issubset() Checks if one set is a proper subset of another (strict subset).
>= issuperset() Checks if one set is a superset of another.
> issuperset() Checks if one set is a proper superset of another (strict superset).
== __eq__() Checks if sets are equal (contain the same elements).
!= __ne__() Checks if sets are not equal (contain different elements).

It's super convenient to use operators when working with sets — you'll see it for yourself in a bit.

8.2 Working with Sets Using Operators

Union (OR)

Operator | Using union() function

 

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
print(union_set)  # Output: {1, 2, 3, 4, 5}
                    

 

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)  
print(union_set)  # Output: {1, 2, 3, 4, 5}
                            
                    

Intersection (AND)

Operator & Using intersection() function

 

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2
print(intersection_set)  # Output: {3}
                    

 

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {3}

Difference (DIFFERENCE)

Operator - Using difference() function

 

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
difference_set = set1 - set2
print(difference_set)  # Output: {1, 2}

                    

 

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set)  # Output: {1, 2}

Symmetric Difference (SYMMETRIC DIFFERENCE)

Operator ^ Using symmetric_difference() function

 

set1 = {1, 2, 3}
set2 = {3, 4, 5}
s_diff = set1 ^ set2
print(s_diff)  # Output: {1, 2, 4, 5}
                    

 

set1 = {1, 2, 3}
set2 = {3, 4, 5}
s_diff = set1.symmetric_difference(set2)
print(s_diff)  # Output: {1, 2, 4, 5}

It's kinda weird that operators like + or * aren't used, but hey, Python developers have their reasons.

2
Task
Python SELF EN, level 9, lesson 8
Locked
Union and Intersection
Union and Intersection
2
Task
Python SELF EN, level 9, lesson 8
Locked
Set Difference
Set Difference
1
Опрос
Sets in Python,  9 уровень,  8 лекция
недоступен
Sets in Python
Sets in Python
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION