Mathematical Operations Part 1
Part of the Logic & Flow section of Coddy's Python journey — lesson 33 of 78.
Sets support mathematical operations such as union, intersection, difference, and symmetric difference. These operations are useful for comparing and combining sets in various ways.
Union (| or union()): Combines the elements from both sets, excluding duplicates.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
print(union_set)
# Output: {1, 2, 3, 4, 5}Intersection (& or intersection()): Returns a set containing only the elements that are common to both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2
print(intersection_set)
# Output: {3}Difference (- or difference()): Returns a set containing the elements that are in the first set but not in the second set.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2
print(difference_set)
# Output: {1, 2}Symmetric Difference (^ or symmetric_difference()): Returns a set containing the elements that are in either of the sets, but not in both.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set)
# Output: {1, 2, 4, 5}Challenge
EasyCreate a function named set_operations that takes two sets, set1 and set2, as arguments. The function should perform the following operations:
- Calculate the union of
set1andset2. - Calculate the intersection of
set1andset2. - Calculate the difference of
set1andset2(elements inset1but not inset2). - Calculate the symmetric difference of
set1andset2. - Return a dictionary containing the results of these operations, with the keys
"union","intersection","difference", and"symmetric_difference".
Cheat sheet
Sets support mathematical operations for comparing and combining sets:
Union (| or union()): Combines elements from both sets, excluding duplicates.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # {1, 2, 3, 4, 5}Intersection (& or intersection()): Returns elements common to both sets.
intersection_set = set1 & set2 # {3}Difference (- or difference()): Returns elements in the first set but not in the second.
difference_set = set1 - set2 # {1, 2}Symmetric Difference (^ or symmetric_difference()): Returns elements in either set, but not in both.
symmetric_difference_set = set1 ^ set2 # {1, 2, 4, 5}Try it yourself
def set_operations(set1, set2):
# Calculate the union
union_result =
# Calculate the intersection
intersection_result =
# Calculate the difference
difference_result =
# Calculate the symmetric difference
symmetric_difference_result =
# Return a dictionary containing the results
return {
"union": union_result,
"intersection": intersection_result,
"difference": difference_result,
"symmetric_difference": symmetric_difference_result
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter