Learning navigation

Code with Animation

Sets & Mathematical Operations

Harness unordered collections of unique elements, O(1) membership testing, mathematical Venn diagram operations, and the empty set syntax trap.

Python6 min readConcept 8 of 24

Unordered Collections of Unique Elements

A set is an unordered, mutable collection of distinct, hashable objects. Duplicate items added to a set are silently and automatically discarded: {1, 2, 2, 3} becomes {1, 2, 3}.

Under the hood, a set is implemented as a hash table where keys point to dummy values. Because lookup checks the hash value directly, checking membership with x in my_set runs in blazing-fast O(1) average constant time.

In contrast, checking x in my_list requires scanning elements one by one from left to right, taking O(n) linear time. For large collections, set lookups are thousands of times faster.

Mathematical Set Operations & Venn Algebra

Python provides native operators that mirror mathematical set theory:

Union (a | b): Returns a new set containing all elements present in either set.

Intersection (a & b): Returns only the elements present in both sets simultaneously.

Difference (a - b): Returns elements that belong to set a but not to set b.

Symmetric Difference (a ^ b): Returns elements that exist in either a or b, but strictly not in both.

Set Syntax & Operator vs Method Differences

Sets are constructed using curly braces: colors = {"red", "green", "blue"}, or by converting any iterable with set([1, 2, 3]).

Syntax trap: writing {} creates an empty dictionary, not an empty set. To create an empty set, you must use the constructor: empty = set().

Operators vs Methods: operators like a | b require both operands to be set types. In contrast, set methods like a.union([4, 5, 6]) accept any arbitrary iterable (lists, tuples, generators) without requiring explicit conversion.

The Venn Diagram & Set Algebra Stage

Select mathematical operations between Set A and Set B. Watch how the interactive Venn diagram highlights the matching regions and computes the resulting set in real time.

# 1. Defined sets:
A = {1, 2, 3}
B = {3, 4, 5}
# 2. Mathematical operation:
res = A | B
# 3. Evaluated result:
print(res)
# {1, 2, 3, 4, 5}
SET VENN ALGEBRA: A = {1, 2, 3} vs B = {3, 4, 5}31245Set ASet BEVALUATED EXPRESSION: A | B{1, 2, 3, 4, 5}Unordered collection of distinct hashable values
★

Python Rule: Writing {} creates an empty dictionary. To create an empty set, always call the constructor: set().

Union (|) merges both sets, including every element that appears in either set.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What is the data type of the expression `empty_val = {}` in Python?
  2. 2Given `a = {1, 2, 3}` and `b = {2, 3, 4}`, what is the result of the symmetric difference `a ^ b`?
  3. 3Why is `x in my_set` significantly faster than `x in my_list` for a collection with 1,000,000 elements?

Remember this

  • Sets store unordered, unique elements; duplicates are silently discarded.
  • Membership testing (x in s) is O(1) average time, vastly outperforming O(n) list scans.
  • Empty sets must be created with set(), because {} creates an empty dictionary.
  • Set operations: union (|), intersection (&), difference (-), and symmetric difference (^).
  • To remove duplicates while preserving list order, use list(dict.fromkeys(items)).

Done with this concept?

Mark it complete to track your progress. No login needed.