Sets: Unique Members and Venn Operations
Interactive lab
Try it: Sets: Unique Members and Venn Operations
How a Python set keeps each value once, answers membership, and combines two sets by membership alone: union, intersection, difference and symmetric difference.
How it works
- set(list) adds each value; a value already in the set changes nothing.
- x in s asks only whether the value is present (no position, no count).
- Every member is only in A, only in B, or in both.
- A | B keeps all, A & B the shared ones, A - B those only in A, A ^ B those in exactly one.
- A set has no order of its own, so the lab shows members sorted, as sorted(s) would.
Default run (16 steps): Build two sets from lists. Sets keep no order, so members are drawn sorted here (like sorted(s)); Python's own print order may differ. … sorted(A & B) → ['india', 'peru'] (2 members; sorted only for display).
Simplified: Up to 8 short words or ints per set; the members are displayed sorted because Python's set iteration order depends on hashing and is not meaningful.
Educational simulation
Loading the simulation…