Mathematical Operations Part 2
Part of the Logic & Flow section of Coddy's Python journey — lesson 34 of 78.
Let's continue exploring mathematical operations with sets, focusing on more advanced concepts such as the intersection of multiple sets and the difference between multiple sets.
Intersection of Multiple Sets (& or intersection()): You can find the common elements among multiple sets by using the & operator or the intersection() method repeatedly or by passing multiple sets to the intersection() method.
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}
intersection_result = set1 & set2 & set3
# Or
intersection_result = set1.intersection(set2, set3)
print(intersection_result)
# Output: {3}Difference Between Multiple Sets (- or difference()): You can find the elements that are unique to the first set compared to multiple other sets by using the - operator or the difference() method. This is done by subtracting multiple sets from the first set.
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6}
set3 = {5, 6, 7}
difference_result = set1 - set2 - set3
# Or
difference_result = set1.difference(set2, set3)
print(difference_result)
# Output: {1, 2, 3}In these examples, we first find the intersection of three sets, resulting in a set containing only the element common to all three sets. Then, we find the difference between multiple sets, resulting in a set containing only the elements that are unique to set1 after subtracting the elements of set2 and set3.
Challenge
EasyImagine you are managing an exclusive club where members are split into different groups based on their interests. You want to find out:
- Who are the members that share a common interest in all groups?
- Who are the truly unique members who belong only to the first group and not to any others?
Write a program that:
- Takes three sets,
group1,group2, andgroup3, representing members of three interest groups. - Finds and prints the intersection of all three groups (members common to all groups).
- Finds and prints the difference of
group1from the other two groups (members only in the first group).
Check the test cases for the final output format!
Cheat sheet
Find common elements among multiple sets using intersection:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}
# Using & operator
intersection_result = set1 & set2 & set3
# Using intersection() method
intersection_result = set1.intersection(set2, set3)
Find elements unique to the first set compared to multiple other sets using difference:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6}
set3 = {5, 6, 7}
# Using - operator
difference_result = set1 - set2 - set3
# Using difference() method
difference_result = set1.difference(set2, set3)
Try it yourself
group1 = eval(input())
group2 = eval(input())
group3 = eval(input())
# Write your code below
# Print the results
print("Members in all groups:", sorted(list(intersection_result)))
print("Unique members in group1:", sorted(list(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