0
0
Pythonprogramming~10 mins

Subset and superset checks in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subset and superset checks
Start with two sets A and B
Check if A is subset of B?
Print True
Check if B is superset of A?
Print True
End
We start with two sets. We check if A is a subset of B, then if B is a superset of A, printing True or False accordingly.
Execution Sample
Python
A = {1, 2}
B = {1, 2, 3}
print(A.issubset(B))
print(B.issuperset(A))
This code checks if A is a subset of B and if B is a superset of A, printing the results.
Execution Table
StepOperationEvaluationResultOutput
1Define AA = {1, 2}{1, 2}
2Define BB = {1, 2, 3}{1, 2, 3}
3Check A.issubset(B)Are all elements of A in B?True
4Print result of A.issubset(B)Print TrueTrue
5Check B.issuperset(A)Does B contain all elements of A?True
6Print result of B.issuperset(A)Print TrueTrue
7EndNo more operations
💡 All checks done, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Aundefined{1, 2}{1, 2}{1, 2}
Bundefinedundefined{1, 2, 3}{1, 2, 3}
Key Moments - 2 Insights
Why does A.issubset(B) return True even though B has an extra element?
Because subset means all elements of A are in B, extra elements in B do not matter (see execution_table step 3).
Is superset just the opposite of subset?
Yes, superset means the set contains all elements of the other set (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 4?
ATrue
BFalse
CError
DNone
💡 Hint
Check the Output column at step 4 in the execution_table.
At which step does the program check if B is a superset of A?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the Operation column for the superset check in the execution_table.
If A was {1, 4} instead of {1, 2}, what would A.issubset(B) return?
ATrue
BFalse
CError
DDepends on B
💡 Hint
Refer to the definition of subset in the key_moments and execution_table step 3.
Concept Snapshot
Subset and superset checks in Python:
- Use A.issubset(B) to check if all elements of A are in B.
- Use B.issuperset(A) to check if B contains all elements of A.
- Both return True or False.
- Extra elements in the superset do not affect the result.
- Syntax: set1.issubset(set2), set1.issuperset(set2).
Full Transcript
We start with two sets A and B. We define A as {1, 2} and B as {1, 2, 3}. Then we check if A is a subset of B by verifying if all elements of A are in B. This returns True because 1 and 2 are in B. We print True. Next, we check if B is a superset of A by verifying if B contains all elements of A. This also returns True. We print True. The program ends after these checks.