0
0
Pythonprogramming~10 mins

Union and intersection in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Union and intersection
Start with two sets
Find Union: all unique elements
Find Intersection: common elements
Output both results
End
We start with two sets, then find their union (all unique items) and intersection (items both sets share), and finally output the results.
Execution Sample
Python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
union = set1 | set2
intersection = set1 & set2
print(union)
print(intersection)
This code finds the union and intersection of two sets and prints them.
Execution Table
StepActionExpressionResult
1Define set1set1 = {1, 2, 3}{1, 2, 3}
2Define set2set2 = {2, 3, 4}{2, 3, 4}
3Calculate unionunion = set1 | set2{1, 2, 3, 4}
4Calculate intersectionintersection = set1 & set2{2, 3}
5Print unionprint(union){1, 2, 3, 4} (order may vary)
6Print intersectionprint(intersection){2, 3} (order may vary)
💡 All steps completed, union and intersection printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
set1undefined{1, 2, 3}{1, 2, 3}{1, 2, 3}{1, 2, 3}{1, 2, 3}
set2undefinedundefined{2, 3, 4}{2, 3, 4}{2, 3, 4}{2, 3, 4}
unionundefinedundefinedundefined{1, 2, 3, 4}{1, 2, 3, 4}{1, 2, 3, 4}
intersectionundefinedundefinedundefinedundefined{2, 3}{2, 3}
Key Moments - 3 Insights
Why does the union use the | operator instead of +?
The | operator combines all unique elements from both sets without duplicates, while + is not defined for sets and would cause an error. See execution_table step 3.
Why does the intersection use the & operator?
The & operator finds only elements present in both sets, which is the definition of intersection. See execution_table step 4.
Why might the printed order of elements vary?
Sets do not keep order, so when printed, elements can appear in any order. This is normal and expected. See execution_table steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'union' after step 3?
A{1, 2, 3, 4}
B{2, 3}
C{1, 2, 3}
Dundefined
💡 Hint
Check the 'Result' column in execution_table row with Step 3.
At which step is the intersection calculated?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action 'Calculate intersection' in execution_table.
If set2 was {3, 4, 5} instead, what would be the intersection after step 4?
A{2, 3}
B{3}
C{1, 2, 3, 4, 5}
D{}
💡 Hint
Intersection is elements common to both sets; check variable_tracker logic.
Concept Snapshot
Union and Intersection of sets in Python:
- Use | for union (all unique elements)
- Use & for intersection (common elements)
- Sets are unordered collections
- Printing sets may show elements in any order
- Useful for combining or comparing groups
Full Transcript
This visual execution shows how to find the union and intersection of two sets in Python. We start by defining two sets, set1 and set2. Then we calculate the union using the | operator, which combines all unique elements from both sets. Next, we calculate the intersection using the & operator, which finds elements present in both sets. Finally, we print both results. The execution table traces each step and the variable tracker shows how variables change. Key moments clarify why we use | and & operators and why set order is not guaranteed. The quiz tests understanding of these steps and outcomes.